1 #include "stringfile.h"
6 StringFile::StringFile(long length)
11 this->length = 100000;
15 this->length = length;
17 string = new char[this->length + 1];
18 available = this->length;
21 StringFile::StringFile(char *filename)
24 if(in = fopen(filename, "rb"))
26 fseek(in, 0, SEEK_END);
29 fseek(in, 0, SEEK_SET);
30 string = new char[length + 5];
32 fread(string, length, 1, in);
33 for(int i = 0; i < 5; i++) string[length + i] = 0;
38 //printf("File not found: %s\n", filename);
48 StringFile::~StringFile()
53 int StringFile::write_to_file(char *filename)
56 if(out = fopen(filename, "wb"))
58 fwrite(string, pointer, 1, out);
62 // printf("Couldn't open %s for writing.\n", filename);
69 int StringFile::read_from_string(char *string)
73 delete [] this->string;
74 length = strlen(string);
76 this->string = new char[length + 5];
77 strcpy(this->string, string);
78 for(i = 0; i < 5; i++) this->string[length + i] = 0;
82 long StringFile::get_length()
84 return strlen(string);
87 long StringFile::get_pointer()
92 int StringFile::readline(char *arg2)
94 readline(string1, arg2);
98 int StringFile::readline()
100 readline(string1, string1);
104 int StringFile::readline(float *arg2)
106 readline(string1, arg2);
110 int StringFile::readline(int *arg2)
112 readline(string1, arg2);
116 int StringFile::readline(long *arg2)
118 readline(string1, arg2);
122 int StringFile::readline(Freq *arg2)
124 readline(string1, &(arg2->freq));
128 int StringFile::readline(char *arg1, char *arg2)
133 while(string[pointer] == ' ') pointer++; // skip indent
134 arg1[0] = 0; arg2[0] = 0;
136 for(i = 0; string[pointer] != ' ' && string[pointer] != '\n' && len < max; i++, pointer++)
138 arg1[i] = string[pointer];
143 if(string[pointer] != '\n')
145 pointer++; // skip space
146 for(i = 0; string[pointer] != '\n' && len < max; i++, pointer++)
148 arg2[i] = string[pointer];
153 pointer++; // skip eoln
157 int StringFile::backupline()
159 while(string[pointer] != 10 && pointer > 0)
161 pointer--; // first eoln
163 if(string[pointer] == 10) pointer--; // skip eoln
165 while(string[pointer] != 10 && pointer > 0)
167 pointer--; // second eoln
170 if(string[pointer] == 10) pointer++; // skip eoln
174 int StringFile::readline(char *arg1, long *arg2)
176 readline(arg1, string1);
177 *arg2 = atol(string1);
181 int StringFile::readline(char *arg1, int *arg2)
184 readline(arg1, &arg);
189 int StringFile::readline(char *arg1, float *arg2)
191 readline(arg1, string1);
192 *arg2 = atof(string1);
196 int StringFile::writeline(char *arg1, int indent)
200 // reallocate the string
201 if(pointer + strlen(arg1) > available)
203 char *newstring = new char[available * 2];
204 strcpy(newstring, string);
211 for(i = 0; i < indent; i++, pointer++) string[pointer] = ' ';
212 sprintf(&string[pointer], arg1);
213 pointer += strlen(arg1);
217 int StringFile::writeline(char *arg1, char *arg2, int indent)
221 sprintf(string1, "%s %s\n", arg1, arg2);
222 writeline(string1, indent);
226 int StringFile::writeline(char *arg1, long arg2, int indent)
228 sprintf(string1, "%s %ld\n", arg1, arg2);
229 writeline(string1, indent);
233 int StringFile::writeline(char *arg1, int arg2, int indent)
235 sprintf(string1, "%s %d\n", arg1, arg2);
236 writeline(string1, indent);
240 int StringFile::writeline(char *arg1, float arg2, int indent)
242 sprintf(string1, "%s %f\n", arg1, arg2);
243 writeline(string1, indent);
247 int StringFile::writeline(char *arg1, Freq arg2, int indent)
249 sprintf(string1, "%s %d\n", arg1, arg2.freq);
250 writeline(string1, indent);