PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/src/parse.cpp

#
C++ | 344 lines | 313 code | 17 blank | 14 comment | 108 complexity | 2987468c62760bd79fc72911de23a699 MD5 | raw file
Possible License(s): GPL-2.0
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <sstream>
  4. #include <string>
  5. using namespace std;
  6. #include "keywords.h"
  7. #include "parseColor.h"
  8. #include "xpUtil.h"
  9. // sloppy, but it's an easy way to set the comment marker as a line
  10. // terminator
  11. static bool commentEndsLine = true;
  12. bool
  13. isDelimiter(char c)
  14. {
  15. return(c == ' ' || c == '\t');
  16. }
  17. bool
  18. isEndOfLine(char c)
  19. {
  20. // 13 is DOS end-of-line, 28 is the file separator
  21. bool returnValue = (c == '\0' || c == 13 || c == 28);
  22. if (commentEndsLine) returnValue = (c == '#' || returnValue);
  23. return(returnValue);
  24. }
  25. static void
  26. skipPastToken(int &i, const char *line, const char endChar)
  27. {
  28. while (line[i] != endChar)
  29. {
  30. if (isEndOfLine(line[i]))
  31. {
  32. ostringstream errStr;
  33. errStr << "Malformed line:\n\t" << line << "\n";
  34. xpWarn(errStr.str(), __FILE__, __LINE__);
  35. return;
  36. }
  37. i++;
  38. }
  39. }
  40. static void
  41. skipPastToken(int &i, const char *line)
  42. {
  43. while (!isDelimiter(line[i]))
  44. {
  45. if (isEndOfLine(line[i])) return;
  46. i++;
  47. }
  48. }
  49. // If the line contains 'key', return everything between 'key' and 'endChar'
  50. static bool
  51. getValue(const char *line, int &i, const char *key, const char endChar,
  52. char *&returnstring)
  53. {
  54. const unsigned int length = strlen(key);
  55. if (strncmp(line + i, key, length) == 0)
  56. {
  57. i += length;
  58. int istart = i;
  59. skipPastToken(i, line, endChar);
  60. returnstring = new char[i - istart + 1];
  61. strncpy(returnstring, (line + istart), i - istart);
  62. returnstring[i-istart] = '\0';
  63. i++;
  64. return(true);
  65. }
  66. return(false);
  67. }
  68. // If the line contains 'key', return everything between 'key' and
  69. // 'endChar' comment markers are allowed in the string. Use this for
  70. // reading marker labels.
  71. static bool
  72. getValueIncludingComment(const char *line, int &i, const char *key,
  73. const char endChar, char *&returnstring)
  74. {
  75. commentEndsLine = false;
  76. bool returnValue = getValue(line, i, key, endChar, returnstring);
  77. commentEndsLine = true;
  78. return(returnValue);
  79. }
  80. static bool
  81. getValueAfter(const char *line, int &i, const char *key, const char endChar,
  82. char *&returnstring)
  83. // If the line contains the 'key', skip past 'endChar' and return
  84. // everything to the end of the line in 'returnString'
  85. {
  86. const unsigned int length = strlen(key);
  87. if (strncmp(line + i, key, length) == 0)
  88. {
  89. i += length;
  90. skipPastToken(i, line, endChar); // Skips 'to' endChar
  91. int istart = i + 1; // Add another to not include endChar
  92. skipPastToken(i, line);
  93. returnstring = new char[i - istart + 1];
  94. strncpy(returnstring, (line + istart), i - istart);
  95. returnstring[i-istart] = '\0';
  96. i++;
  97. return(true);
  98. }
  99. return(false);
  100. }
  101. static bool
  102. getValue(const char *line, int &i, const char *key, char *&returnstring)
  103. {
  104. const unsigned int length = strlen(key);
  105. if (strncmp(line + i, key, length) == 0)
  106. {
  107. i += length;
  108. int istart = i;
  109. skipPastToken(i, line);
  110. returnstring = new char[i - istart + 1];
  111. strncpy(returnstring, (line + istart), i - istart);
  112. returnstring[i-istart] = '\0';
  113. i++;
  114. return(true);
  115. }
  116. return(false);
  117. }
  118. // This routine returns the next token in the line and its type.
  119. int
  120. parse(int &i, const char *line, char *&returnString)
  121. {
  122. if (i >= (int) strlen(line)) return(ENDOFLINE);
  123. if (returnString != NULL)
  124. xpWarn("returnString is not NULL!\n", __FILE__, __LINE__);
  125. int returnVal = UNKNOWN;
  126. if (isDelimiter(line[i]))
  127. {
  128. i++;
  129. returnVal = DELIMITER;
  130. }
  131. else if (isEndOfLine(line[i]))
  132. returnVal = ENDOFLINE;
  133. else if (getValue(line, i, "align=", returnString))
  134. returnVal = ALIGN;
  135. else if (getValue(line, i, "arc_color={", '}', returnString))
  136. returnVal = ARC_COLOR;
  137. else if (getValue(line, i, "arc_color=", returnString))
  138. {
  139. unsigned char RGB[3];
  140. parseColor(returnString, RGB);
  141. delete [] returnString;
  142. returnString = new char[32];
  143. snprintf(returnString, 32, "%d,%d,%d", RGB[0], RGB[1], RGB[2]);
  144. returnVal = ARC_COLOR;
  145. }
  146. else if (getValue(line, i, "arc_thickness=", returnString))
  147. returnVal = THICKNESS;
  148. // This line must be after all other "arc_" keywords, because it
  149. // gobbles all forms
  150. else if (getValueAfter(line, i, "arc_", '=', returnString))
  151. returnVal = ARC_FILE;
  152. else if (getValue(line, i, "[", ']', returnString))
  153. returnVal = BODY;
  154. else if (getValue(line, i, "altcirc=", returnString))
  155. returnVal = CIRCLE;
  156. else if (getValue(line, i, "circle=", returnString))
  157. returnVal = CIRCLE;
  158. else if (getValue(line, i, "bump_map=", returnString))
  159. returnVal = BUMP_MAP;
  160. else if (getValue(line, i, "bump_scale=", returnString))
  161. returnVal = BUMP_SCALE;
  162. else if (getValue(line, i, "bump_shade=", returnString))
  163. returnVal = BUMP_SHADE;
  164. else if (getValue(line, i, "cloud_gamma=", returnString))
  165. returnVal = CLOUD_GAMMA;
  166. else if (getValue(line, i, "cloud_map=", returnString))
  167. returnVal = CLOUD_MAP;
  168. else if (getValue(line, i, "cloud_ssec=", returnString))
  169. returnVal = CLOUD_SSEC;
  170. else if (getValue(line, i, "cloud_threshold=", returnString))
  171. returnVal = CLOUD_THRESHOLD;
  172. else if (getValue(line, i, "color={", '}', returnString))
  173. returnVal = COLOR;
  174. else if (getValue(line, i, "color=", returnString))
  175. {
  176. unsigned char RGB[3];
  177. parseColor(returnString, RGB);
  178. delete [] returnString;
  179. returnString = new char[32];
  180. snprintf(returnString, 32, "%d,%d,%d", RGB[0], RGB[1], RGB[2]);
  181. returnVal = COLOR;
  182. }
  183. else if (getValue(line, i, "draw_orbit=", returnString))
  184. returnVal = DRAW_ORBIT;
  185. else if (getValue(line, i, "font=", returnString))
  186. returnVal = FONT;
  187. else if (getValue(line, i, "fontsize=", returnString))
  188. returnVal = FONTSIZE;
  189. else if (getValue(line, i, "grid=", returnString))
  190. returnVal = GRID;
  191. else if (getValue(line, i, "grid1=", returnString))
  192. returnVal = GRID1;
  193. else if (getValue(line, i, "grid2=", returnString))
  194. returnVal = GRID2;
  195. else if (getValue(line, i, "grid_color=", returnString))
  196. returnVal = GRID_COLOR;
  197. else if (getValue(line, i, "image=", returnString))
  198. returnVal = IMAGE;
  199. else if (getValue(line, i, "lang=", returnString))
  200. returnVal = LANGUAGE;
  201. else if (getValue(line, i, "magnify=", returnString))
  202. returnVal = MAGNIFY;
  203. else if (getValue(line, i, "mapbounds={", '}', returnString))
  204. returnVal = MAP_BOUNDS;
  205. else if (getValue(line, i, "marker_color={", '}', returnString))
  206. returnVal = MARKER_COLOR;
  207. else if (getValue(line, i, "marker_color=", returnString))
  208. {
  209. unsigned char RGB[3];
  210. parseColor(returnString, RGB);
  211. delete [] returnString;
  212. returnString = new char[32];
  213. snprintf(returnString, 32, "%d,%d,%d", RGB[0], RGB[1], RGB[2]);
  214. returnVal = MARKER_COLOR;
  215. }
  216. else if (getValue(line, i, "marker_font=", returnString))
  217. returnVal = MARKER_FONT;
  218. else if (getValue(line, i, "marker_fontsize=", returnString))
  219. returnVal = MARKER_FONTSIZE;
  220. // This line must be after all other "marker_" keywords, because
  221. // it gobbles all forms
  222. else if (getValueAfter(line, i, "marker_", '=', returnString))
  223. returnVal = MARKER_FILE;
  224. else if (getValue(line, i, "map=", returnString))
  225. returnVal = DAY_MAP;
  226. else if (getValue(line, i, "max_radius_for_label=", returnString))
  227. returnVal = MAX_RAD_FOR_LABEL;
  228. else if (getValue(line, i, "min_radius_for_label=", returnString))
  229. returnVal = MIN_RAD_FOR_LABEL;
  230. else if (getValue(line, i, "min_radius_for_markers=", returnString))
  231. returnVal = MIN_RAD_FOR_MARKERS;
  232. else if (getValue(line, i, "max_radius=", returnString))
  233. returnVal = MAX_RAD_FOR_MARKERS;
  234. else if (getValue(line, i, "min_radius=", returnString))
  235. returnVal = MIN_RAD_FOR_MARKERS;
  236. else if (getValueIncludingComment(line, i, "\"", '"', returnString))
  237. returnVal = NAME;
  238. else if (getValueIncludingComment(line, i, "{", '}', returnString))
  239. returnVal = NAME;
  240. else if (getValue(line, i, "night_map=", returnString))
  241. returnVal = NIGHT_MAP;
  242. else if (getValue(line, i, "orbit={", '}', returnString))
  243. returnVal = ORBIT;
  244. else if (getValue(line, i, "orbit_color={", '}', returnString))
  245. returnVal = ORBIT_COLOR;
  246. else if (getValue(line, i, "orbit_color=", returnString))
  247. {
  248. unsigned char RGB[3];
  249. parseColor(returnString, RGB);
  250. delete [] returnString;
  251. returnString = new char[32];
  252. snprintf(returnString, 32, "%d,%d,%d", RGB[0], RGB[1], RGB[2]);
  253. returnVal = ORBIT_COLOR;
  254. }
  255. else if (getValue(line, i, "relative_to=", returnString))
  256. returnVal = ORIGIN;
  257. else if (getValue(line, i, "opacity=", returnString))
  258. returnVal = OPACITY;
  259. else if (getValue(line, i, "outlined=", returnString))
  260. returnVal = OUTLINED;
  261. else if (getValue(line, i, "position=", returnString))
  262. returnVal = POSITION;
  263. else if (getValue(line, i, "radius=", returnString))
  264. returnVal = RADIUS;
  265. else if (getValue(line, i, "random_origin=", returnString))
  266. returnVal = RANDOM_ORIGIN;
  267. else if (getValue(line, i, "random_target=", returnString))
  268. returnVal = RANDOM_TARGET;
  269. else if (getValue(line, i, "rayleigh_emission_weight=", returnString))
  270. returnVal = RAYLEIGH_EMISSION_WEIGHT;
  271. else if (getValue(line, i, "rayleigh_file=", returnString))
  272. returnVal = RAYLEIGH_FILE;
  273. else if (getValue(line, i, "rayleigh_limb_scale=", returnString))
  274. returnVal = RAYLEIGH_LIMB_SCALE;
  275. else if (getValue(line, i, "rayleigh_scale=", returnString))
  276. returnVal = RAYLEIGH_SCALE;
  277. // Any 'new' satellite_* tokens must be before this, because it
  278. // gobbles all forms
  279. else if (getValueAfter(line, i, "satellite_", '=', returnString))
  280. returnVal = SATELLITE_FILE;
  281. else if (getValue(line, i, "shade=", returnString))
  282. returnVal = SHADE;
  283. else if (getValue(line, i, "spacing=", returnString))
  284. returnVal = SPACING;
  285. else if (getValue(line, i, "specular_map=", returnString))
  286. returnVal = SPECULAR_MAP;
  287. else if (getValue(line, i, "symbolsize=", returnString))
  288. returnVal = SYMBOLSIZE;
  289. else if (getValue(line, i, "text_color={", '}', returnString))
  290. returnVal = TEXT_COLOR;
  291. else if (getValue(line, i, "thickness=", returnString))
  292. returnVal = THICKNESS;
  293. else if (strncmp(line+i, "timezone=", 9) == 0)
  294. {
  295. i += 9;
  296. int istart = i;
  297. while (line[i] == '/' || line[i] == ',' || !isDelimiter(line[i]))
  298. {
  299. if (isEndOfLine(line[i])) break;
  300. i++;
  301. }
  302. returnString = new char[i - istart + 1];
  303. strncpy(returnString, (line + istart), i - istart);
  304. returnString[i-istart] = '\0';
  305. returnVal = TIMEZONE;
  306. }
  307. else if (getValue(line, i, "trail={", '}', returnString))
  308. returnVal = TRAIL;
  309. else if (getValue(line, i, "trail_output=", returnString))
  310. returnVal = OUTPUT;
  311. else if (getValue(line, i, "transparent={", '}', returnString))
  312. returnVal = TRANSPARENT;
  313. else if (getValue(line, i, "twilight=", returnString))
  314. returnVal = TWILIGHT;
  315. else // assume it's a latitude/longitude value
  316. {
  317. int istart = i;
  318. skipPastToken(i, line);
  319. returnString = new char[i - istart + 1];
  320. strncpy(returnString, (line + istart), i - istart);
  321. returnString[i-istart] = '\0';
  322. returnVal = LATLON;
  323. }
  324. return(returnVal);
  325. }