1 /*
2  *  JLib - Jacob's Library.
3  *  Copyright (C) 2003, 2004  Juan Carlos Seijo P�rez
4  *
5  *  This library is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Library General Public
7  *  License as published by the Free Software Foundation; either
8  *  version 2 of the License, or (at your option) any later version.
9  *
10  *  This library is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *  Library General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Library General Public
16  *  License along with this library; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  *  Juan Carlos Seijo P�rez
20  *  jacob@mainreactor.net
21  */
22 
23 /** Class to manipulate text files.
24  * @file    JTextFile.h.
25  * @author  Juan Carlos Seijo P�rez
26  * @date    15/06/2003
27  * @version 0.0.1 - 15/06/2003 - First version.
28  */
29 
30 #ifndef _JTEXTFILE_INCLUDED
31 #define _JTEXTFILE_INCLUDED
32 
33 #include <JLib/Util/JTypes.h>
34 #include <JLib/Util/JFile.h>
35 #include <stdarg.h>
36 #include <ctype.h>
37 #include <stdlib.h>
38 
39 /** Max line length.
40  */
41 #define MAX_LINE_LENGTH 1024
42 
43 /** This class allows to manipulate text files. Contains methods to load, save, write formatted strings,
44  * and search words among others.
45  */
46 class JTextFile : public JFile
47 {
48 protected:
49   s8 *ptr;                              /**< Seek pointer */
50   static s8 line;                       /**< End of line character */
51 
52 public:
53 
54   /** Creates a new file with the given name.
55    * @param  _name Name of the file.
56    */
57   JTextFile(const s8 *_name = 0);
58 
59   /** Frees allocated resources and closes the file.
60    */
61   ~JTextFile();
62 
63   /** Seeks to the first character of the next occurrence of the given string.
64    * @param  str String to look for.
65    * @param  jump Indicates whether it must jump to the beginning of the file if the end of the file is reached.
66    * @return <b>true</b> if the word was found, <b>false</b> if not.
67    */
68   bool FindNext(const s8 *str, bool jump = false);
69 
70   /** Reads the file as in the base class but assigns the seek pointer also.
71    * @param  readSize Size to read or zeor to read the whole file.
72    * @return Number of bytes read or 0 (zero) if an error occurred.
73    */
74   virtual u32 Read(u32 readSize = 0);
75 
76   /** Loads the whole file as in the base class but assigns the seek pointer.
77    * @param  filename Name of the file to load.
78    * @param  mode Open mode as in fopen() (like {w|r|a}[+][{b|t}]).
79    * by default is "rb" (read, binary).
80    * @return <b>true</b> if succeeded, <b>false</b> otherwise.
81    */
82   virtual bool Load(const char *filename, const char *mode = "rb");
83 
84 	/** Advances the seek pointer to the next word.
85    * @return <b>true</b> if another word existed, <b>false</b> if not.
86    */
87   bool SkipNextWord();
88 
89   /** Reads the next word and moves the pointer.
90    * @param  str Buffer where to put the read word.
91    * @return <b>true</b> if another word existed, <b>false</b> if not.
92    */
93   bool ReadWord(s8 *str);
94 
95   /** Reads the next double-quoted word and moves the pointer.
96    * @param  str Buffer where to put the read double-quoted word.
97    * @return <b>true</b> if another word existed, <b>false</b> if not.
98    */
99   bool ReadQuotedWord(s8 *str);
100 
101   /** Reads the next decimal number. Skips all characters that are not numbers (or the decimal dot or a sign).
102    * @param  f Variable to store the result.
103    * @return <b>true</b> if there was a number, <b>false</b> if not.
104    */
105   bool ReadFloat(float *f);
106 
107   /** Reads the next integer. Skips all characters that are not numbers (or signs).
108    * @param  i Variable to store the result.
109    * @return <b>true</b> if there was a number, <b>false</b> if not.
110    */
111   bool ReadInteger(s32 *i);
112 
113   /** Advances the pointer to the next line.
114    * @return <b>true</b> if there was next line, <b>false</b> if not.
115    */
116   bool NextLine();
117 
118   /** Moves the pointer to the start of the document.
119    */
120   void StartOfDocument();
121 
122   /** Reads the line under the actual position.
123    * @param  str Variable to store the result.
124    * @return <b>true</b> if there was next line, <b>false</b> if not.
125    */
126   bool ReadLine(s8 *str);
127 
128   /** Returns the pointer to the current position.
129    * @return Pointer to the current position.
130    */
131   s8 * GetPos();
132 
133   /** Sets the current position.
134    * @param _ptr New current position.
135    * @return <b>true</b> if the position existed, <b>false</b> if not.
136    */
137   bool SetPos(s8 *_ptr);
138 
139   /** Writes in the file without line-feed.
140    * @param  str String to write.
141    * @return Number of characters written.
142    */
143   u32 Print(const s8 *str);
144 
145   /** Writes in the file with line-feed.
146    * @param  str String to write.
147    * @return Number of characters written.
148    */
149   u32 PrintLine(const s8 *str);
150 
151   /** Writes in the file with format and without line-feed.
152    * @param  fmt Format string, printf-like.
153    * @return Number of characters written.
154    */
155   u32 Printf(const s8 *fmt, ... );
156 
157   /** Counts the number of occurrences of the given string among the file.
158    * @param  str String to search.
159    * @param  init Start position.
160    * @param  end End position.
161    * @return Number of coincidences.
162    */
163   u32 CountString(const s8 *str, s8* init = 0, s8* end = 0);
164 
165 	/** Reads like scanf.
166 	 * @param  format Format string.
167 	 * @param  format Format parameters.
168 	 * @return Number of parameters read successfuly.
169 	 */
170 	s32 Scanf(const char *format, ...);
171 };
172 
173 #endif  // _JTEXTFILE_INCLUDED
174