summaryrefslogtreecommitdiff
path: root/ept/debtags/coll/TextFormat.cc
blob: dac5696896d5561e638eefc7bab6d20822f841ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * Serialize a tagged collection to a text file
 *
 * Copyright (C) 2003--2015  Enrico Zini <enrico@debian.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 */

#include "TextFormat.h"
#include <stdexcept>
#include <system_error>

using namespace std;
using namespace wibble;

namespace tagcoll {
namespace textformat {

// Parse an element
// Return the trailing separating char, that can be:
//  input::Input::Eof
//  '\n'
//  ':'
//  ','
// Return the item in `item'

// element: \s*[^ \t,:]\s*([.:])\s*
// or
// element: \s*[^ \t,:].*?[^ \t,:]\s*([.:])\s+
int parseElement(FILE* in, const std::string& pathname, string& item)
{
    item = string();
    string sep;
    int c;
    char sepchar = 0;
    enum {LSPACE, ITEM, ISPACE, ISEP, TSPACE} state = LSPACE;
    while ((c = getc(in)) != EOF)
    {
        if (c == '\n')
        {
            if (sepchar && sepchar != ':')
                throw std::runtime_error("separator character ends the line");
            else
                return '\n';
        }
        switch (state)
        {
            // Optional leading space
            case LSPACE:
                switch (c)
                {
                    case ' ':
                    case '\t':
                        break;
                    case ':':
                    case ',':
                        throw std::runtime_error("element cannot start with a separation character");
                        break;
                    default:
                        item += c;
                        state = ITEM;
                        break;
                }
                break;
            // Non-separating characters
            case ITEM:
                switch (c)
                {
                    case ' ':
                    case '\t':
                        sep += c;
                        state = ISPACE;
                        break;
                    case ':':
                    case ',':
                        sepchar = c;
                        sep += c;
                        state = ISEP;
                        break;
                    default:
                        item += c;
                        break;
                }
                break;
            // Space inside item or at the end of item
            case ISPACE:
                switch (c)
                {
                    case ' ':
                    case '\t':
                        sep += c;
                        break;
                    case ':':
                    case ',':
                        sepchar = c;
                        state = TSPACE;
                        break;
                    default:
                        item += sep;
                        item += c;
                        sep = string();
                        state = ITEM;
                        break;
                }
                break;
            // Separator inside item or at the end of item
            case ISEP:
                switch (c)
                {
                    case ' ':
                    case '\t':
                        if (sep.size() > 1)
                            throw std::runtime_error("item is followed by more than one separator characters");
                        state = TSPACE;
                        break;
                    case ':':
                    case ',':
                        sep += c;
                        break;
                    default:
                        item += sep;
                        item += c;
                        sepchar = 0;
                        sep = string();
                        state = ITEM;
                        break;
                }
                break;
            case TSPACE:
                switch (c)
                {
                    case ' ':
                    case '\t':
                        break;
                    default:
                        ungetc(c, in);
                        return sepchar;
                }
                break;
        }
    }
    if (ferror(in))
        throw std::system_error(errno, std::system_category(), "cannot read from " + pathname);
    return EOF;
}

}
}

#include "TextFormat.tcc"