File: strlib.c

package info (click to toggle)
spd 1.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 3,572 kB
  • sloc: ansic: 25,938; fortran: 10,483; sh: 1,032; makefile: 75
file content (758 lines) | stat: -rw-r--r-- 19,841 bytes parent folder | download | duplicates (2)
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
/*
 *   Project: The SPD Image correction and azimuthal regrouping
 *                      http://forge.epn-campus.eu/projects/show/azimuthal
 *
 *   Copyright (C) 2005-2010 European Synchrotron Radiation Facility
 *                           Grenoble, France
 *
 *   Principal authors: P. Boesecke (boesecke@esrf.fr)
 *                      R. Wilcke (wilcke@esrf.fr)
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   and the GNU Lesser General Public License  along with this program.
 *   If not, see <http://www.gnu.org/licenses/>.
 */
# define STRLIB_VERSION    "strlib : V1.04 Peter Boesecke 2011-10-22"
/*---------------------------------------------------------------------------
NAME

   strlib.c --- string functions 

DESCRIPTION

   Collection of string functions.

HISTORY

   2011-04-27 V1.0  Peter Boesecke
   2011-05-14 V1.01 PB functions renamed from str.. to strlib_..., 
                       strlib_ncasecmp, strlib_toupper, strlib_tolower added,
                       strlib_collapse collapses always to a single space.
   2011-05-25 V1.02 PB strlib_toupper, strlib_tolower: corrected
   2011-06-30 V1.03 PB strlib_concat added
   2011-10-22 V1.04 PB strlib_tok and strlib_tok_r added
    
---------------------------------------------------------------------------*/

/****************************************************************************
*  Include                                                                  *
****************************************************************************/
# include <errno.h>
# include <stdio.h>
# include <stdlib.h>
# include <stdarg.h>
# include <string.h>
# include <ctype.h>

# include "strlib.h"

/***************************************************************************
 * Defines                                                                 *
 ***************************************************************************/

# define BUFLEN EdfMaxLinLen+1

# ifndef MIN
#   define MIN( n1, n2) ( ( n1)<( n2) ? ( n1) : ( n2) )
# endif

# ifndef ABS
#   define ABS( n1) ( ( n1)<0 ? -( n1) : ( n1) )
# endif

/****************************************************************************
* Static Variables                                                          *
****************************************************************************/

PRIVATE char white_spaces[7] = { ' ', '\t', '\r', '\n', '\f', '\v', '\0' };
  
/****************************************************************************
*  Functions                                                                *
****************************************************************************/

/*--------------------------------------------------------------------------
NAME

  strlib_version --- returns pointer to the version string

SYNOPSIS

  const char *strlib_version ( void );

DESCRPTION

  Returns pointer to the version string.

--------------------------------------------------------------------------*/
PUBLIC const char *strlib_version ( void )
{
  return ( STRLIB_VERSION );
} /* strlib_version */

/*---------------------------------------------------------------------------
NAME

    strlib_is_white

SYNOPSIS

    int strlib_is_white ( char c );

DESCRIPTION

Returns 1 if c is a white space, 0, if not.

HISTORY
18-Jan-1998 Peter Boesecke
---------------------------------------------------------------------------*/
PUBLIC int strlib_is_white ( char c )
{
   if (!c) return(0); // no white space
   if ( strchr( white_spaces, (int) c ) )
     return(1); // white space 
   else return(0); // no white space 

} /* strlib_is_white */

/*---------------------------------------------------------------------------
NAME

    strlib_is_no_skip --- returns 0 when the input string is a skip pattern. 

SYNOPSIS

   int strlib_is_no_skip( const char * s );

DESCRIPTION

   Checks, if the input string is a skip sign: "...", "-", ".".

RETURN VALUE

   Returns only 0, when the input string is a skip sign.

---------------------------------------------------------------------------*/
PUBLIC int strlib_is_no_skip( const char * s )
{ int value=0;

  value = strcmp ( s, "..." );
  if (value) value = strcmp ( s, "-" );
  if (value) value = strcmp ( s, "." );

  return ( value );
} // strlib_is_no_skip

/*---------------------------------------------------------------------------
NAME

    strlib_is_empty --- returns 1 if the input string is empty or not allocated

SYNOPSIS

   int strlib_is_empty( const char *s );

DESCRIPTION

   Rreturns 1 if the input string s contains only white space, if it
   has the length 0, or is the NULL pointer, otherwise 0.

RETURN VALUE

   1: if string is empty, 0: otherwise

---------------------------------------------------------------------------*/
PUBLIC int strlib_is_empty( const char *s )
{ const char *ps;
  int empty=1;

  if (s) {
    ps = s;
    while (*ps) { empty = empty&&strlib_is_white(*ps); ps++; }
  }
  return( empty );

} // strlib_is_empty

/*---------------------------------------------------------------------------
NAME

    strlib_uncomment --- removes comments 

SYNOPSIS

   char * strlib_uncomment ( char * s );

DESCRIPTION

   Truncates the string at the comment char ('#').

RETURN VALUE

   Pointer to the modified input string.

---------------------------------------------------------------------------*/
PUBLIC char * strlib_uncomment ( char * s )
{ char *ps;

  if (!s) return("");

  ps=s;

  // copy ps2 to ps1 until end of string or comment char
  while ( *ps ) {
    if (*ps == '#') break;
    ps++;
  }

  // terminate string
  *ps='\0';

  return( s );

} // strlib_uncomment

/*---------------------------------------------------------------------------
NAME

    strlib_trim --- removes leading and trailing white spaces

SYNOPSIS

   char * strlib_trim ( char * s );

DESCRIPTION

   Removes leading and trailing white spaces from the input string.

RETURN VALUE

   Pointer to the modified input string.

---------------------------------------------------------------------------*/
PUBLIC char * strlib_trim ( char * s )
{ char *ps1, *ps2;

  if (!s) return("");

  ps1=ps2=s;

  // search first non-white character (nul is not a white space)
  while ( strlib_is_white( *ps2 ) ) ps2++;

  // copy ps2 to ps1 until end of string
  while ( *ps2 ) {
    *ps1++=*ps2++;
  }

  // terminate string
  *ps1='\0';

  // remove trailing white spaces
  while ( --ps1>=s ) {
    if ( !strlib_is_white(*ps1) ) break;
    *ps1='\0';
  }

  return( s );

} // strlib_trim

/*---------------------------------------------------------------------------
NAME

    strlib_collapse --- collapses to single white spaces

SYNOPSIS

   char * strlib_collapse ( char * s );

DESCRIPTION

   Collapses multiple white spaces to a single space ' '.

RETURN VALUE

   Pointer to the modified input string.

---------------------------------------------------------------------------*/
PUBLIC char * strlib_collapse ( char * s )
{ 
  char *ps1, *ps2;

  if (!s) return("");

  ps1=ps2=s;

  while (*ps2) {
    // copy ps2 to ps1 until next white space
    while ( (*ps2) && (!strlib_is_white( *ps2 )) ) {
      *ps1++=*ps2++;
    }

    // write a single white space
    if (*ps2) {
      *ps1++ = ' ';
      ps2++;
    }

    // search first non-white character
    while ( strlib_is_white( *ps2 ) ) ps2++;

  }

  // terminate string
  *ps1='\0';

  return( s );

} // strlib_strcollapse

/*---------------------------------------------------------------------------
NAME

    strlib_split --- splits string at white spaces

SYNOPSIS

   int strlib_split ( char *sv[], int sc, char * s );

DESCRIPTION

   Splits s white spaces into substrings and writes the pointers to the
   first sc-1 substrings into subsequent cells of sv[]. All white spaces 
   in s are replaced with character '\0' as terminator.  The length 
   of sv is sc. The pointers in sv are valid as long as the string s exists.

RETURN VALUE

   Number of substring pointers in sv[].

---------------------------------------------------------------------------*/
PUBLIC int strlib_split ( char *sv[], int sc, char * s )
{ char *nul = (char *) NULL;
  int cnt=0;
  char *ps;

  if ((!s)||(!sv)||(!sc)) return(0);

  ps=s;

  cnt=0;

  // skip leading white spaces
  while (strlib_is_white( *ps )) ps++;

  while (*ps) {

    if (cnt+1<sc) sv[cnt++]=ps;

    // search next white space
    while ( (*ps) && (!strlib_is_white( *ps )) ) ps++;

    if ( strlib_is_white( *ps ) ) {
      *ps='\0';
      ps++;
    }

    // skip leading white spaces
    while (strlib_is_white( *ps )) ps++;

  }

  // terminate
  if (cnt<sc) sv[cnt]=nul;

  return( cnt );

} // strlib_split

/*---------------------------------------------------------------------------
 NAME

    strlib_concat --- return a pointer to the concatenation of a and b

 SYNOPSIS

    char *strlib_concat( char *buffer, size_t buflen, 
                         const char *a, const char *b )


 DESCRIPTION

    If the pointers to a and to buffer are different the function copies the 
    string a to the destination buffer and appends afterwards the string b.
    If the pointers to a and b are identical the string b is immediately
    appended to the destination buffer without copying a. Apart from that
    the destination buffer may not overlap with the input strings a and b.
    The output string is truncated at buflen-1. The output string is 
    terminated with zero. 

 RETURN VALUE

    pointer to buffer with concatenated strings

---------------------------------------------------------------------------*/
char * strlib_concat( char * buffer, size_t buflen, 
                      const char * a, const char * b )
{ size_t rest, len;
  char *pb;
  if ((!buffer)||(!buflen)) return( buffer );
  buffer[buflen-1] = '\0'; // terminating zero
  pb=buffer;
  rest=buflen-1;
  if (pb!=(char*)a) {
    strncpy( pb, a, rest );
    len=strlen(pb);
    pb+=len;
    rest-=len;
  }
  strncpy( pb, b, rest);
  return ( buffer );
} /* strlib_concat */

/*+++------------------------------------------------------------------------
NAME

   strlib_param --- extract parameter n from string 

SYNOPSIS

   char * strlib_param ( char * buffer, size_t buflen,
                         const char *s, char separator, int parno );

DESCRIPTION
   Extracts substrings from string that are separated by separator and copies 
   them to buffer. If the input string does not contain a comma the full 
   input string is copied. 
   If substring parno is not available (char *) NULL is returned. parno 0 
   returns always the full string.
   Different to the function filename_parameter, the substring before the 
   first comma has the number 1.

   <0> = "<1><separator><2><separator><3><separator>..."

RETURN VALUE
   Pointer to the filled output buffer or NULL if parameter string is not
   available. A parameter between two separators that immediately follow 
   each other is returned as an empty string.

----------------------------------------------------------------------------*/
PUBLIC char * strlib_param ( char * buffer, size_t buflen, 
                             const char *s, char separator, int parno )
{ size_t len;
  char *tmp=NULL;
  const char *str1, *str2, *strend;
  int cnt;

  if (!s) goto strlib_param_error; // missing input string 

  if (parno<0) goto strlib_param_error; // parameter is not available

  len = strlen(s);
  if ( !( tmp = (char*) malloc( sizeof(char)*(len+1) )) )
    goto strlib_param_error;

  // copy s into temporary buffer
  strcpy (tmp, s);

  strend = tmp+len;
  cnt = 0; str1 = tmp; str2 = str1-1;
  while ( cnt++ < parno ) { // start numbering before the first separator
    // set str1 to start of parameter
    if ( str2 < strend ) {
      str1 = ++str2; // skip separator
    } else {
      goto strlib_param_error; // no more parameter available
    }
    // search end of parameter
    if ( (str2 = strchr (str1, separator)) == (char *) NULL) {
      str2 = strend;
    } 
  }

  // copy parameter
  len = MIN(buflen,(size_t) (str2-str1+1))-1;
  strncpy (buffer, str1, len);
  buffer[len] = '\0';

  if (tmp) free( tmp );

  return( buffer );

strlib_param_error:

  if (tmp) free( tmp );

  return(  (char *) NULL );

} // strlib_parameter 

/*---------------------------------------------------------------------------
NAME

    strlib_toupper --- converts all characters to upper case 

SYNOPSIS

   char * strlib_toupper ( char * s );

DESCRIPTION

  Converts all characters to upper case and returns pointer to input string.

RETURN VALUE

  Pointer to input string 

---------------------------------------------------------------------------*/
PUBLIC char * strlib_toupper ( char * s )
{ char * ps;

  if(s) {
    ps=s;
    while ( *ps ) { *ps=(char)toupper(*ps); ps++; }
  }

  return(s);

} // strlib_toupper

/*---------------------------------------------------------------------------
NAME

    strlib_tolower --- converts all characters to lower case

SYNOPSIS

   char * strlib_tolower ( char * s );

DESCRIPTION

  Converts all characters to lower case and returns pointer to input string.
       
RETURN VALUE
       
  Pointer to input string
       
---------------------------------------------------------------------------*/
PUBLIC char * strlib_tolower ( char * s )
{ char * ps;

  if (s) {
    ps=s;
    while ( *ps ) { *ps=(char)tolower(*ps); ps++; }
  }

  return(s);

} // strlib_tolower

/*---------------------------------------------------------------------------
NAME

        strlib_ncasecmp ---  strncasecmp

SYNOPSIS

       int strlib_ncasecmp(const char *s1, const char *s2, size_t n);


DESCRIPTION
       The function compares the first n characters ot the two
       strings s1  and s2,  ignoring  the  case of the characters.
       It returns an integer less than, equal to, or greater than
       zero if s1 is less  than, matches, or is greater than s2.
       It can be used instead of the function strncasecmp if this
       function is not available.

       NULL pointers are treated as empty strings.

RETURN VALUE
       The function returns an integer less than, equal to, or
       greater than  zero.

---------------------------------------------------------------------------*/
PUBLIC int strlib_ncasecmp(const char *s1, const char *s2, size_t n)
{ int value;
  size_t s1len, s2len;
  char *_s1, *_s2;
  register unsigned int i;

  if (!s1) s1="";
  if (!s2) s2="";

  s1len = strlen(s1);
  s1len = (s1len<n)?s1len:n;
  _s1 = (char *) malloc( s1len+1 );
  for (i=0;i<s1len;i++) _s1[i]=(char)tolower(s1[i]);
  _s1[s1len]='\0';

  s2len = strlen(s2);
  s2len = (s2len<n)?s2len:n;
  _s2 = (char *) malloc( s2len+1 );
  for (i=0;i<s2len;i++) _s2[i]=(char)tolower(s2[i]);
  _s2[s2len]='\0';

  value = strcmp(_s1,_s2);

  free(_s2);
  free(_s1);

  return(value);

} /* strlib_ncasecmp */

/*---------------------------------------------------------------------------
NAME

      strlib_tok_r --- split a string into tokens 

SYNOPSIS

      char *strlib_tok_r(char *s, const char *sep, char **lasts);


DESCRIPTION

      The `strlib_tok_r' function is used to isolate sequential tokens in a 
      NULL-terminated string `*s'. It replaces the string.h function 
      `strtok_r' when it is not available. 

      These tokens are delimited in the string by at least one of the 
      characters in `*sep'. The first time that `strlib_tok_r' is called, 
      `*s' must be specified; subsequent calls, wishing to obtain further 
      tokens from the same string, must pass a NULL pointer instead. The 
      separator string, `*sep', must be supplied each time and may change 
      between calls.

      The  `strlib_tok_r'  function  returns  a pointer to the beginning of 
      each subsequent token in the string, after replacing the separator 
      character itself with a null character. When no more tokens remain, a 
      NULL pointer is returned. The placeholder `*lasts' must be supplied by 
      the caller and must not be modified.

      The `strlib_tok_r' function has the same behavior as `strlib_tok', 
      except that a pointer to placeholder `*LASTS' must be supplied by 
      the caller.

RETURN VALUE

     `strlib_tok_r' returns a pointer to the next token, or `NULL' if no 
     more tokens can be found.

---------------------------------------------------------------------------*/
PUBLIC char *strlib_tok_r(char *s, const char *sep, char **lasts)
{ char *pstart,*ps,*retval=(char*) NULL;

  if (s) {
    /* skip starting separators */
    for (pstart=s;*pstart;pstart++) {
      if (!(strchr(sep,(int) *pstart))) {
        retval=pstart;
        break;
      }
    }
  } else {
    pstart=(lasts)?*lasts:(char*)NULL;
    retval=pstart;
  }
  
  *lasts=(char*)NULL;
  if (pstart) {
    for (ps=pstart;*ps;ps++) {
      if (strchr(sep,(int) *ps)) {
        *ps='\0';
        *lasts = ps+1;
        break;
      }
    }
  }

  return( retval );
  
} /* strlib_tok_r */

/*---------------------------------------------------------------------------
NAME

      strlib_tok --- split a string into tokens

SYNOPSIS

      char *strlib_tok(char *s, const char *sep);


DESCRIPTION

      The `strlib_tok_r' function is used to isolate sequential tokens in a
      NULL-terminated string `*s'. It replaces the string.h function `strtok'
      when it is not available.

      These tokens are delimited in the string by at least one of the
      characters in `*sep'. The first time that `strlib_tok_r' is called,
      `*s' must be specified; subsequent calls, wishing to obtain further
      tokens from the same string, must pass a NULL pointer instead. The
      separator string, `*sep', must be supplied each time and may change
      between calls.

      The  `strlib_tok'  function  returns  a pointer to the beginning of
      each subsequent token in the string, after replacing the separator
      character itself with a null character. When no more tokens remain, a
      NULL pointer is returned. 

      The `strlib_tok' function has the same behavior as `strlib_tok_r', 
      except that the placeholder `*LASTS' cannot be specified. It is kept 
      in an internal static buffer for subsequent calls and consequently the 
      function is not thread safe.

RETURN VALUE

     `strlib_tok' returns a pointer to the next token, or `NULL' if no
     more tokens can be found.

---------------------------------------------------------------------------*/
PUBLIC char *strlib_tok(char *s, const char *sep)
{ static char *lasts;

  return( strlib_tok_r(s, sep, &lasts) );

} /* strlib_tok */

/*---------------------------------------------------------------------------
NAME

     strlib_newstr --- copy input string into new allocated memory

SYNOPSIS

     char *strlib_newstr( const char *string );

DESCRIPTION

     Makes a copy of the input string into new allocated memory. The memory 
     must be released with free( ) when it is not needed any more.

RETURN VALUE

     Pointer to allocated string or NULL in case of error.

---------------------------------------------------------------------------*/
PUBLIC char *strlib_newstr( const char *string )
{ char *new;

  if ( new=malloc( strlen(string)+1 ) )
    strcpy(new,string);
  return(new);

} /* strlib_newstr */