PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/WishPiX/Prefs/prefline.cpp

https://bitbucket.org/rainbow21/postal
C++ | 319 lines | 203 code | 23 blank | 93 comment | 67 complexity | a11a504efceec652322fb11bbd9d07e7 MD5 | raw file
Possible License(s): GPL-2.0
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 RWS Inc, All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of version 2 of the GNU General Public License as published by
  7. // the Free Software Foundation
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. //
  18. // prefline.cpp
  19. // Project: Soon-to-be-RSPiX
  20. //
  21. // History:
  22. // 12/11/96 JPW RPrefsLine created to contain ini file lines, information
  23. // on the type of lines, and to help in processing of lines.
  24. // 12/16/96 JPW Fixed so it will work with the STL stuff that comes with
  25. // MSVC 4.1 or newer. Also fixed a few psz parameters that
  26. // should have been const's.
  27. //
  28. // 05/08/97 JMI Added conditions for compiler versions' STL
  29. // differences (namely "list" vs. "list.h").
  30. // Also, changed #include <rspix.h> to #include "RSPiX.h".
  31. //
  32. // 06/29/97 MJR Replaced STL vector with an RSP list. STL is an evil
  33. // entity that should be banished from the face of the earth.
  34. // Whoever suggested we use it should be shot. (Good thing
  35. // I'm the president -- it's against the rules to shoot me.)
  36. //
  37. // 07/10/97 MJR Removed TRACE() that occured if there was nothing following
  38. // an entry's '='.
  39. //
  40. // 08/27/97 JMI Now Var names and Section names can contain spaces (note,
  41. // though, that leading and trailing spaces are ignored for
  42. // user's convenience).
  43. //
  44. ////////////////////////////////////////////////////////////////////////////////
  45. #include <stdio.h>
  46. #include <string.h>
  47. #include <assert.h>
  48. #include <ctype.h>
  49. #include "Blue.h"
  50. #include "prefline.h"
  51. ////////////////////////////////////////////////////////////////////////////////
  52. // Handy inline to parse before and after an equals into two strings.
  53. // The whitespace directly preceding and postceding.
  54. ////////////////////////////////////////////////////////////////////////////////
  55. inline int16_t GetVar( // Returns 0 on success.
  56. char* pszLine, // In: Line to parse.
  57. char* pszVar, // Out: Var name, if not NULL.
  58. char* pszVal) // Out: Val, if not NULL.
  59. {
  60. int16_t sRes = 0; // Assume success.
  61. int16_t j, i, k;
  62. // Copy variable name to out string
  63. for (j = 0, i = 0; pszLine[i] != '\0' && pszLine[i] != '='; i++, j++)
  64. {
  65. if (pszVar)
  66. {
  67. pszVar[j] = pszLine[i];
  68. }
  69. }
  70. if (pszVar)
  71. {
  72. // Remove trailing whitespace.
  73. for (k = j - 1; isspace(pszVar[k]) && k > 0; k--) ;
  74. pszVar[k + 1] = '\0';
  75. }
  76. if (pszLine[i] == '\0')
  77. {
  78. TRACE("GetVar(): Missing '=' in line: '%s'\n", pszLine);
  79. sRes = 2;
  80. }
  81. else
  82. {
  83. if (pszVal)
  84. {
  85. // Find first non-space char after '='
  86. for (i++; pszLine[i] != '\0' && isspace(pszLine[i]); i++)
  87. ;
  88. // Did we find find anything after '='
  89. if (pszLine[i] == '\0')
  90. {
  91. // 7/10/97 MJR - Removed this TRACE() because we often use entries with nothing after the '='
  92. // TRACE("GetVar(): Badly formed variable syntax.\n");
  93. sRes = 3;
  94. }
  95. else
  96. // Copy variable value to out string
  97. strcpy(pszVal, &pszLine[i]);
  98. }
  99. }
  100. return sRes;
  101. }
  102. ////////////////////////////////////////////////////////////////////////////////
  103. // Constructor
  104. ////////////////////////////////////////////////////////////////////////////////
  105. RPrefsLine::RPrefsLine (RPrefsLine::ePrefsLineType Type, const char *pszLine)
  106. {
  107. m_Type = Type;
  108. m_pszLine = new char[strlen (pszLine) + 1];
  109. assert (m_pszLine);
  110. strcpy (m_pszLine, pszLine);
  111. return;
  112. }
  113. ////////////////////////////////////////////////////////////////////////////////
  114. // Destructor
  115. ////////////////////////////////////////////////////////////////////////////////
  116. RPrefsLine::~RPrefsLine ()
  117. {
  118. delete [] m_pszLine;
  119. m_pszLine = NULL;
  120. return;
  121. }
  122. ////////////////////////////////////////////////////////////////////////////////
  123. // Method to get a constant pointer to the Line of text.
  124. ////////////////////////////////////////////////////////////////////////////////
  125. const char* RPrefsLine::GetLine (void)
  126. {
  127. return (m_pszLine);
  128. }
  129. ////////////////////////////////////////////////////////////////////////////////
  130. // Method to get type of line.
  131. ////////////////////////////////////////////////////////////////////////////////
  132. RPrefsLine::ePrefsLineType RPrefsLine::GetType()
  133. {
  134. return (m_Type);
  135. }
  136. ////////////////////////////////////////////////////////////////////////////////
  137. // Get the section name. returns 0 on success
  138. ////////////////////////////////////////////////////////////////////////////////
  139. int16_t RPrefsLine::GetSectionName(char *pszSection)
  140. {
  141. int16_t sRes = 0;
  142. if (m_Type != Section)
  143. {
  144. TRACE("RPrefsLine::GetSectionName(): Not a section line.\n");
  145. sRes = 1;
  146. }
  147. else
  148. {
  149. int16_t i, j;
  150. // Find index of first char is section name
  151. for (i = 0; (m_pszLine[i] != '\0') &&
  152. (isspace(m_pszLine[i]) || (m_pszLine[i] == '[')); i++)
  153. ;
  154. // Make sure there even is somthing after the '[' beside space
  155. if (m_pszLine[i] == '\0')
  156. sRes = 2;
  157. else
  158. {
  159. // Copy section name to out string
  160. for (j = 0; (m_pszLine[i] != '\0') && (m_pszLine[i] != ']'); i++, j++)
  161. pszSection[j] = m_pszLine[i];
  162. j--;
  163. // Remove trailing spaces.
  164. while (isspace(pszSection[j]) )
  165. j--;
  166. pszSection[j + 1] = '\0';
  167. }
  168. }
  169. if (sRes != 0)
  170. strcpy(pszSection, "");
  171. return (sRes);
  172. }
  173. ////////////////////////////////////////////////////////////////////////////////
  174. // Get the variable name. returns 0 on success
  175. ////////////////////////////////////////////////////////////////////////////////
  176. int16_t RPrefsLine::GetVariableName(char *pszVariable)
  177. {
  178. int16_t sRes = 0;
  179. if (m_Type != Variable)
  180. {
  181. TRACE("RPrefsLine::GetVariableName(): Not a variable line.\n");
  182. sRes = 1;
  183. }
  184. else
  185. #if 0
  186. {
  187. short i, j;
  188. // Find index of first non-space char
  189. for (i = 0; m_pszLine[i] != '\0' && isspace(m_pszLine[i]); i++)
  190. ;
  191. if (m_pszLine[i] == '\0')
  192. {
  193. TRACE("RPrefsLine::GetVariableName(): Badly formed variable name.\n");
  194. sRes = 2;
  195. }
  196. else
  197. {
  198. // Copy variable name to out string
  199. for (j = 0; m_pszLine[i] != '\0' && !isspace(m_pszLine[i]) &&
  200. m_pszLine[i] != '='; i++, j++)
  201. pszVariable[j] = m_pszLine[i];
  202. pszVariable[j] = '\0';
  203. }
  204. }
  205. if (sRes != 0)
  206. strcpy(pszVariable, "");
  207. #else
  208. sRes = GetVar(m_pszLine, pszVariable, NULL);
  209. #endif
  210. return (sRes);
  211. }
  212. ////////////////////////////////////////////////////////////////////////////////
  213. // Get the value of the variable. returns 0 on success
  214. ////////////////////////////////////////////////////////////////////////////////
  215. int16_t RPrefsLine::GetVariableValue(char *pszValue)
  216. {
  217. int16_t sRes = 0;
  218. // Make sure the prefs line is a variable
  219. if (m_Type != Variable)
  220. {
  221. TRACE("RPrefsLine::GetVariableValue(): Not a variable line.\n");
  222. sRes = 1;
  223. }
  224. else
  225. #if 0
  226. {
  227. short i;
  228. // Find index of '=' char
  229. for (i = 0; m_pszLine[i] != '\0' && m_pszLine[i] != '='; i++)
  230. ;
  231. if (m_pszLine[i] == '\0')
  232. {
  233. TRACE("RPrefsLine::GetVariableName(): Missing '=' in line: '%s'\n", m_pszLine);
  234. sRes = 2;
  235. }
  236. else
  237. {
  238. // Find first non-space char after '='
  239. for (i++; m_pszLine[i] != '\0' && isspace(m_pszLine[i]); i++)
  240. ;
  241. // Did we find find anything after '='
  242. if (m_pszLine[i] == '\0')
  243. {
  244. // 7/10/97 MJR - Removed this TRACE() because we often use entries with nothing after the '='
  245. // TRACE("RPrefsLine::GetVariableName(): Badly formed variable syntax.\n");
  246. sRes = 3;
  247. }
  248. else
  249. // Copy variable value to out string
  250. strcpy(pszValue, &m_pszLine[i]);
  251. }
  252. }
  253. if (sRes != 0)
  254. strcpy(pszValue, "");
  255. #else
  256. sRes = GetVar(m_pszLine, NULL, pszValue);
  257. #endif
  258. return (sRes);
  259. }
  260. ////////////////////////////////////////////////////////////////////////////////
  261. // Set the value of the variable
  262. ////////////////////////////////////////////////////////////////////////////////
  263. int16_t RPrefsLine::SetVariableValue(const char *pszValue)
  264. {
  265. int16_t sRes = 0;
  266. char pszLine[128], pszVariable[64];
  267. ASSERT(pszValue);
  268. // Make sure the prefs line is a variable
  269. if (m_Type != Variable)
  270. {
  271. TRACE("RPrefsLine::SetVariableValue(): Not a variable line.\n");
  272. sRes = 1;
  273. }
  274. else
  275. {
  276. #if 0
  277. short i;
  278. for(i = 0; (m_pszLine[i] != '\0') && !isspace(m_pszLine[i]) &&
  279. m_pszLine[i] != '='; i++)
  280. pszVariable[i] = m_pszLine[i];
  281. pszVariable[i] = '\0';
  282. #else
  283. sRes = GetVar(m_pszLine, pszVariable, NULL);
  284. if (sRes == 0)
  285. #endif
  286. {
  287. ASSERT(m_pszLine);
  288. delete [] m_pszLine;
  289. m_pszLine = NULL;
  290. sprintf(pszLine, "%s = %s", pszVariable, pszValue);
  291. m_pszLine = new char[strlen(pszLine) + 1];
  292. strcpy(m_pszLine, pszLine);
  293. }
  294. }
  295. return (sRes);
  296. }