File: rstring.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 (495 lines) | stat: -rw-r--r-- 10,502 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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/***************************************************************************
                          rstring.cpp  -  description
                             -------------------
    begin                : Mon Sep 27 1999
    copyright            : (C) 1999 by Andreas Mustun
    email                : andrew@ribbonsoft.com
 ***************************************************************************/


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

#include "rstring.h"

#include <ctype.h>
#include <string.h>

#include <qfileinfo.h>

#include "rlog.h"


// Get nick name of a file (short file name)
//
QString
strGetNickName(QString _fileName)
{
  QFileInfo fileInfo(_fileName);

  if(!fileInfo.fileName().isNull()) {
    return fileInfo.fileName();
  }
  else {
    return 0;
  }
}




// Get the int value from a line from a INI-File:
//   e.g.: "ArrowSize = 5"  returns 5
//
// _iniLine: Line from ini-file
//
// return: read int value
//         0: failure or 0-Value
//
int     
strGetIntFromIniLine(QString _iniLine)
{
  int     eqPos;      // position of equal symbol (=) in string
  QString numStr;     // 
  
  eqPos = _iniLine.find('=', 0);
  if(eqPos>0) {
    ++eqPos;
    numStr = _iniLine.mid(eqPos, 16);
    numStr = numStr.stripWhiteSpace();
    return numStr.toInt();
  }
  else {
    return 0;
  }
}



// Get the float value from a line from a INI-File:
//   e.g.: "Tolerance = 0.001"  returns 0.001
//
// _iniLine: Line from ini-file
//
// return: read float value
//         0.0: failure or 0.0-Value
//
float
strGetFloatFromIniLine(QString _iniLine)
{
  int     eqPos;      // position of equal symbol (=) in string
  QString numStr;     // 
  
  eqPos = _iniLine.find('=', 0);
  if(eqPos>0) {
    ++eqPos;
    numStr = _iniLine.mid(eqPos, 16);
    numStr = numStr.stripWhiteSpace();
    return numStr.toFloat();
  }
  else {
    return 0;
  }
}



// Get the character from a line from a INI-File:
//   e.g.: "QuotationMark1 = <"  returns '<'
//
// _iniLine: Line from ini-file
//
// return: read char value
//         '\0': failure or no value
//
char
strGetCharFromIniLine(QString _iniLine)
{
  int     eqPos;      // position of equal symbol (=) in string
  QString numStr;     // 
  
  eqPos = _iniLine.find('=', 0);
  if(eqPos>0) {
    ++eqPos;
    numStr = _iniLine.mid(eqPos, 64);
    numStr = numStr.stripWhiteSpace();
    return numStr.at(0).latin1();
  }
  else {
    return '\0';
  }
}



// Get the string from a line from a INI-File:
//   e.g.: "DxfPath = C:\data\dxf"  returns C:\data\dxf
//
// _iniLine: Line from ini-file
//
// return: read string value
//         "": failure or no value
//
QString
strGetStringFromIniLine(QString _iniLine)
{
  int     eqPos;      // position of equal symbol (=) in string
  QString numStr;     // 
  
  eqPos = _iniLine.find('=', 0);
  if(eqPos>0) {
    ++eqPos;
    numStr = _iniLine.mid(eqPos, 1024);
    numStr = numStr.stripWhiteSpace();
    return numStr;
  }
  else {
    return "";
  }
}



// Is the character _ch a valid character for an order?
//
bool
strIsValidNcOrderChar(char _ch) 
{
  if(isdigit(_ch) || _ch=='.' || _ch=='-' || _ch==':') {
    return true;
  }
  else {
    return false;
  }
}



// Test if the string _str is an integer value:
//   (like "37" / NOT "37.5")
//
bool     
strIsInteger(const char* _str)
{
  bool ret=true;
  
  if(_str) {
    int i;
    for(i=0; i<(int)strlen(_str); ++i) {
      if(!isdigit(_str[i])) {
        ret=false;
        break;
      }
    }
  }
  else {
    ret=false;
  }
  
  return ret;
}



// Test if the string _str contains only spaces:
//   (like "  " / NOT "" / NOT "   \n")
//
bool     
strIsSpace(const char* _str)
{
  bool ret=true;
  
  if(_str) {
    int i;
    for(i=0; i<(int)strlen(_str); ++i) {
      if(_str[i]!=' ') {
        ret=false;
        break;
      }
    }
  }
  else {
    ret=false;
  }
  
  return ret;
}



// Converts a float into a String which is as short as possible
//   (2.10000 -> 2.1, 2.00000 -> 2)
//
QString
strFloatToString(float _value)
{
  QString valStr;

  valStr.setNum(_value, 'f', 6);

  if(valStr.contains('.')) {
    // Remove zeros at the end:
    //
    while(valStr.at(valStr.length()-1)=='0') {
      valStr.truncate(valStr.length()-1);  /* ### UNSURE ### OK: 19990804 ###*/
    }

    if(valStr.at(valStr.length()-1)=='.') {
      valStr.truncate(valStr.length()-1);
    }
  }

  return valStr;
}



// Replace all char _old with char _new:
//   (e.g.: '_' with ' ')
//
// return: true: _old found and replaced
//         false: otherwise
//
bool     
strReplaceChar(char* _str, char _old, char _new)
{
  bool ret=false;
  
  if(_str) {
    int i;
    for(i=0; i<(int)strlen(_str); ++i) {
      if(_str[i]==_old) {
        _str[i]=_new;
        ret=true;
      }
    }
  }
  
  return ret;
}



// Get values from a parameter line (usually 
//   read from a machine generator)
//
// Line must have the following format:
//   Parameter free $e Extension NC   CNC D
//   --------- ---- -- --------- --   -----
//       \       \   \      \     \      \_____ List with possible values
//        \       \   \      \     \___________ Standard value
//         \       \   \      \________________ Description
//          \       \   \______________________ Parameter code
//           \       \_________________________ free or fixed
//            \________________________________ Parameter or LayerParameter
//
// values are returned in args
//
void
strGetValuesFromParameterLine(const char* _paraLine, 
                              bool* _layer, 
                              bool* _rw,
                              char* _code,
                              char* _label,
                              char* _std,
                              char* _list)
{
  if(_paraLine) {
    char  scode;            // Code for parameter
    char  slayer[128];      // "LayerParameter" / "Parameter"
    char  slabel[128];      // Label for parameter
    char  smode[16];        // Mode: free or fixed
    char  sstd[64];         // Standard value
    char  slist[256];       // List of other values seperated with spaces

    scode=0;
    slayer[0]='\0';
    slabel[0]='\0';
    smode[0]='\0';
    sstd[0]='\0';
    slist[0]='\0';

    RLOG("\n_paraLine: ");
    RLOG(_paraLine);
  
    sscanf(_paraLine, 
           // ."LayerParameter" ."free " ."$x "         .Label       .Std     .List
            //"%*[ ]%[^ \n]%*[ ]%15s%*[ ]%*[$]%c%*[^ \n]%127s%*[^ \n]%63s%*[ ]%255[^\n]", 
            "%[^ \n]%*[ ]%15s%*[ ]%*[$]%c%127s%63s%*[ ]%255[^\n]",
            slayer, smode, &scode, slabel, sstd, slist);

    RLOG("\nscode: ");
    RLOG(scode);
    RLOG("\nslayer: ");
    RLOG(slayer);
    RLOG("\nslabel: ");
    RLOG(slabel);
    RLOG("\nsmode: ");
    RLOG(smode);
    RLOG("\nsstd: ");
    RLOG(sstd);
    RLOG("\nslist: ");
    RLOG(slist);
  
    if(_layer) {
      if(!strcmp(slayer, "LayerParameter")) *_layer=true;
      else                                  *_layer=false;
    }
    if(_rw) {
      if(!strcmp(smode, "free")) *_rw=true;
      else                       *_rw=false;
    }
    if(_code) *_code=scode;
    if(_label) strcpy(_label, slabel);
    if(_std)   strcpy(_std, sstd);
    if(_list)  strcpy(_list, slist);
  }
}



// Get read write flag (fixed/free) from a parameter line (usually 
//   read from a machine generator)
//
// return: true:  read/write
//         false: read only
//
bool 
strGetReadWriteFromParameterLine(QString _paraLine)
{
  char readWrite[128];
  
  sscanf(_paraLine.data(), 
         //      ."Parameter" ."free"
           "%*[ ]%*[^ \n]%*[ ]%15s", 
           readWrite);

  if(!strcmp(readWrite, "free")) {
    return true;
  }
  else {
    return false;
  }
}



// Step to order item _index:
//
//  "OrderLine { <NewLine>N <Number>... }"
//               0       1^ ^2
//   012345.............21^
//
//   _overReadEnd: true: We don't count end items (delete item)
//
//   return: char index of position 1->21
//           -1: failure
//               
int
strStepToOrderItem(const char* _order, int _index, bool _overReadEnd)
{
  const char* pos;    // Ptr (walks through order)
  int         ret=-1; // Return value (integer pos)
  int         i;      // Counter
  
  pos = _order;

  if(pos && strlen(pos)>0) {
    ret=0;
          
    // Step to 1st char after '{':
    //
    while(pos[0]!='\0' && pos[0]!='{') { ++pos; ++ret; }
    if(pos[0]!='\0') { ++pos; ++ret; }
    
    i=0;
    if(i<_index) {
      do {      
        if(pos[0]=='<') {
          // Don't count end items:
          //
          if(!_overReadEnd || strncmp(pos, "</", 2)) {
            ++i;
          }
          
          while(pos[0]!='\0' && pos[0]!='>') { ++pos; ++ret; }
          if(pos[0]!='\0') { ++pos; ++ret; }
        }
        else if(pos[0]=='$') {
          if(pos[0]!='\0') { ++pos; ++ret; }
          if(pos[0]!='\0') { ++pos; ++ret; }
          ++i;
        }
        else {
          while(pos[0]!='\0' && pos[0]!='<' && pos[0]!='$' && pos[0]!='}') { ++pos; ++ret; }
          ++i;
        }

        RLOG("\nWe're at item: ");
        RLOG(i);
        RLOG(": ");
        RLOG(pos);
          
      }while(i<_index || (_overReadEnd && !strncmp(pos, "</", 2)));
    }
  }
  return ret;
}



// Get order item length:
//
//  "OrderLine { <NewLine>N <Number>... }"
//               0       1^ ^2
//   012345.............21^
//
//  _orderItem points to item
//
// return: char index of position 1->21
//         -1: failure
//               
int
strOrderItemLength(const char* _orderItem)
{
  int ret=-1; // Return value (integer length)
  
  if(_orderItem && strlen(_orderItem)>0) {
    ret=0;
    
    // Special Item (<...>):
    //      
    if(_orderItem[0]=='<') {
      while(_orderItem[0]!='\0' && 
            _orderItem[0]!='>'  &&
            _orderItem[0]!='}'     ) { ++_orderItem; ++ret; }
      if   (_orderItem[0]!='\0' &&
            _orderItem[0]!='}'     ) { ++_orderItem; ++ret; }
    }

    // Special Item ($x):
    //
    else if(_orderItem[0]=='$') {
      return 2;
    }

    // Text-Item:
    //
    else {
      while(_orderItem[0]!='\0' && 
            _orderItem[0]!='<'  && 
            _orderItem[0]!='$'  &&
            _orderItem[0]!='}'     ) { ++_orderItem; ++ret; }
    }
  }
  return ret;
}


// EOF