summaryrefslogtreecommitdiff
path: root/comments/io.cpp
blob: 6f89340ea886388923c6b9c6ec50eadfa205ff5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
**  This program is free software; you can redistribute it and/or modify
**  it under the terms of the GNU General Public License as published by
**  the Free Software Foundation; either version 2 of the License, or
**  (at your option) any later version.
**
**  This program is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**  GNU General Public License for more details.
**
**  You should have received a copy of the GNU General Public License
**  along with this program; if not, write to the Free Software
**  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/


#include "main.h"
#define CLASS_ERROR_PRE "io"

io::io(char* in, char* out, char *comments, bool testing)
{
  file_open=false;
  first_init=true;
  init(in,out,comments, testing);
};

io::io()
{
  file_open=false;
  first_init=true;
  ready=false; // We didn't init a file, so we're not ready
};

io::~io() // Done, just take care of the files
{
  i.close();
  o.close();
  co.close();
};

// Initialize variables and set everything up for a new file
void io::init(char* in, char* out, char * comments, bool testing)
{
  if(file_open) // dumbo didn't call done()
    done(testing);

  ready=true; // we are ready unless otherwise changed

  // (re)init variables
  memset(buf,'\0',BUF_LENGTH);
  memset(last_written,'\0',LAST_WRITTEN_LENGTH);
  memset(last_read,'\0',LAST_READ_LENGTH);
  memset(o_name,'\0',FILE_NAME_LENGTH);
  memset(i_name,'\0',FILE_NAME_LENGTH);
  memset(c_name,'\0',FILE_NAME_LENGTH);
  icounter=0.0; ocounter=0.0;
  buf_count=0; // reset # of bytes in buffer
  if(first_init) // only on first init set global_counter to 0.0
  {
    first_init=false;
    iglobal_counter=0.0;
    oglobal_counter=0.0;
    addMarkers = 0;
    regionsCount = 0;
  }
  outputRegions = 0;
  input_line=0; input_column=0;
  inComment = -1;
  doneOutput = 0;
  inCode = -1;
  output_line=0; output_column=0;

//  removed because of strange bug causing input_from_stdin to always set to TRUE
//  if(!input_from_stdin)
//  {
    i.open(in/*, ios::nocreate*/);
    if(!i)
    {
      ready=false;
      cerr << "Could not open (input) [" << in << "]" << endl;
    }
#ifdef DEBUG
    else
      cerr << CLASS_ERROR_PRE << "::init() Opened (input) ["
           << in << "]" << endl;
#endif
//  }

  if(!output_to_stdout)
  {
    o.open(out);
    if(!o)
    {
      ready=false;
      cerr << CLASS_ERROR_PRE << "::init() Could not open (output) \""
           << out << "\"" << endl;
    }
    co.open(comments);
    if(!co)
    {
      ready=false;
      cerr << CLASS_ERROR_PRE << "::init() Could not open (comments) \""
           << comments << "\"" << endl;
    }

  }
  else
    output_to_stdout=true;

  strcpy(i_name,in);
  if(output_to_stdout){
    strcpy(o_name,"");
    strcpy(c_name,"");
  } else {
    strcpy(c_name,comments);
    strcpy(o_name,out);
  }

  file_open=true;
};

void io::done(bool testing)
{
  if(!file_open) // your calling me without a open file?
    return;

  // close the files
// removed next if because of strange bug causing input_from_stdin to be set when the code designates otherwise
//  if(!input_from_stdin)
    i.close();
    if(!output_to_stdout) {
    o.close();
    co.close();
    }
  file_open=false;
}

int io::get_input_line()
{
  return input_line;
}

int io::get_input_column()
{
  return input_column;
}

int io::get_output_line()
{
  return output_line;
}

int io::get_output_column()
{
  return output_column;
}

// Get data
int io::in()
{
  memmove((last_read+1),last_read,(LAST_READ_LENGTH-1));
  last_read[0]=buf[0];
  memmove(buf,(buf+1),(BUF_LENGTH-1));

  i.get(buf[(BUF_LENGTH-1)]); // get the next char
  if(i.eof()) // EOF found, cancel that last read
  {
    buf[(BUF_LENGTH-1)]='\0';
    if(buf[0] || buf_count==1) // if there is data at the front, then we erased some
      buf_count--;
  }
  else
    buf_count+=(buf_count<BUF_LENGTH ? (buf_count ? 1 : 2) : 0);

#ifdef IODEBUG
    if(!i.eof())
      cout << i_name << " >> \"" << buf[(BUF_LENGTH-1)] << "\"" << endl;
    else
      cout << CLASS_ERROR_PRE << "::in() " << i_name << " [EOF] " << buf[(BUF_LENGTH-1)] << endl;
#endif

  if(buf[0]=='\n' || buf[0]=='\r')
  {
    input_column=0;
    input_line++;
  }
  else
    input_column++;

  if(buf[(BUF_LENGTH-1)]!='\0')
  {
    icounter++;
    iglobal_counter++;
  }

  return 1;
};

// see if we still have data in the buffer
bool io::data_waiting()
{
  return (buf_count ? true : false);
};


// Output data
void io::out(char c)
{
  if(!c) // replace '\0' with the value of buf[0]
     c=buf[0];
  if(c) // Make sure we have something to spit
  {
    if (inCode == 0 && addMarkers) {
      if(output_to_stdout)
        cout << "/****/";
      else
        o << "/****/";
    }
    inCode = 1;
    // only reset code marker if not a space...
    if (c != ' ' && c!= '\t' &&c!= '\n' && c!= '\r')
      inComment = 0;
    if(output_to_stdout)
      cout << c;
    else
      o << c;

    // keep track of sizes
    ocounter++; // this file
    oglobal_counter++; // all the files
  }

#ifdef IODEBUG
    cout << o_name << " << \"" << c << "\"" << endl;
#endif

  if(c=='\n' || c=='\r') // new line, return column to 0
  {
    output_line++;
    output_column=0;
  }
  else
    output_column++;

  memmove(last_written,(last_written+1),(LAST_WRITTEN_LENGTH-1));
  last_written[(LAST_WRITTEN_LENGTH-1)]=c;
};

// Output data
void io::commentOut(char c)
{
  if (inComment == 0) {
    if (regionsCount > 0 && outputRegions+1 == regionsCount)  {
      doneOutput = 1;
      return;
    }
    if (addMarkers) {
      if(output_to_stdout)
        cerr << "\nCODE\n ";
      else
        co << "\nCODE\n";
    }
    outputRegions++;
  }

  inComment = 1;
  inCode = 0;
  if(!c) // replace '\0' with the value of buf[0]
     c=buf[0];
  if(output_to_stdout)
    cerr << c;
  else
    co << c;

};



bool io::ok()
{
  return (ready ? true : false);
};

double io::input_bytes()
{
  return icounter;
};

double io::output_bytes()
{
  return ocounter;
};

double io::global_input_bytes()
{
  return iglobal_counter;
};

double io::global_output_bytes()
{
  return oglobal_counter;
};