SimpleBatch.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. #ifndef SIMPLE_BATCH_H
  19. #define SIMPLE_BATCH_H
  20. //=====================
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include "Blue.h"
  24. // TO DO!
  25. // REPLACE THIS WITH LINKED LISTS or some dynamic format!
  26. // Add in a high level control for #define and #include
  27. // Add macros for combining tokens into one.
  28. #define SB_MAX_TOKENS 20
  29. #define SB_MAX_TOKEN_SIZE 256
  30. #define SB_MAX_CHAR_LIST 10
  31. // Designed ony for ASCII style input files
  32. // It will normally ignore control characters...
  33. //
  34. class RBatch
  35. {
  36. //---------------- USER MEMBERS -------------
  37. public:
  38. FILE* m_fp;
  39. long m_lCurrentLine;
  40. short m_sNumTokens;
  41. char m_pszTokenList[SB_MAX_TOKENS][SB_MAX_TOKEN_SIZE];
  42. short m_sLinePos[SB_MAX_TOKENS];
  43. short m_sCurToken; // THIS IS ONLY FOR USER TO USE => Has no meaning!
  44. char m_pszFileName[128]; // For user convenience
  45. //----------- CONFIGURATION MEMBERS -------------
  46. char m_pszSeparators[SB_MAX_CHAR_LIST];
  47. char m_pszSpecialCharacters[SB_MAX_CHAR_LIST];
  48. short m_sCharEOL; // 2 for \n\r, else 1
  49. short m_sLowFilter;
  50. short m_sHighFilter;
  51. char m_cStringContext;
  52. char m_cComment;
  53. //--------------- CONSTRUCTION -------------
  54. void clear() // NOT A RESET! Will hose memory!
  55. {
  56. m_fp = NULL;
  57. m_sNumTokens = m_sCharEOL = m_sLowFilter = m_sHighFilter = 0;
  58. m_pszSeparators[0] = '\0';
  59. m_pszFileName[0] = '\0';
  60. m_pszSpecialCharacters[0] = '\0';
  61. m_lCurrentLine = 0;
  62. for (short i=0; i < SB_MAX_TOKENS; i++)
  63. {
  64. m_pszTokenList[i][0] = '\0';
  65. m_sLinePos[i] = 0;
  66. }
  67. m_sCurToken = -2; // for user convenience!
  68. }
  69. void init(char* pszFileName,char* pszSeparators = " \t,",char* pszSpecialCharacters=NULL,
  70. char cString = '"',char cComment = '*',short sCharEOL=1,short sLowFilter=32,short sHighFilter=128)
  71. {
  72. clear();
  73. if ((m_fp = fopen(pszFileName,"r")) == NULL)
  74. {
  75. TRACE("RBatch: file %s not found.\n",pszFileName);
  76. return;
  77. }
  78. strcpy(m_pszFileName,pszFileName);
  79. m_sCharEOL = sCharEOL;
  80. m_sLowFilter = sLowFilter;
  81. m_sHighFilter = sHighFilter;
  82. m_cStringContext = cString;
  83. m_cComment = cComment;
  84. if (pszSpecialCharacters) strcpy(m_pszSpecialCharacters,pszSpecialCharacters);
  85. if (pszSeparators) strcpy(m_pszSeparators,pszSeparators);
  86. }
  87. void configure(char* pszSeparators = " \t,",char* pszSpecialCharacters=NULL,
  88. char cString = '"',char cComment = '*',short sCharEOL=1,short sLowFilter=32,short sHighFilter=128)
  89. {
  90. m_sCharEOL = sCharEOL;
  91. m_sLowFilter = sLowFilter;
  92. m_sHighFilter = sHighFilter;
  93. m_cStringContext = cString;
  94. m_cComment = cComment;
  95. if (pszSpecialCharacters) strcpy(m_pszSpecialCharacters,pszSpecialCharacters);
  96. if (m_pszSeparators) strcpy(m_pszSeparators,pszSeparators);
  97. }
  98. RBatch() // default, no file
  99. {
  100. clear();
  101. configure();
  102. }
  103. RBatch(char* pszFileName,char* pszSeparators = " \t,",char* pszSpecialCharacters = NULL,
  104. char cString = '"',char cComment = '*',short sCharEOL=1,short sLowFilter=32,short sHighFilter=128)
  105. {
  106. init(pszFileName,pszSeparators,pszSpecialCharacters,cString,cComment,sCharEOL,sLowFilter,sHighFilter);
  107. }
  108. short open(char* pszFileName)
  109. {
  110. if (m_fp == NULL)
  111. {
  112. if ((m_fp = fopen(pszFileName,"r")) == NULL)
  113. {
  114. TRACE("RBatch: file %s not found.\n",pszFileName);
  115. return -1;
  116. }
  117. strcpy(m_pszFileName,pszFileName);
  118. m_lCurrentLine = 0;
  119. m_sNumTokens = 0;
  120. return 0;
  121. }
  122. else
  123. {
  124. TRACE("RBatch: file already open!\n");
  125. return -1;
  126. }
  127. }
  128. short close()
  129. {
  130. if (m_fp != NULL)
  131. {
  132. fclose(m_fp);
  133. m_fp = NULL;
  134. return 0;
  135. }
  136. else
  137. {
  138. TRACE("RBatch: File already closed.\n");
  139. return -1;
  140. }
  141. }
  142. ~RBatch()
  143. {
  144. if (m_fp) fclose(m_fp);
  145. //if (m_pszSeparators) free(m_pszSeparators);
  146. //if (m_pszSpecialCharacters) free (m_pszSpecialCharacters);
  147. /*
  148. for (short i=0;i<SB_MAX_TOKENS;i++)
  149. {
  150. if (m_pszTokenList[i]) free(m_pszTokenList[i]);
  151. }
  152. */
  153. clear();
  154. }
  155. //--------------- STATIC SPACE -------------
  156. static char ms_Error[256];
  157. //--------------- MEMBER FUNCTIONS -------------
  158. // returns the number of tokens found, or -1 for EOF
  159. short GetLine();
  160. // If you care nothing of the concept of file lines except for
  161. // reporting errors, you may take advantage of this function:
  162. char* NextToken();
  163. // creates an error message
  164. char* CreateError(short sToken = -1);
  165. };
  166. //=====================
  167. #endif