File: rfilebase.cpp

package info (click to toggle)
qcad 1.3.3-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 4,504 kB
  • ctags: 2,449
  • sloc: cpp: 29,400; makefile: 49; sh: 15
file content (377 lines) | stat: -rw-r--r-- 5,923 bytes parent folder | download
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/***************************************************************************
                          rfilebase.cpp  -  description
                             -------------------
    begin                : Mon Sep 27 1999
    copyright            : (C) 1999 by Andreas Mustun
    email                : andrew@ribbonsoft.com
 ***************************************************************************/


/****************************************************************************
** rfilebase.cpp 1998/08/28 A. Mustun RibbonSoft
**
** Copyright (C) 1998 RibbonSoft.  All rights reserved.
**
*****************************************************************************/

#include "rfilebase.h"

#include <qfile.h>
#include <qdatetime.h>

#include "rappwin.h"
#include "rlog.h"
#include "rstatuspanel.h"

// Constructor without name:
//
RFileBase::RFileBase(const char* _name)
{
  fPointer=0;
  fBuf=0;
  fBufP=0;
  fSize=0;
  if(_name) setName(_name);
}



// Destructor:
//
RFileBase::~RFileBase()
{

}



// Reset whole object
//   (base class too)
//
void 
RFileBase::reset()
{
  QFile::reset();
  
  delBuffer();
  fBufP=0;
  fSize=0;
  if(fPointer) {
    fclose(fPointer);
    fPointer=0;
  }
}



// Reset buffer pointer to the beginning of the buffer:
//
void 
RFileBase::resetBufP()
{
  fBufP=0;
}



// Set buffer pointer to the given index:
//
void 
RFileBase::setBufP(int _fBufP)
{
  if(_fBufP<(int)fSize) {
    fBufP = _fBufP;
  }
}


// delete buffer:
//
void 
RFileBase::delBuffer()
{
  if(fBuf) {
    delete[] fBuf;
    fBuf=0;
  }
}



// Remove any 13-characters in the buffer:
//
void
RFileBase::dos2unix()
{
	if(fBuf) {
		char * bDest;
		char * bSrc;
		
		bDest=&fBuf[0];
    bSrc=&fBuf[0];
		
		//RLOG("\nOriginal Buffer (DOS): \n");
		//RLOG(fBuf);
		
		while(bSrc[0]!='\0') {
			while(bSrc[0]==13) ++bSrc;
			
			bDest[0] = bSrc[0];
			++bDest;
			++bSrc;
		}
		
		bDest[0] = '\0';
		
		//RLOG("\nNew Buffer (UNIX): \n");
    //RLOG(fBuf);
	}
}


// Get next line in the buffer:
//   and overread ALL seperators
//
// return:  -Null-string: end of buffer 
//          -String which is the next line in buffer
//
char*
RFileBase::getBufLine()
{
  char* ret=0;
  
  if(fBufP<(int)fSize) {
  
    // Point to current position of buffer:
    //
    ret=&fBuf[fBufP];
    
    // Overread the line data:
    //
    do {
      ++fBufP;
    }while(fBufP<(int)fSize && fBuf[fBufP]!='\0');
    
    // Overread ALL seperators (<32):
    //
    do {
      ++fBufP;
    }while(fBufP<(int)fSize && fBuf[fBufP]<32 && fBuf[fBufP]>=0);
  }

  return(ret);
}



// Copy buffer from a given string:
//
void    
RFileBase::copyBufFrom(const char* _buf)
{
  if(_buf) {
    fBuf = new char[strlen(_buf)+16];
    strcpy(fBuf, _buf);
  }
}



// Go to the next '_lstr'-line in buffer:
//
// return: true:  line found
//         false: end of buffer
//
bool 
RFileBase::gotoBufLine(char* _lstr)
{
  QCString l;
  do {
    l=getBufLine();
  }while(!l.isNull() && l!=_lstr);
  
  if(!l.isNull()) return true;
  return false;
}



// Goto next line where the string _lstr appears:
//
// return: true:  string in line found
//         false: end of buffer
//         
//
bool 
RFileBase::gotoBufLineString(char* _lstr)
{
  QCString l;
  do {
    l=getBufLine();
  }while(!l.isNull() && l.contains(_lstr));

  if(!l.isNull()) return true;
  return false;
}



// Replace bynary Bytes (<32) by an other (given) byte:
//
void
RFileBase::replaceBinaryBytesBy(char _c)
{
  int bc;

  for(bc=0; bc<(int)fSize; ++bc) {
    if(fBuf[bc]<32 && fBuf[bc]>=0) {
      fBuf[bc] = _c;
    }
  }
}



// Seperate buffer (change chars sc1 and sc2 in '\0'
//
void
RFileBase::seperateBuf(char _c1,
                       char _c2,
                       char _c3,
                       char _c4)
{
  int bc;

  for(bc=0; bc<(int)fSize; ++bc) {
    if(fBuf[bc]==_c1 || fBuf[bc]==_c2 ||
       fBuf[bc]==_c3 || fBuf[bc]==_c4    ) {
      fBuf[bc] = '\0';
    }
  }
}



// remove comment between '_fc' and '_lc'
//   comments get replaced by '\0'
//
void
RFileBase::removeComment(char _fc, char _lc)
{
  bool rem=false;   // Are we removing currrently?
  int bc;           // counter
  
  for(bc=0; bc<(int)fSize; ++bc) {
    if(fBuf[bc]==_fc) rem=true;
    if(fBuf[bc]==_lc) { fBuf[bc]='\0'; rem=false; }
    if(rem) fBuf[bc]='\0';
  }
}



// Read file '_name' in buffer (buf)
//
// '_bNum' : Max number of Bytes
//         : -1: All
// return: true: successful
//         false: file not found
//
bool 
RFileBase::readFileInBuffer(char* _name, int _bNum)
{
  setName(_name);
  return readFileInBuffer(_bNum);
}



// Read file in buffer (buf)
//
// 'bNum' : Max number of Bytes
//        : -1: All
// return: true: successful
//         false: file not found
//
bool 
RFileBase::readFileInBuffer(int _bNum)
{
  fPointer = fopen(name(), "rb");
  if(fPointer!=NULL) {
    if(open(IO_ReadOnly, fPointer)) {
      fSize=size();
      if(_bNum==-1) _bNum=fSize;
      
      fBuf = new char[_bNum+16];
      
      readBlock(fBuf, _bNum);
      fBuf[_bNum] = '\0';
      close();
    }
    fclose(fPointer);
  	
  	dos2unix();
    fPointer=NULL;

    return true;
  }
  return false;
}



// Save buffer (buf) to file '_name'
//
// return: true: successful
//         false: can not open file
//
bool 
RFileBase::saveBufferToFile(char* _name)
{
  setName(_name);
  return saveBufferToFile();
}



// Save buffer (buf) to file 
//
// return: true: successful
//         false: file not found
//
bool 
RFileBase::saveBufferToFile()
{
  fPointer = fopen(name(), "wb");
  if(fPointer!=NULL) {
    if(open(IO_WriteOnly, fPointer)) {
      fSize=strlen(fBuf);
      
      writeBlock(fBuf, fSize);
      close();
    }
    fclose(fPointer);
    fPointer=NULL;

    return true;
  }
  return false;
}




// Get status panel for current Doc:
//
RStatusPanel*
RFileBase::statusPanel()
{
  return RAppWin::getRAppWin()->getStatusPanel();
}



// EOF