libept  0.5.25
desktopfile.h
Go to the documentation of this file.
1 
3 #include <iostream>
4 #include <string>
5 #include <map>
6 #include <stdio.h>
7 
8 #ifndef EPT_CORE_DESKTOPFILE_H
9 #define EPT_CORE_DESKTOPFILE_H
10 
11 namespace ept {
12 namespace core {
13 namespace desktop {
14 
15 struct File {
16  struct Entry {
17  std::string key;
18  std::string value;
19  };
20  typedef std::map< std::string, Entry > EntryMap;
21 
22  struct Group {
23  std::string name;
25  Entry &entry( std::string k ) { return entries[ k ]; }
26  };
27 
28  typedef std::map< std::string, Group > GroupMap;
30  Group &group( std::string k ) { return groups[ k ]; }
31 };
32 
33 inline std::istream &operator >>( std::istream &i, File::Entry &e )
34 {
35  std::string spaces = ""; char c; bool started = false;
36 
37  e.key = "";
38  // read key
39  while ( i.peek() != EOF ) {
40  c = i.get();
41  if ( !started && c == '\n' )
42  return i >> e;
43  if ( isspace( c ) ) {
44  spaces += c;
45  continue;
46  }
47  if ( !started && c == '#' ) {
48  while ( i.peek() != EOF && i.get() != '\n' )
49  ; // read till eol
50  return i >> e; // restart reading
51  }
52  started = true;
53  if ( c == '=' )
54  break;
55  e.key += spaces;
56  e.key += c;
57  spaces = "";
58  }
59  // std::cerr << "read key: " << e.key << std::endl;
60 
61  started = false;
62  bool backslash = false;
63  // read value
64  while ( i.peek() != EOF ) {
65  c = i.get();
66  if ( c == '\n' ) {
67  if ( backslash )
68  e.value += '\\';
69  return i;
70  }
71  if ( !started && isspace( c ) )
72  continue;
73  started = true;
74  if ( backslash ) { // interpret escape sequences
75  if ( c == '\\' ) e.value += '\\';
76  else if ( c == 'n' ) e.value += '\n';
77  else if ( c == 't' ) e.value += '\t';
78  else if ( c == 'r' ) e.value += '\r';
79  else if ( c == 's' ) e.value += ' ';
80  else { e.value += '\\'; e.value += c; }
81  backslash = false;
82  continue;
83  }
84  if ( c == '\\' ) {
85  backslash = true;
86  continue;
87  }
88  e.value += c;
89  }
90  return i;
91 }
92 
93 inline std::istream &operator >>( std::istream &i, File::Group &g )
94 {
95  bool started = false; char c;
96  g.name = "";
97  while ( i.peek() != EOF ) {
98  c = i.get();
99  if ( !started && isspace( c ) )
100  continue;
101  if ( !started && c == '#' ) {
102  while( i.peek() != EOF && i.get() != '\n' )
103  ; // read till eol
104  return i >> g; // restart reading
105  }
106  if ( !started && c == '[' ) {
107  started = true;
108  continue;
109  }
110  if ( started && c == ']' ) {
111  while( i.peek() != EOF && i.get() != '\n' )
112  ; // read till eol
113  break;
114  }
115  g.name += c;
116  }
117  while ( i.peek() != EOF ) {
118  File::Entry e;
119  i >> e;
120  g.entries[ e.key ] = e;
121  }
122  return i;
123 }
124 
125 inline std::istream &operator >>( std::istream &i, File &f )
126 {
127  while ( i.peek() != EOF ) {
128  File::Group g;
129  i >> g;
130  f.groups[ g.name ] = g;
131  }
132  return i;
133 }
134 
135 }
136 }
137 }
138 
139 #endif
Group & group(std::string k)
Definition: desktopfile.h:30
Definition: desktopfile.h:15
Definition: desktopfile.h:22
std::istream & operator>>(std::istream &i, Category &cat)
Definition: desktop.h:41
EntryMap entries
Definition: desktopfile.h:24
std::map< std::string, Group > GroupMap
Definition: desktopfile.h:28
Entry & entry(std::string k)
Definition: desktopfile.h:25
std::string key
Definition: desktopfile.h:17
std::string name
Definition: desktopfile.h:23
GroupMap groups
Definition: desktopfile.h:29
std::string value
Definition: desktopfile.h:18
std::map< std::string, Entry > EntryMap
Definition: desktopfile.h:20
Definition: desktopfile.h:16