SimpleBatch.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. #include "SimpleBatch.h"
  19. char RBatch::ms_Error[256];
  20. // returns the number of tokens found, or -1 for EOF
  21. short RBatch::GetLine()
  22. {
  23. m_sNumTokens = 0;
  24. short i;
  25. for (i=0; i < SB_MAX_TOKENS; i++)
  26. {
  27. m_pszTokenList[i][0] = '\0';
  28. m_sLinePos[i] = 0;
  29. }
  30. if (m_fp == NULL)
  31. {
  32. TRACE("RBatch: File not open.\n");
  33. return -1;
  34. }
  35. int iChar;
  36. char c;
  37. short sLinePos = 0;
  38. short sTokenChar = 0;
  39. short sLoop = TRUE;
  40. short sRet = 0;
  41. short sMidToken = FALSE;
  42. short sInString = FALSE;
  43. while (sLoop)
  44. {
  45. BEGIN_LOOP:
  46. if ((iChar = fgetc(m_fp))==EOF)
  47. {
  48. if (sMidToken)
  49. {
  50. m_pszTokenList[m_sNumTokens][sTokenChar] = '\0';
  51. m_sNumTokens++;
  52. sMidToken = FALSE;
  53. sTokenChar = 0;
  54. }
  55. m_fp = NULL;
  56. sRet = -1;
  57. sLoop = FALSE;
  58. break;
  59. }
  60. c = (char) iChar;
  61. sLinePos++;
  62. // 1) check for end of line:
  63. if ( (c == '\n') || (c == '\r'))
  64. {
  65. m_lCurrentLine++;
  66. if (m_sCharEOL == 2) // ASSUME another follows
  67. fgetc(m_fp);
  68. if (sMidToken)
  69. {
  70. m_pszTokenList[m_sNumTokens][sTokenChar] = '\0';
  71. m_sNumTokens++;
  72. sMidToken = FALSE;
  73. sTokenChar = 0;
  74. }
  75. sLoop = FALSE;
  76. break;
  77. }
  78. if (!sInString)
  79. {
  80. // 2) Check for special characters:
  81. if (m_pszSpecialCharacters)
  82. for (i=0;i < strlen(m_pszSpecialCharacters);i++)
  83. {
  84. if (c == m_pszSpecialCharacters[i]) // found it!
  85. {
  86. // insert a special character
  87. if (sMidToken)
  88. {
  89. m_pszTokenList[m_sNumTokens][sTokenChar] = '\0';
  90. m_sNumTokens++;
  91. sTokenChar = 0;
  92. sMidToken = FALSE;
  93. }
  94. m_sLinePos[m_sNumTokens] = sLinePos;
  95. m_pszTokenList[m_sNumTokens][0] = c;
  96. m_pszTokenList[m_sNumTokens][1] = '\0';
  97. m_sNumTokens++;
  98. goto BEGIN_LOOP;
  99. }
  100. }
  101. // 3) Check for a filtered character
  102. if (((short)c < m_sLowFilter) || ( (short)c > m_sHighFilter))
  103. {
  104. if (sMidToken)
  105. {
  106. m_pszTokenList[m_sNumTokens][sTokenChar] = '\0';
  107. m_sNumTokens++;
  108. sTokenChar = 0;
  109. sMidToken = FALSE;
  110. }
  111. continue; // KEEP SCANNING!
  112. }
  113. // 4) Check for separator
  114. for (i=0;i < strlen(m_pszSeparators);i++)
  115. {
  116. if (c == m_pszSeparators[i]) // found it!
  117. {
  118. if (sMidToken)
  119. {
  120. m_pszTokenList[m_sNumTokens][sTokenChar] = '\0';
  121. m_sNumTokens++;
  122. sTokenChar = 0;
  123. sMidToken = FALSE;
  124. }
  125. goto BEGIN_LOOP; // KEEP SCANNING!
  126. }
  127. }
  128. }
  129. // Check for string changes....
  130. if (c == m_cStringContext)
  131. if (sInString) // end the string
  132. {
  133. m_pszTokenList[m_sNumTokens][sTokenChar] = '\0';
  134. m_sNumTokens++;
  135. sTokenChar = 0;
  136. sMidToken = FALSE;
  137. sInString = FALSE;
  138. continue; // KEEP SCANNING!
  139. }
  140. else // begin a string context!
  141. {
  142. sInString = TRUE;
  143. sMidToken = TRUE;
  144. m_sLinePos[m_sNumTokens] = sLinePos;
  145. continue; // KEEP SCANNING!
  146. }
  147. // 5) Add to token
  148. if (sMidToken) // continue to add onto existing token
  149. {
  150. m_pszTokenList[m_sNumTokens][sTokenChar++] = c;
  151. continue; // KEEP SCANNING!
  152. }
  153. //************************************* START A TOKEN
  154. else // start a new token
  155. {
  156. sMidToken = TRUE;
  157. m_sLinePos[m_sNumTokens] = sLinePos;
  158. m_pszTokenList[m_sNumTokens][sTokenChar++] = c;
  159. }
  160. // KEEP SCANNING!
  161. }
  162. if (sRet != -1)
  163. {
  164. return m_sNumTokens;
  165. }
  166. else
  167. return -1;
  168. }
  169. char* RBatch::CreateError(short sToken)
  170. {
  171. if (m_fp == NULL)
  172. {
  173. sprintf(ms_Error,"RBatch(%s):\n* Premature end of file!\n",m_pszFileName);
  174. return ms_Error;
  175. }
  176. char temp[256] = "\0";
  177. if ((sToken != -1) && (sToken < m_sNumTokens))
  178. {
  179. sprintf(temp,
  180. "Unexpected token \"%s\" found at column %hd\n",
  181. m_pszTokenList[sToken],m_sLinePos[sToken]);
  182. }
  183. sprintf(ms_Error,"RBatch(%s):\n* Parse error at line %ld\n* %s",m_pszFileName,
  184. m_lCurrentLine,temp);
  185. return ms_Error;
  186. }
  187. // Use a command syntax that is completely not line based:
  188. // Returns NULL if EOF
  189. // MUST ADVANCE WHEN CALLED AND SIT THERE!!!!!
  190. //
  191. char* RBatch::NextToken()
  192. {
  193. if (m_sCurToken == -2) // first time:
  194. {
  195. if (GetLine() == short(-1))
  196. {
  197. return NULL;
  198. }
  199. m_sCurToken = -1;
  200. }
  201. search:
  202. m_sCurToken++; // move to next token!
  203. while (m_sCurToken >= m_sNumTokens)
  204. {
  205. if ((GetLine() == -1) && (m_sNumTokens==0))
  206. {
  207. return NULL;
  208. }
  209. m_sCurToken = 0; // search for non null line
  210. }
  211. // comment lines
  212. if (*m_pszTokenList[m_sCurToken] == m_cComment)
  213. {
  214. m_sCurToken = m_sNumTokens;
  215. goto search;
  216. }
  217. return &m_pszTokenList[m_sCurToken][0];
  218. }