JNR
laFileParser.cpp
1 /*
2 
3 Jump'n'Run Engine
4 http://www.atanaslaskov.com/jnr/
5 
6 BSD LICENSE
7 Copyright (c) 2007-2013, Atanas Laskov
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12 1. Redistributions of source code must retain the above copyright notice,
13 this list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright notice,
15 this list of conditions and the following disclaimer in the documentation
16 and/or other materials provided with the distribution.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 */
29 
30 //
31 // FILE: laFileParser.cpp
32 // Basic file parser
33 //
34 // Copyright (C) 2007-2013 Atanas Laskov, <latanas@gmail.com>
35 //
36 #include "stdafx.h"
37 #include "laParser.h"
38 #include "Core.h"
39 
40 laFileParser::laFileParser(void)
41 {
42  _strFileName[0]=0;
43  _pFile = NULL;
44 
45  strcpy(_strSeparators, " ");
46 
47  _cTextSep[0] = M_DTEXT;
48  _cTextSep[1] = M_DTEXT;
49 
50  _cSectSep[0] = M_DSECTION_1;
51  _cSectSep[1] = M_DSECTION_2;
52 
53  _bHaltOnErrors = M_TRUE;
54 }
55 
56 laFileParser::laFileParser(char* strFile)
57 {
58  _strFileName[0]=0;
59  _pFile = NULL;
60 
61  strcpy(_strSeparators, " ");
62 
63  _cTextSep[0] = M_DTEXT;
64  _cTextSep[1] = M_DTEXT;
65 
66  _cSectSep[0] = M_DSECTION_1;
67  _cSectSep[1] = M_DSECTION_2;
68 
69  _bHaltOnErrors = M_TRUE;
70 
71  fileOpen(strFile);
72 }
73 
74 laFileParser::~laFileParser(void)
75 {
76  if(_pFile) fileClose();
77 }
78 
79 FILE* laFileParser::fileOpen(char* strFile)
80 {
81  _pFile = fopen(strFile, "r");
82 
83  ASSERT(_pFile, "Unable to open file '%s'", strFile);
84  strcpy(_strFileName, strFile);
85 
86  return _pFile;
87 }
88 
89 void laFileParser::fileClose()
90 {
91  ASSERT(_pFile, "File is not open.");
92  fclose(_pFile);
93 }
94 
95 void laFileParser::readLabel(char *buf)
96 {
97  ASSERT(_pFile, "File is not open.");
98 
99  char *buffer;
100  M_BOOL internal_buf = M_FALSE;
101  char c;
102  unsigned i=0;
103 
104  if(!buf){
105  buffer = new char[128];
106  internal_buf = M_TRUE;
107  }else buffer = buf;
108 
109  if(feof(_pFile))
110  {
111  buf[0]='\0';
112  throw laError("laFileParser::readLabel() failed");
113  }
114 
115  while( (c=getc(_pFile))==' ' );
116  buffer[i++] = c;
117 
118  while( (c=getc(_pFile))!=M_DLABEL )
119  {
120  if(feof(_pFile))break;
121 
122  buffer[i++] = c;
123  }
124  buffer[i] = '\0';
125 
126  if(internal_buf) delete [] buffer;
127 }
128 
129 void laFileParser::readInt(int *var, M_BOOL labeled)
130 {
131  ASSERT(_pFile, "File is not open.");
132 
133  char strFormat[128] = "%d";
134  strcat(strFormat, _strSeparators);
135 
136  if(labeled) readLabel();
137  fscanf(_pFile, strFormat, var);
138 }
139 
140 void laFileParser::readUnsigned(unsigned *var, M_BOOL labeled)
141 {
142  ASSERT(_pFile, "File is not open.");
143 
144  char strFormat[128] = "%u";
145  strcat(strFormat, _strSeparators);
146 
147  if(labeled) readLabel();
148  fscanf(_pFile, strFormat, var);
149 }
150 
151 void laFileParser::readDouble(double *var, M_BOOL labeled)
152 {
153  ASSERT(_pFile, "File is not open.");
154 
155  char strFormat[128] = "%f";
156  strcat(strFormat, _strSeparators);
157 
158  if(labeled) readLabel();
159  float f; //TODO: fix
160  fscanf(_pFile, strFormat, &f);
161  *var = (double)f;
162 }
163 
164 void laFileParser::readText(char *var, M_BOOL labeled)
165 {
166  ASSERT(_pFile, "File is not open.");
167 
168  char c;
169  unsigned i=0;
170 
171  while( getc(_pFile)!=_cTextSep[0] )
172  {
173  if(feof(_pFile))
174  {
175  var[0]='\0';
176  throw laError("laFileParser::readText() failed");
177  }
178  }
179 
180  while( (c=getc(_pFile))!=_cTextSep[1] )
181  {
182  if(feof(_pFile))break;
183 
184  var[i++] = c;
185  }
186  var[i] = '\0';
187 }
188 
189 void laFileParser::readSectionSeparator(char *buf/*, char sep_a, char sep_b*/)
190 {
191  ASSERT(_pFile, "File is not open.");
192 
193  char ts[2];
194  ts[0] = _cTextSep[0];
195  ts[1] = _cTextSep[1];
196  _cTextSep[0] = _cSectSep[0];//sep_a;
197  _cTextSep[1] = _cSectSep[1];//sep_b;
198 
199  if(buf==NULL)
200  {
201  char *my_buff = new char[128];
202  readText(my_buff, M_FALSE);
203  delete my_buff;
204  }
205  else readText(buf, M_FALSE);
206 
207  _cTextSep[0] = ts[0];
208  _cTextSep[1] = ts[1];
209 }
210 
211 void laFileParser::readBool(M_BOOL *var, M_BOOL labeled)
212 {
213  ASSERT(_pFile, "File is not open.");
214 
215  char c;
216  unsigned i=0;
217  char txt[32];
218 
219  while( getc(_pFile)!=M_DTEXT );
220 
221  while( (c=getc(_pFile))!=M_DTEXT )
222  {
223  txt[i++] = c;
224  }
225  txt[i] = '\0';
226 
227  if(!strcmp(txt, "true")) *var = M_TRUE;
228  else *var = M_FALSE;
229 }
230 
231 void laFileParser::readObj(laLoadableObj *obj, M_BOOL labeled)
232 {
233  ASSERT(_pFile, "File is not open.");
234 
235  if(labeled) readLabel();
236 
237  obj->load(this);
238 }
Interface for loadable objects.
Definition: laLoadableObj.h:43
Error handling class.
Definition: laError.h:46