4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "stringfile.h"
27 StringFile::StringFile(size_t length)
32 this->length = 100000;
36 this->length = length;
38 string = new char[this->length + 1];
39 available = this->length;
42 StringFile::StringFile(const char *filename)
44 FILE *in = fopen(filename, "rb");
47 fseek(in, 0, SEEK_END);
50 fseek(in, 0, SEEK_SET);
51 string = new char[length + 5];
53 (void)fread(string, length, 1, in);
54 for(int i = 0; i < 5; i++) string[length + i] = 0;
59 //printf("File not found: %s\n", filename);
69 StringFile::~StringFile()
74 int StringFile::write_to_file(const char *filename)
76 FILE *out = fopen(filename, "wb");
79 fwrite(string, pointer, 1, out);
83 // printf("Couldn't open %s for writing.\n", filename);
90 int StringFile::read_from_string(const char *string)
94 delete [] this->string;
95 length = strlen(string);
97 this->string = new char[length + 5];
98 strcpy(this->string, string);
99 for(i = 0; i < 5; i++) this->string[length + i] = 0;
103 size_t StringFile::get_length()
105 return strlen(string);
108 size_t StringFile::get_pointer()
113 int StringFile::readline(char *arg2)
115 readline(string1, arg2);
119 int StringFile::readline()
121 readline(string1, string1);
125 int StringFile::readline(float *arg2)
127 readline(string1, arg2);
131 int StringFile::readline(int *arg2)
133 readline(string1, arg2);
137 int StringFile::readline(long *arg2)
139 readline(string1, arg2);
143 int StringFile::readline(Freq *arg2)
145 readline(string1, &(arg2->freq));
149 int StringFile::readline(char *arg1, char *arg2)
154 while(string[pointer] == ' ') pointer++; // skip indent
155 arg1[0] = 0; arg2[0] = 0;
157 for(i = 0; string[pointer] != ' ' && string[pointer] != '\n' && len < max; i++, pointer++)
159 arg1[i] = string[pointer];
164 if(string[pointer] != '\n')
166 pointer++; // skip space
167 for(i = 0; string[pointer] != '\n' && len < max; i++, pointer++)
169 arg2[i] = string[pointer];
174 pointer++; // skip eoln
178 int StringFile::backupline()
180 while(string[pointer] != 10 && pointer > 0)
182 pointer--; // first eoln
184 if(string[pointer] == 10) pointer--; // skip eoln
186 while(string[pointer] != 10 && pointer > 0)
188 pointer--; // second eoln
191 if(string[pointer] == 10) pointer++; // skip eoln
195 int StringFile::readline(char *arg1, long *arg2)
197 readline(arg1, string1);
198 *arg2 = atol(string1);
202 int StringFile::readline(char *arg1, int *arg2)
205 readline(arg1, &arg);
210 int StringFile::readline(char *arg1, float *arg2)
212 readline(arg1, string1);
213 *arg2 = atof(string1);
217 int StringFile::writeline(char *arg1, int indent)
219 // reallocate the string
220 int len = strlen(arg1);
221 if( (len + indent) > (int)(available - pointer) ) {
222 char *newstring = new char[available * 2];
223 strcpy(newstring, string);
230 for(int i = 0; i < indent; i++) string[pointer++] = ' ';
231 strcpy(&string[pointer], arg1);
236 int StringFile::writeline(char *arg1, char *arg2, int indent)
238 sprintf(string1, "%s %s\n", arg1, arg2);
239 writeline(string1, indent);
243 int StringFile::writeline(char *arg1, long arg2, int indent)
245 sprintf(string1, "%s %ld\n", arg1, arg2);
246 writeline(string1, indent);
250 int StringFile::writeline(char *arg1, int arg2, int indent)
252 sprintf(string1, "%s %d\n", arg1, arg2);
253 writeline(string1, indent);
257 int StringFile::writeline(char *arg1, float arg2, int indent)
259 sprintf(string1, "%s %f\n", arg1, arg2);
260 writeline(string1, indent);
264 int StringFile::writeline(char *arg1, Freq arg2, int indent)
266 sprintf(string1, "%s %d\n", arg1, arg2.freq);
267 writeline(string1, indent);