CONFIG LIB 1.5
Configuration Files Library (by TGG 2020)
Loading...
Searching...
No Matches
iofun.cpp
1/*********************************************************************/
2/* */
3/* Config files library - (C) TGG 2015 */
4/* */
5/*********************************************************************/
6/* Warszawa, 2015 */
7/*********************************************************************/
8/* */
9/* File: iofun.cpp */
10/* */
11/* Author: T.Grabowski */
12/* */
13/* Contents - simple I/O functions class */
14/* */
15/* */
16/*********************************************************************/
17/* */
18/* */
19
20#include "iofun.h"
21
22
23// CONVERT WINDOWS BACKSLASHES TO UNIX FRONTSLASHES
24
25void IOFUN::Win2Unix(char *s)
26{
27 for ( ; *s; s++ )
28 if ( *s == '\\' ) *s = '/';
29}
30
31// CONVERT UNIX FRONTSLASHES TO WINDOWS BACKSLASHES
32
33void IOFUN::Unix2Win(char *s)
34{
35 for ( ; *s; s++ )
36 if ( *s == '/' ) *s = '\\';
37}
38
39
40/*--------------------------------------------------------------------*/
41
42int IOFUN::ReadComm( FILE *stream )
43{
44 char a;
45 while ( a = fgetc( stream ) , ( a != '\n' && a != EOF && a != 0xD ) ); /* omit comment */
46 if( a == EOF )return EOF;
47 return 0;
48}
49
50void IOFUN::ReadDummy( FILE *stream )
51{
52 unsigned char ucRes = 1;
53 while ( ucRes )
54 {
55 char ch = fgetc(stream);
56 ucRes = (ch == '#');
57 ungetc(ch,stream);
58 //fseek( stream, -1, SEEK_CUR );
59 if( ucRes )ReadComm( stream );
60 }
61}
62
63/*--------------------------------------------------------------------*/
64
65// read up to four values from one line with comment
66
67int IOFUN::ReadPar( FILE *stream, const char *Format, void *Par )
68{
69 ReadDummy( stream );
70 fscanf( stream, Format, Par ); /* read value */
71 return ReadComm( stream );
72}
73
74int IOFUN::ReadPar( FILE *stream, const char *Format, void *Par1, void *Par2 )
75{
76 ReadDummy( stream );
77 fscanf( stream, Format, Par1, Par2 );
78 return ReadComm( stream );
79}
80
81int IOFUN::ReadPar( FILE *stream, const char *Format, void *Par1, void *Par2, void *Par3 )
82{
83 ReadDummy( stream );
84 fscanf( stream, Format, Par1, Par2, Par3 );
85 return ReadComm( stream );
86}
87
88int IOFUN::ReadPar( FILE * stream, const char * Format, void *Par1, void *Par2, void *Par3, void *Par4 )
89{
90 ReadDummy( stream );
91 fscanf( stream, Format, Par1, Par2, Par3, Par4 );
92 return ReadComm( stream );
93}
94
95/*--------------------------------------------------------------------*/
96
97// read n-element vector from one line with comment
98
99int IOFUN::ReadVect( FILE *stream, double *Par, int n, double dScal )
100{
101 ReadDummy( stream );
102 int i, ierr = 0;
103 for( i=0; i<n; i++ ) ierr = fscanf( stream, "%lf", Par+i ); /* read values */
104 if( ierr == -1 ) return -1;
105 ReadComm( stream );
106 for( i=0; i<n; i++ ) Par[i] *= dScal;
107 return 0;
108}
109int IOFUN::ReadVect( FILE *stream, float *Par, int n, float fScal )
110{
111 ReadDummy( stream );
112 int i, ierr = 0;
113 for( i=0; i<n; i++ ) ierr = fscanf( stream, "%f", Par+i );
114 if( ierr == -1 ) return -1;
115 ReadComm( stream );
116 for( i=0; i<n; i++ ) Par[i] *= fScal;
117 return 0;
118}
119int IOFUN::ReadVect( FILE *stream, long *Par, int n, long lScal )
120{
121 ReadDummy( stream );
122 int i, ierr = 0;
123 for( i=0; i<n; i++ ) ierr = fscanf( stream, "%ld", Par+i );
124 if( ierr == -1 ) return -1;
125 ReadComm( stream );
126 for( i=0; i<n; i++ ) Par[i] *= lScal;
127 return 0;
128}
129int IOFUN::ReadVect( FILE *stream, int *Par, int n, int iScal )
130{
131 ReadDummy( stream );
132 int i, ierr = 0;
133 for( i=0; i<n; i++ ) ierr = fscanf( stream, "%d", Par+i );
134 if( ierr == -1 ) return -1;
135 ReadComm( stream );
136 for( i=0; i<n; i++ ) Par[i] *= iScal;
137 return 0;
138}
139/*--------------------------------------------------------------------*/
140
141// read 3-element vector from one line with comment
142
143int IOFUN::ReadVect3( FILE *stream, double *Par, double dScal )
144{
145 return ReadVect( stream, Par, 3, dScal );
146}
147int IOFUN::ReadVect3( FILE *stream, float *Par, float fScal )
148{
149 return ReadVect( stream, Par, 3, fScal );
150}
151int IOFUN::ReadVect3( FILE *stream, long *Par, long lScal )
152{
153 return ReadVect( stream, Par, 3, lScal );
154}
155int IOFUN::ReadVect3( FILE *stream, int *Par, int iScal )
156{
157 return ReadVect( stream, Par, 3, iScal );
158}
159/*--------------------------------------------------------------------*/
160
161// read line
162
163int IOFUN::ReadStr( FILE * stream, char *Par )
164{
165 ReadDummy( stream );
166
167 char cc;
168 int i;
169
170 cc = fgetc( stream );
171 i = 0;
172 while ( cc != '\n' && cc != EOF && cc != 0xD )
173 {
174 Par[i] = cc;
175 cc = fgetc( stream);
176 i++;
177 }
178 if( cc == 0xD )
179 {
180 cc = fgetc( stream );
181 if( cc != 0xA )ungetc( cc, stream );
182 }
183
184 Par[i]='\0';
185
186 int j;
187 for( j=i-1; j>0; j-- )
188 {
189 if( isblank(Par[j]) )
190 Par[j] = '\0';
191 else
192 break;
193 }
194
195 char *cRob = new char[j+2];
196 std::strcpy( cRob, Par );
197 int i0 = 0;
198 for( int i=0; i<j+1; i++ )
199 {
200 if( isblank(cRob[i]) )
201 i0 = i+1;
202 else
203 break;
204 }
205 strcpy( Par, cRob+i0 );
206 delete [] cRob;
207
208 if( cc == EOF )return EOF;
209 return strlen( Par );
210}
211
212/*--------------------------------------------------------------------*/
213
214// read line with string length
215
216void IOFUN::ReadStrL( FILE * stream, char *Par , int *len)
217{
218 char cc;
219 int i;
220
221 cc = fgetc( stream );
222 i = 0;
223 while ( cc != '\n' && cc != EOF && cc != 0xD )
224 {
225 Par[i] = cc;
226 cc = fgetc( stream);
227 i++;
228 }
229 if( cc == 0xD )
230 {
231 cc = fgetc( stream );
232 if( cc != 0xA )ungetc(cc,stream);
233 }
234
235 Par[i]='\0';
236 if( cc == EOF )Par[i+1]=EOF;
237 *len = i;
238
239}
240
241/*--------------------------------------------------------------------*/
242
243void IOFUN::ClipString( char* string )
244{
245 int i=0;
246 while( !( isspace( string[i] ) || string[i] == '#' || string[i] == '\0' ) ) i++;
247 string[i] = '\0';
248}
249
250void IOFUN::ClipFileName( char* string )
251{
252 int len = strlen( string );
253 int i=0;
254 while( !( string[i] == '#' || string[i] == '\0' ) ) i++;
255 i--;
256 while( isspace( string[i] ) )i--;
257 i++;
258 string[i] = '\0';
259 for( int ii=i; ii<len; ii++ )string[ii] = '\0';
260
261#ifdef WIN32
262 Unix2Win( string );
263#else
264 Win2Unix( string );
265#endif
266}
267
268/*--------------------------------------------------------------------*/
269
270bool IOFUN::CompareStrings( char* str1, char* str2 )
271{
272 char tmp1[256], tmp2[256];
273 int i;
274 for( i=0; i<(int)strlen(str1); i++ ) tmp1[i] = tolower( str1[i] ); tmp1[i] = '\0';
275 for( i=0; i<(int)strlen(str2); i++ ) tmp2[i] = tolower( str2[i] ); tmp2[i] = '\0';
276 if( strcmp( tmp1, tmp2 ) == 0 )
277 return true;
278 else
279 return false;
280}
281
282/*--------------------------------------------------------------------*/
283
284const char* IOFUN::filename_name( const char *name )
285{
286 const char *p, *q;
287 if (!name) return (0);
288 for ( p = q = name ; *p ; )
289 {
290 if ( ( p[0] == ':' ) && ( p[1] == ':' ) )
291 {
292 q = p+2;
293 p++;
294 }
295 else if (p[0] == '/' || p[0]=='\\' )
296 q = p + 1;
297 p++;
298 }
299 return q;
300}
301
302const char* IOFUN::filename_ext(const char *buf)
303{
304 const char *q = 0;
305 const char *p = buf;
306 for (p=buf; *p; p++)
307 {
308 if (*p == '/') q = 0;
309#ifdef WIN32
310 else if (*p == '\\') q = 0;
311#endif
312 else if (*p == '.') q = p;
313 }
314 return q ? q : p;
315}
316
317char* IOFUN::filename_setext(char *buf, int buflen, const char *ext)
318{
319 char *q = (char *)filename_ext(buf);
320 if (ext)
321 strlcpy(q,ext,buflen - (q - buf));
322 else
323 *q = 0;
324 return (buf);
325}
326
327int IOFUN::file_exist( const char *name )
328{
329 int iReturn = 0;
330 FILE *fc = fopen( name, "r" );
331 if( fc )
332 {
333 fclose( fc );
334 iReturn = 1;
335 }
336
337 return iReturn;
338}
339
340int IOFUN::filename_dir( char *name )
341{
342 int ilen = strlen(name);
343 if( ilen == 0 )return 0;
344 int i = ilen-1;
345 while( name[i] != '/' && name[i] != '\\' && name[i] != ':' && i >= 0 )
346 i--;
347 int id = i+1;
348
349#ifdef WIN32
350 Unix2Win( name );
351#else
352 Win2Unix( name );
353#endif
354
355 return id;
356}
357
358FILE* IOFUN::fopen(const char*filename, const char*mode)
359{
360#ifdef _WIN32
361 int fn_len_s = strlen(filename);
362 int m_len_s = strlen(mode);
363 if( fn_len_s == 0 ) return NULL;
364 if( m_len_s == 0 ) return NULL;
365 wchar_t path[MAX_PATH];
366 wchar_t wmode[MAX_PATH];
367
368 int new_Len_f = MultiByteToWideChar(CP_UTF8, 0, filename, fn_len_s, path, fn_len_s);
369 if( new_Len_f >= MAX_PATH ) return NULL;
370 path[new_Len_f] = L'\0';
371
372 int new_Len_m = MultiByteToWideChar(CP_UTF8, 0, mode, m_len_s, wmode, m_len_s);
373 if( new_Len_m >= MAX_PATH ) return NULL;
374 wmode[new_Len_m] = L'\0';
375
376 return _wfopen(path, wmode);
377#else
378 return ::fopen(filename, mode);
379#endif
380}
381
382int IOFUN::stat(const char* f, struct stat *b)
383{
384#ifdef WIN32
385 int fn_len_s = strlen(f);
386 if( fn_len_s == 0 ) return 0;
387 wchar_t path[MAX_PATH];
388
389 int new_Len_f = MultiByteToWideChar(CP_UTF8, 0, f, fn_len_s, path, fn_len_s);
390 if( new_Len_f >= MAX_PATH ) return 0;
391 path[new_Len_f] = L'\0';
392
393 return _wstat(path, (struct _stat*)b);
394#else
395 return ::stat(f, b);
396#endif
397}
398
399const char* IOFUN::filename_date( const char *name )
400{
401 static char date[100];
402 struct stat b;
403 if( !stat( name, & b ) )
404 {
405 strftime( date, 100, "%d/%m/%Y %H:%M:%S", localtime( & b.st_mtime ) );
406 return date;
407 }
408 else
409 {
410 fprintf( stderr, "filename_date Error\n" );
411 return 0;
412 }
413}
414
415size_t IOFUN::strlcpy(char *d, const char *s, size_t size)
416{
417 if (size <= 0) return 0;
418 size_t len = strlen(s);
419 size_t retVal = len;
420 if (len >= size) len = size-1;
421 memcpy(d, s, len);
422 d[len] = '\0';
423 return retVal;
424}
425
426int IOFUN::nLines( FILE * stream )
427{
428 long lPos = ftell( stream );
429
430 char cc[512];
431 long int i = 0;
432 int iLen = 0;
433 do
434 {
435 ReadStrL( stream, cc, &iLen );
436 if( isstrblank( cc, iLen ) ) continue;
437 i++;
438 }
439 while( iLen > 0 || cc[iLen+1] != EOF );
440 //i--;
441
442 fseek( stream, lPos, SEEK_SET );
443
444 return i;
445}
446
447int IOFUN::nColumns( FILE * stream, char Par[][32] )
448{
449 char cc , cc_old = ' ';
450
451 cc = fgetc( stream );
452 int i = 0;
453 int licz = -1;
454 while ( cc != '\n' && cc != EOF && cc != 0xD )
455 {
456 if( ( isblank(cc_old) && !isblank(cc) ) )
457 {
458 i = 0;
459 licz++;
460 Par[licz][i] = cc;
461 i++;
462 }
463 else if( !isblank(cc) )
464 {
465 Par[licz][i] = cc;
466 i++;
467 }
468 cc_old = cc;
469 cc = fgetc( stream);
470 }
471 if( cc == 0xD) cc = fgetc( stream);
472
473 return licz+1;
474}
475
476int IOFUN::isstrblank( char *cc, int iLen )
477{
478 int iReturn = 1;
479 if( iLen > 0 )
480 {
481 for( int i=0; i<iLen; i++ )
482 {
483 if( !isblank(cc[i]) )
484 {
485 iReturn = 0;
486 break;
487 }
488 }
489 }
490// else
491// iReturn = 0;
492
493 return iReturn;
494}
495
496void IOFUN::ReadVect3( FILE *stream, VECTOR_3D *Vec, double dScal )
497{
498 double Par[3];
499 ReadVect3( stream, Par, dScal );
500 Vec->GetFrom( Par );
501}
502
504//
505// Pascal style "readln" and "read" functions
506// by P.Blaszczyk - modif. by TGG (2002)
507//
509
510int IOFUN::readln( FILE *ff, char *value_x )
511{
512 fscanf( ff, "%s", value_x );
513 return ReadComm( ff );
514 //return 0;
515}
516
517int IOFUN::readln( FILE *ff, int *value_x )
518{
519 ReadPar( ff, "%d", value_x );
520 return 0;
521}
522
523int IOFUN::readln( FILE *ff, long *value_x )
524{
525 ReadPar( ff, "%ld", value_x );
526 return 0;
527}
528
529int IOFUN::readln( FILE *ff, float *value_x )
530{
531 ReadPar( ff, "%f", value_x );
532 return 0;
533}
534
535int IOFUN::readln( FILE *ff, double *value_x )
536{
537 ReadPar( ff, "%lf", value_x );
538 return 0;
539}
540
541int IOFUN::readln( FILE *ff, long double *value_x )
542{
543 ReadPar( ff, "%Lf", value_x );
544 return 0;
545}
546
547int IOFUN::readln( FILE *ff )
548{
549 ReadComm( ff );
550 return 0;
551}
552
553int IOFUN::readln( FILE *ff, VECTOR_3D *vec )
554{
555 ReadVect3( ff, vec );
556 return 0;
557}
558
559// "read" function
560
561int IOFUN::read( FILE *ff, char *value_x )
562{
563 fscanf( ff, "%s", value_x );
564 return 0;
565}
566
567int IOFUN::read( FILE *ff, int *value_x )
568{
569 fscanf( ff, "%d", value_x );
570 return 0;
571}
572
573int IOFUN::read( FILE *ff, long *value_x )
574{
575 fscanf( ff, "%ld", value_x );
576 return 0;
577}
578
579int IOFUN::read( FILE *ff, float *value_x )
580{
581 fscanf( ff, "%f", value_x );
582 return 0;
583}
584
585int IOFUN::read( FILE *ff, double *value_x )
586{
587 fscanf( ff, "%lf", value_x );
588 return 0;
589}
590
591int IOFUN::read( FILE *ff, long double *value_x )
592{
593 fscanf( ff, "%Lf", value_x);
594 return 0;
595}
596
597int IOFUN::read( FILE *ff, VECTOR_3D *vec )
598{
599 fscanf( ff, "%lf%lf%lf", &vec->x, &vec->y, &vec->z );
600 return 0;
601}
602
static void ReadStrL(FILE *stream, char *Par, int *len)
Function to read the string (Par) from FILE "stream". It reads from current pointer of FILE to the en...
Definition iofun.cpp:216
static int ReadVect(FILE *stream, double *Par, int n, double dScal=1.)
Function to read double vector "Par" of "n" dimension from FILE "stream". The read values are scaled ...
Definition iofun.cpp:99
static FILE * fopen(const char *filename, const char *mode)
Cross-platform function to fopen function that supports UTF-8 encoded name.
Definition iofun.cpp:358
static void ClipFileName(char *string)
Clipping of the ending blanc characters of "string".
Definition iofun.cpp:250
static const char * filename_date(const char *name)
Returns pointer to the string that contains the date of last file modification/creation.
Definition iofun.cpp:399
static void Win2Unix(char *s)
conversion of directory separators - MS Windows to Unix
Definition iofun.cpp:25
static int isstrblank(char *cc, int iLen)
Checks if string (cc) is blank (1/0)
Definition iofun.cpp:476
static const char * filename_name(const char *name)
Returns pointer to filename without path.
Definition iofun.cpp:284
static int nColumns(FILE *stream, char Par[][32])
Returns number of columns of text file (number of strings in the first line). Names of columns are st...
Definition iofun.cpp:447
static int read(FILE *ff, char *value_x)
Pascal style "read" function to read string.
Definition iofun.cpp:561
static char * filename_setext(char *buf, int buflen, const char *ext)
set/change filename extension
Definition iofun.cpp:317
static int readln(FILE *ff, char *value_x)
Pascal style "readln" function to read string.
Definition iofun.cpp:510
static int filename_dir(char *name)
returns the length of the path in (path)name
Definition iofun.cpp:340
static int file_exist(const char *name)
Returns true (1) if file "name" does exist, 0 otherwise.
Definition iofun.cpp:327
static int nLines(FILE *stream)
Returns number of lines of text file from current pointer to EOF.
Definition iofun.cpp:426
static int stat(const char *f, struct stat *b)
Cross-platform function to stat() a file using a UTF-8 encoded name or value.
Definition iofun.cpp:382
static int ReadComm(FILE *stream)
Function to read of a comment till the end of line. It returns 0 or EOF if it is performed.
Definition iofun.cpp:42
static void ReadDummy(FILE *stream)
Function to read a comment line.
Definition iofun.cpp:50
static int ReadVect3(FILE *stream, double *Par, double dScal=1.)
Function to read double vector "Par" of "3" dimension from FILE "stream". The read values are scaled ...
Definition iofun.cpp:143
static void ClipString(char *string)
Clipping of the "string" up to the first blanc character or comment "#" character.
Definition iofun.cpp:243
static int ReadStr(FILE *stream, char *Par)
Function to read the new line from FILE "stream" and to store it in table of char "Par"....
Definition iofun.cpp:163
static int ReadPar(FILE *stream, const char *Format, void *Par)
Function to read one variable. The type of variable depends on Format, compatible with stdio library.
Definition iofun.cpp:67
static void Unix2Win(char *s)
conversion of directory separators - Unix to MS Windows
Definition iofun.cpp:33
static const char * filename_ext(const char *buf)
Returns pointer to filename extension.
Definition iofun.cpp:302
static size_t strlcpy(char *d, const char *s, size_t bufsize)
The implementation of nonstandard strlcpy function.
Definition iofun.cpp:415
static bool CompareStrings(char *str1, char *str2)
Compares two strings (str1 and str2). Returns true if they match, false otherwise.
Definition iofun.cpp:270
Vector 3D class - vector manipulation.
Definition vector3d.h:34
double x
x coordinate
Definition vector3d.h:38
double z
z coordinate
Definition vector3d.h:38
void GetFrom(double xp, double yp, double zp)
sets coordinates from three variables
Definition vector3d.h:65
double y
y coordinate
Definition vector3d.h:38
This file contains some smart I/O functions.