libept
0.5.25
|
00001 00002 00003 #include <iostream> 00004 #include <string> 00005 #include <map> 00006 #include <stdio.h> 00007 00008 #ifndef EPT_CORE_DESKTOPFILE_H 00009 #define EPT_CORE_DESKTOPFILE_H 00010 00011 namespace ept { 00012 namespace core { 00013 namespace desktop { 00014 00015 struct File { 00016 struct Entry { 00017 std::string key; 00018 std::string value; 00019 }; 00020 typedef std::map< std::string, Entry > EntryMap; 00021 00022 struct Group { 00023 std::string name; 00024 EntryMap entries; 00025 Entry &entry( std::string k ) { return entries[ k ]; } 00026 }; 00027 00028 typedef std::map< std::string, Group > GroupMap; 00029 GroupMap groups; 00030 Group &group( std::string k ) { return groups[ k ]; } 00031 }; 00032 00033 inline std::istream &operator >>( std::istream &i, File::Entry &e ) 00034 { 00035 std::string spaces = ""; char c; bool started = false; 00036 00037 e.key = ""; 00038 // read key 00039 while ( i.peek() != EOF ) { 00040 c = i.get(); 00041 if ( !started && c == '\n' ) 00042 return i >> e; 00043 if ( isspace( c ) ) { 00044 spaces += c; 00045 continue; 00046 } 00047 if ( !started && c == '#' ) { 00048 while ( i.peek() != EOF && i.get() != '\n' ) 00049 ; // read till eol 00050 return i >> e; // restart reading 00051 } 00052 started = true; 00053 if ( c == '=' ) 00054 break; 00055 e.key += spaces; 00056 e.key += c; 00057 spaces = ""; 00058 } 00059 // std::cerr << "read key: " << e.key << std::endl; 00060 00061 started = false; 00062 bool backslash = false; 00063 // read value 00064 while ( i.peek() != EOF ) { 00065 c = i.get(); 00066 if ( c == '\n' ) { 00067 if ( backslash ) 00068 e.value += '\\'; 00069 return i; 00070 } 00071 if ( !started && isspace( c ) ) 00072 continue; 00073 started = true; 00074 if ( backslash ) { // interpret escape sequences 00075 if ( c == '\\' ) e.value += '\\'; 00076 else if ( c == 'n' ) e.value += '\n'; 00077 else if ( c == 't' ) e.value += '\t'; 00078 else if ( c == 'r' ) e.value += '\r'; 00079 else if ( c == 's' ) e.value += ' '; 00080 else { e.value += '\\'; e.value += c; } 00081 backslash = false; 00082 continue; 00083 } 00084 if ( c == '\\' ) { 00085 backslash = true; 00086 continue; 00087 } 00088 e.value += c; 00089 } 00090 return i; 00091 } 00092 00093 inline std::istream &operator >>( std::istream &i, File::Group &g ) 00094 { 00095 bool started = false; char c; 00096 g.name = ""; 00097 while ( i.peek() != EOF ) { 00098 c = i.get(); 00099 if ( !started && isspace( c ) ) 00100 continue; 00101 if ( !started && c == '#' ) { 00102 while( i.peek() != EOF && i.get() != '\n' ) 00103 ; // read till eol 00104 return i >> g; // restart reading 00105 } 00106 if ( !started && c == '[' ) { 00107 started = true; 00108 continue; 00109 } 00110 if ( started && c == ']' ) { 00111 while( i.peek() != EOF && i.get() != '\n' ) 00112 ; // read till eol 00113 break; 00114 } 00115 g.name += c; 00116 } 00117 while ( i.peek() != EOF ) { 00118 File::Entry e; 00119 i >> e; 00120 g.entries[ e.key ] = e; 00121 } 00122 return i; 00123 } 00124 00125 inline std::istream &operator >>( std::istream &i, File &f ) 00126 { 00127 while ( i.peek() != EOF ) { 00128 File::Group g; 00129 i >> g; 00130 f.groups[ g.name ] = g; 00131 } 00132 return i; 00133 } 00134 00135 } 00136 } 00137 } 00138 00139 #endif