GraphChi  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Macros
configfile.hpp
Go to the documentation of this file.
1 
2 
30 #ifndef GRAPHCHI_CONFIGFILE_DEF
31 #define GRAPHCHI_CONFIGFILE_DEF
32 
33 #include <iostream>
34 #include <cstdio>
35 #include <string>
36 #include <map>
37 #include <assert.h>
38 
39 namespace graphchi {
40 
41  // Code for trimming strings copied from + modified
42  // http://stackoverflow.com/questions/479080/trim-is-not-part-of-the-standard-c-c-library
43  const std::string whiteSpaces( " \f\n\r\t\v" );
44 
45 
46  static void trimRight( std::string &str,
47  const std::string& trimChars )
48  {
49  std::string::size_type pos = str.find_last_not_of( trimChars );
50  str.erase( pos + 1 );
51  }
52 
53 
54  static void trimLeft( std::string &str,
55  const std::string& trimChars )
56  {
57  std::string::size_type pos = str.find_first_not_of( trimChars );
58  str.erase( 0, pos );
59  }
60 
61 
62  static std::string trim( std::string str)
63  {
64  std::string trimChars = " \f\n\r\t\v";
65  trimRight( str, trimChars );
66  trimLeft( str, trimChars );
67  return str;
68  }
69 
70 
71  // Removes \n from the end of line
72  static void _FIXLINE(char * s) {
73  int len = (int)strlen(s)-1;
74  if(s[len] == '\n') s[len] = 0;
75  }
76 
82  static std::map<std::string, std::string> loadconfig(std::string filename) {
83  FILE * f = fopen(filename.c_str(), "r");
84  if (f == NULL) {
85  std::cout << "ERROR: Could not read configuration file: " << filename << std::endl;
86  std::cout << "Please define environment variable GRAPHCHI_ROOT or run the program from that directory." << std::endl;
87  assert(f != NULL);
88  }
89 
90  char s[4096];
91  std::map<std::string, std::string> conf;
92 
93  // I like C parsing more than C++, that is why this is such a mess
94  while(fgets(s, 4096, f) != NULL) {
95  _FIXLINE(s);
96  if (s[0] == '#') continue; // Comment
97  if (s[0] == '%') continue; // Comment
98 
99  char delims[] = "=";
100  char * t;
101  t = strtok(s, delims);
102  const char * ckey = t;
103  t = strtok(NULL, delims);
104  const char * cval = t;
105 
106  if (ckey != NULL && cval != NULL) {
107  std::string key = trim(std::string(ckey));
108  std::string val = trim(std::string(cval));
109  conf[key] = val;
110  }
111  }
112 
113 
114  return conf;
115  }
116 
117 };
118 
119 
120 #endif
121