File: rfile.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 (327 lines) | stat: -rw-r--r-- 7,422 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
/***************************************************************************
                          rfile.cpp  -  description
                             -------------------
    begin                : Mon Sep 27 1999
    copyright            : (C) 1999 by Andreas Mustun
    email                : andrew@ribbonsoft.com
 ***************************************************************************/


/****************************************************************************
** rfile.cpp 1998/09/25 A. Mustun RibbonSoft
**
** Copyright (C) 1998 RibbonSoft.  All rights reserved.
**
*****************************************************************************/

#include "rfile.h"

#include <qfileinfo.h>

#include "rconfig.h"
#include "rlog.h"
#include "ryesnodialog.h"


// Get nick name of a file (short file name)
//
// copies file _source to file _dest
//
// _what: What is this file? (Machinegenerator, ...) Used for overwrite warning
//
// return: true: successful
//         false: error
//
bool 
fileCopy(QString _source, QString _dest, const char* _what, QWidget* _parent)
{
  bool ret=false;
  
  QFile sourceFile(_source.data());
  QFile destFile(_dest.data());

  if(!destFile.exists() || 
     fileOverwriteWarning(_dest.data(), _what, _parent)) {
      
    if(sourceFile.open(IO_ReadOnly)) {
      if(destFile.open(IO_WriteOnly)) {
    
        char* buffer;
        buffer= new char[sourceFile.size()+1];
      
        sourceFile.readBlock(buffer, sourceFile.size());
        destFile.writeBlock(buffer, sourceFile.size());
               
        destFile.close();
        ret=true;
      }
      sourceFile.close();
    }
  }
  
  return ret;
}



// Overwrite-Dialog:
//
// _file: filename
// _what: e.g.: "Machine-Generator 'name' does already..."
//               -> _what = Machine-Generator
// _ext:  show extensions
//
bool
fileOverwriteWarning(const char* _file, const char* _what, QWidget* _parent)
{
  if(_file && _what) {
    // Overwrite?
    //
    QString mes;
    mes.sprintf("%s\n'%s'\n%s\n"
                "%s\n",
                _what, _file, RMES(176), RMES(177));
    RYesNoDialog overDlg(mes.data(), 
                         RMES(168), RMES(169),
                         _parent);
    if(overDlg.exec()) {
      return true;
    }
  }
  
  return false;
}


// Delete file _file:
//
// _what: e.g.: "Machine-Generator 'name' does already..."
//               -> _what = Machine-Generator
//
bool 
fileDelete(const char* _file, const char* _what, QWidget* _parent)
{
  if(_file && _what) {
    QFile file(_file);
    if(file.exists()) {
      QString mes;
      mes.sprintf("%s %s\n'%s'?",
                  RMES(178), _what, _file);
      RYesNoDialog overDlg(mes.data(), 
                           RMES(168), RMES(169),
                           _parent);
      if(overDlg.exec()) {
        QFile(_file).remove();
        return true;
      }
    }
  }
  return false;
}



// Check Format of file _file:
//   Format must be _format and version must be _version
//
bool     
fileCheckFormat(const char* _file, 
                const char* _format, 
                const char* _version)
{
  bool ret=true;
  char value[128];
  
  RLOG("\nfileCheckFormat");
  RLOG("\nFile: ");
  RLOG(_file);
  RLOG("\nFormat: ");
  RLOG(_format);
  RLOG("\nVersion: ");
  RLOG(_version);
  
  // Check Format type:
  //
  if(_format) {
    fileGetFormatInfo(_file, "Format", value, 127);
    
    RLOG("\nRead Format: ");
    RLOG(value);
    
    if(stricmp(value, _format)) {
      ret=false;
    }
  }
  
  // Check Format version:
  //
  if(_version) {
    fileGetFormatInfo(_file, "Version", value, 127);
    
    RLOG("\nRead Version: ");
    RLOG(value);
    
    if(stricmp(value, _version)) {
      ret=false;
    }
  }
  
  RLOG("\nReturn: ");
  RLOG(ret);

  return ret;
}




// Get Format info of file _file. Info name: _name
//
void
fileGetFormatInfo(const char* _file, const char* _name, char* _value, int _maxLength)
{
  bool done=false;     // Finished?
    
  RLOG("\nfileGetFormatInfo");
  RLOG("\nFile: ");
  RLOG(_file);
  RLOG("\nName: ");
  if(_name) RLOG(_name);
  RLOG("\nMax. Length: ");
  RLOG(_maxLength);
  
  if(_file && _name) {
    QFile file(_file);
    if(file.exists()) {
      FILE* fp=fopen(_file, "rt");
    
      if(fp) {
        
        // Check Format:
        //
        char  fLine[128];    // The whole line:  "# Format:     CAM Expert Font"
        char* fWalker;       // Walk through line
        char  fName[128];    // Info name:       "Format"
        char  fValue[128];   // Info value:      "CAM Expert Font"
        char  fDum1[128];    // Dummy for length measuring
        char  fDum2[128];    // Dummy for length measuring
        
        while(!done && !feof(fp)) {
          
          fLine[0]='\0';
          fName[0]='\0';
          fValue[0]='\0';
          fDum1[0]='\0';
          fDum2[0]='\0';

          // Read whole line:
          //
          fscanf(fp, "%127[^\n]%*[\n]", fLine);
          fWalker=fLine;
          
          RLOG("\nWhole Line: ");
          RLOG(fWalker);
          
          // Scan #:
          //
          if(fWalker[0]=='#') {
            
            // Scan Name of current format info:
            //
            ++fWalker;
            sscanf(fWalker, "%127[ ]%127[^: \n]%127[ :]", fDum1, fName, fDum2);
            
            RLOG("\nName: ");
            RLOG(fName);
            RLOG("\nDum1: "); 
            RLOG(fDum1);
            RLOG("\nDum2: "); 
            RLOG(fDum2); 
            
            // Overjump read strings:
            //
            fWalker+=strlen(fDum1);   // Overjump space
            fWalker+=strlen(fName);   // Overjump name
            fWalker+=strlen(fDum2);   // Overjump colon and space
            
            // Scan Value of current format info:
            //
            sscanf(fWalker, "%127[^\n]", fValue);
            if(fValue[strlen(fValue)-1]<32) fValue[strlen(fValue)-1]='\0';
            
            RLOG("\nValue: ");
            RLOG(fValue);
            
            if(!stricmp(fName, _name)) {
              strncpy(_value, fValue, _maxLength);
              done=true;
              
              RLOG("\nValue: ");
              RLOG(_value);

            }
          }
        }
        
        fclose(fp);
      } // -> if(fp)
      
    }
  }
}



/**
 * Searchs a file with a given subdir and a file name.
 */
QString
fileSearchFile(QString _subdir, QString _name)
{
  QString ret = RCONFIG->getPrgDir().path()+"/"+_subdir+"/"+_name;
  if(QFileInfo(ret).exists()) return ret;

#ifndef DEF_WINDOWS
  // Test /usr/share/<proj>/...:
  ret = QString("/usr/share/")+DEF_APPNAME_L+"/"+_subdir+"/"+_name;
  if(QFileInfo(ret).exists()) return ret;

  // Read from ~/.<proj>/fonts:
  ret = RCONFIG->getConfigDir().path()+"/"+_subdir+"/"+_name;
  if(QFileInfo(ret).exists()) return ret;
#endif

  ret="";
  return ret;
}


/**
 * Searchs a subdir.
 */
QString
fileSearchSubdir(QString _subdir)
{
  QString ret = RCONFIG->getPrgDir().path()+"/"+_subdir;
  if(QFileInfo(ret).exists()) return ret;

#ifndef DEF_WINDOWS
  // Test /usr/share/<proj>/...:
  ret = QString("/usr/share/")+DEF_APPNAME_L+"/"+_subdir;
  if(QFileInfo(ret).exists()) return ret;

  // Read from ~/.<proj>/fonts:
  ret = RCONFIG->getConfigDir().path()+"/"+_subdir;
  if(QFileInfo(ret).exists()) return ret;
#endif

  ret="";
  return ret;
}



// EOF