* updated korganizer (21.12.1 -> 21.12.2), untested
[t2-trunk.git] / source / tag-parser.hh
blob4db117c942519b884dc48ae6d924b84f0d1d1784
1 #include <vector>
2 #include <iostream>
3 #include <fstream>
6 class Tag {
7 public:
9 Tag(const std::string& i_short_name,
10 const std::string& i_name,
11 const std::string& i_long_name)
12 : short_name(i_short_name), name(i_name),
13 long_name(i_long_name)
17 virtual ~Tag(){};
19 void Clear ()
21 value.clear();
22 ClearImpl ();
25 virtual std::istream& Read(std::istream& is)
27 return is;
29 virtual std::ostream& Write(std::ostream& os)
31 return os;
34 virtual void ClearImpl() {}
36 std::string short_name;
37 std::string name;
38 std::string long_name;
40 std::string value;
44 class TagParser
46 protected:
48 TagParser () {}
49 virtual ~TagParser () {}
51 public:
53 // erases all tag's data
54 void Clear() {
55 for (std::vector<Tag*>::size_type i = 0;
56 i < tags.size(); ++i)
57 tags[i]->Clear();
60 bool Parse(const std::string& file)
62 int error = 0;
64 // parse
65 std::ifstream src_file(file.c_str());
67 if (!src_file) {
68 std::cerr << file << ": Can not open file!" << std::endl;
69 return false;
72 std::string line;
73 std::string tag, value;
74 for (int linenr = 0; src_file; ++linenr) {
75 std::getline(src_file, line);
77 if (line.size() == 0)
78 continue;
80 // skip comments (maybe we also need to compress
81 // whitespaces? -ReneR
82 if (line[0] == '#' || line[0] == ' ')
83 continue;
85 if (line.size() < 3) {
86 ++error;
87 std::cerr << file << ":"
88 << linenr << ": only garbage in this line." << std::endl
89 << " '" << line << "'" << std::endl;
92 std::string::size_type idx = line.find(' ');
93 if (idx == std::string::npos)
94 idx = line.size();
96 if (line[0] != '[' || line[idx - 1] != ']') {
97 ++error;
98 std::cerr << file << ":" << linenr << ": this line contains no tag." << std::endl
99 << " '" << line << "'" << std::endl;
100 continue;
103 tag.erase();
104 tag.append(line, 1, idx - 2);
105 value.erase();
106 value.append(line, idx, std::string::npos);
108 // search thru "registered" tags
109 bool tag_found = false;
110 for (std::vector<Tag*>::size_type i = 0;
111 i < tags.size(); ++i) {
112 if (tag == tags[i]->short_name ||
113 tag == tags[i]->long_name ||
114 tag == tags[i]->name)
116 tags[i]->value.append (value + '\n');
117 //std::cerr << line << std::endl;
118 //std::cerr << tag << ": " << value << std::endl;
119 tag_found = true;
120 break;
124 if (!tag_found) {
125 ++error;
126 std::cerr << file << ":" << linenr << ": Unknown tag '" << tag << "'" << std::endl;
129 return error == 0;
132 // derived class has to fill this vector
133 std::vector<Tag*> tags;