1fba7cef9a065f7a0b946061518427d12ab92d83
[alexxy/gromacs.git] / src / gromacs / gmxlib / string2.c
1 /*
2  *
3  *                This source code is part of
4  *
5  *                 G   R   O   M   A   C   S
6  *
7  *          GROningen MAchine for Chemical Simulations
8  *
9  *                        VERSION 3.2.0
10  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12  * Copyright (c) 2001-2004, The GROMACS development team,
13  * check out http://www.gromacs.org for more information.
14
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * If you want to redistribute modifications, please consider that
21  * scientific software is very special. Version control is crucial -
22  * bugs must be traceable. We will be happy to consider code for
23  * inclusion in the official distribution, but derived work must not
24  * be called official GROMACS. Details are found in the README & COPYING
25  * files - if they are missing, get the official version at www.gromacs.org.
26  *
27  * To help us fund GROMACS development, we humbly ask that you cite
28  * the papers on the package - you can find them in the top README file.
29  *
30  * For more info, check our website at http://www.gromacs.org
31  *
32  * And Hey:
33  * GROningen Mixture of Alchemy and Childrens' Stories
34  */
35 /* This file is completely threadsafe - keep it that way! */
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39 #include "gromacs/utility/gmx_header_config.h"
40
41 #ifdef GMX_CRAY_XT3
42 #undef HAVE_PWD_H
43 #endif
44
45 #include <stdio.h>
46 #include <ctype.h>
47 #include <stdlib.h>
48 #include <errno.h>
49 #include <sys/types.h>
50 #include <time.h>
51
52 #ifdef HAVE_SYS_TIME_H
53 #include <sys/time.h>
54 #endif
55
56 #ifdef HAVE_UNISTD_H
57 #include <unistd.h>
58 #endif
59
60 #ifdef HAVE_PWD_H
61 #include <pwd.h>
62 #endif
63 #include <time.h>
64 #include <assert.h>
65
66 #include "typedefs.h"
67 #include "smalloc.h"
68 #include "gmx_fatal.h"
69 #include "macros.h"
70 #include "string2.h"
71 #include "futil.h"
72
73 int continuing(char *s)
74 {
75     int sl;
76     assert(s);
77
78     rtrim(s);
79     sl = strlen(s);
80     if ((sl > 0) && (s[sl-1] == CONTINUE))
81     {
82         s[sl-1] = 0;
83         return TRUE;
84     }
85     else
86     {
87         return FALSE;
88     }
89 }
90
91
92
93 char *fgets2(char *line, int n, FILE *stream)
94 {
95     char *c;
96     if (fgets(line, n, stream) == NULL)
97     {
98         return NULL;
99     }
100     if ((c = strchr(line, '\n')) != NULL)
101     {
102         *c = '\0';
103     }
104     else
105     {
106         /* A line not ending in a newline can only occur at the end of a file,
107          * or because of n being too small.
108          * Since both cases occur very infrequently, we can check for EOF.
109          */
110         if (!gmx_eof(stream))
111         {
112             gmx_fatal(FARGS, "An input file contains a line longer than %d characters, while the buffer passed to fgets2 has size %d. The line starts with: '%20.20s'", n, n, line);
113         }
114     }
115     if ((c = strchr(line, '\r')) != NULL)
116     {
117         *c = '\0';
118     }
119
120     return line;
121 }
122
123 void strip_comment (char *line)
124 {
125     char *c;
126
127     if (!line)
128     {
129         return;
130     }
131
132     /* search for a comment mark and replace it by a zero */
133     if ((c = strchr(line, COMMENTSIGN)) != NULL)
134     {
135         (*c) = 0;
136     }
137 }
138
139 void upstring (char *str)
140 {
141     int i;
142
143     for (i = 0; (i < (int)strlen(str)); i++)
144     {
145         str[i] = toupper(str[i]);
146     }
147 }
148
149 void ltrim (char *str)
150 {
151     char *tr;
152     int   i, c;
153
154     if (NULL == str)
155     {
156         return;
157     }
158
159     c = 0;
160     while (('\0' != str[c]) && isspace(str[c]))
161     {
162         c++;
163     }
164     if (c > 0)
165     {
166         for (i = c; ('\0' != str[i]); i++)
167         {
168             str[i-c] = str[i];
169         }
170         str[i-c] = '\0';
171     }
172 }
173
174 void rtrim (char *str)
175 {
176     int nul;
177
178     if (NULL == str)
179     {
180         return;
181     }
182
183     nul = strlen(str)-1;
184     while ((nul > 0) && ((str[nul] == ' ') || (str[nul] == '\t')) )
185     {
186         str[nul] = '\0';
187         nul--;
188     }
189 }
190
191 void trim (char *str)
192 {
193     ltrim (str);
194     rtrim (str);
195 }
196
197 char *
198 gmx_ctime_r(const time_t *clock, char *buf, int n)
199 {
200     char tmpbuf[STRLEN];
201
202 #ifdef GMX_NATIVE_WINDOWS
203     /* Windows */
204     ctime_s( tmpbuf, STRLEN, clock );
205 #elif (defined(__sun))
206     /*Solaris*/
207     ctime_r(clock, tmpbuf, n);
208 #else
209     ctime_r(clock, tmpbuf);
210 #endif
211     strncpy(buf, tmpbuf, n-1);
212     buf[n-1] = '\0';
213
214     return buf;
215 }
216
217 void nice_header (FILE *out, const char *fn)
218 {
219     const char    *unk = "onbekend";
220     time_t         clock;
221     const char    *user = unk;
222     int            gh;
223 #ifdef HAVE_PWD_H
224     uid_t          uid;
225 #else
226     int            uid;
227 #endif
228     char           buf[256] = "";
229     char           timebuf[STRLEN];
230 #ifdef HAVE_PWD_H
231     struct passwd *pw;
232 #endif
233
234     /* Print a nice header above the file */
235     time(&clock);
236     fprintf (out, "%c\n", COMMENTSIGN);
237     fprintf (out, "%c\tFile '%s' was generated\n", COMMENTSIGN, fn ? fn : unk);
238
239 #ifdef HAVE_PWD_H
240     uid  = getuid();
241     pw   = getpwuid(uid);
242     gh   = gethostname(buf, 255);
243     /* pw returns null on error (e.g. compute nodes lack /etc/passwd) */
244     user = pw ? pw->pw_name : unk;
245 #else
246     uid = 0;
247     gh  = -1;
248 #endif
249
250     gmx_ctime_r(&clock, timebuf, STRLEN);
251     fprintf (out, "%c\tBy user: %s (%d)\n", COMMENTSIGN,
252              user ? user : unk, (int) uid);
253     fprintf(out, "%c\tOn host: %s\n", COMMENTSIGN, (gh == 0) ? buf : unk);
254
255     fprintf (out, "%c\tAt date: %s", COMMENTSIGN, timebuf);
256     fprintf (out, "%c\n", COMMENTSIGN);
257 }
258
259
260 int gmx_strcasecmp_min(const char *str1, const char *str2)
261 {
262     char ch1, ch2;
263
264     do
265     {
266         do
267         {
268             ch1 = toupper(*(str1++));
269         }
270         while ((ch1 == '-') || (ch1 == '_'));
271         do
272         {
273             ch2 = toupper(*(str2++));
274         }
275         while ((ch2 == '-') || (ch2 == '_'));
276
277         if (ch1 != ch2)
278         {
279             return (ch1-ch2);
280         }
281     }
282     while (ch1);
283     return 0;
284 }
285
286 int gmx_strncasecmp_min(const char *str1, const char *str2, int n)
287 {
288     char  ch1, ch2;
289     char *stri1, *stri2;
290
291     stri1 = (char *)str1;
292     stri2 = (char *)str2;
293     do
294     {
295         do
296         {
297             ch1 = toupper(*(str1++));
298         }
299         while ((ch1 == '-') || (ch1 == '_'));
300         do
301         {
302             ch2 = toupper(*(str2++));
303         }
304         while ((ch2 == '-') || (ch2 == '_'));
305
306         if (ch1 != ch2)
307         {
308             return (ch1-ch2);
309         }
310     }
311     while (ch1 && (str1-stri1 < n) && (str2-stri2 < n));
312     return 0;
313 }
314
315 int gmx_strcasecmp(const char *str1, const char *str2)
316 {
317     char ch1, ch2;
318
319     do
320     {
321         ch1 = toupper(*(str1++));
322         ch2 = toupper(*(str2++));
323         if (ch1 != ch2)
324         {
325             return (ch1-ch2);
326         }
327     }
328     while (ch1);
329     return 0;
330 }
331
332 int gmx_strncasecmp(const char *str1, const char *str2, int n)
333 {
334     char ch1, ch2;
335
336     if (n == 0)
337     {
338         return 0;
339     }
340
341     do
342     {
343         ch1 = toupper(*(str1++));
344         ch2 = toupper(*(str2++));
345         if (ch1 != ch2)
346         {
347             return (ch1-ch2);
348         }
349         n--;
350     }
351     while (ch1 && n);
352     return 0;
353 }
354
355 char *gmx_strdup(const char *src)
356 {
357     char *dest;
358
359     snew(dest, strlen(src)+1);
360     strcpy(dest, src);
361
362     return dest;
363 }
364
365 char *
366 gmx_strndup(const char *src, int n)
367 {
368     int   len;
369     char *dest;
370
371     len = strlen(src);
372     if (len > n)
373     {
374         len = n;
375     }
376     snew(dest, len+1);
377     strncpy(dest, src, len);
378     dest[len] = 0;
379     return dest;
380 }
381
382 /* Magic hash init number for Dan J. Bernsteins algorithm.
383  * Do NOT use any other value unless you really know what you are doing.
384  */
385 const unsigned int
386     gmx_string_hash_init = 5381;
387
388
389 unsigned int
390 gmx_string_hash_func(const char *s, unsigned int hash_init)
391 {
392     int c;
393
394     while ((c = toupper(*s++)) != '\0')
395     {
396         if (isalnum(c))
397         {
398             hash_init = ((hash_init << 5) + hash_init) ^ c;            /* (hash * 33) xor c */
399         }
400     }
401     return hash_init;
402 }
403
404 int
405 gmx_wcmatch(const char *pattern, const char *str)
406 {
407     while (*pattern)
408     {
409         if (*pattern == '*')
410         {
411             /* Skip multiple wildcards in a sequence */
412             while (*pattern == '*' || *pattern == '?')
413             {
414                 ++pattern;
415                 /* For ?, we need to check that there are characters left
416                  * in str. */
417                 if (*pattern == '?')
418                 {
419                     if (*str == 0)
420                     {
421                         return GMX_NO_WCMATCH;
422                     }
423                     else
424                     {
425                         ++str;
426                     }
427                 }
428             }
429             /* If the pattern ends after the star, we have a match */
430             if (*pattern == 0)
431             {
432                 return 0;
433             }
434             /* Match the rest against each possible suffix of str */
435             while (*str)
436             {
437                 /* Only do the recursive call if the first character
438                  * matches. We don't have to worry about wildcards here,
439                  * since we have processed them above. */
440                 if (*pattern == *str)
441                 {
442                     int rc;
443                     /* Match the suffix, and return if a match or an error */
444                     rc = gmx_wcmatch(pattern, str);
445                     if (rc != GMX_NO_WCMATCH)
446                     {
447                         return rc;
448                     }
449                 }
450                 ++str;
451             }
452             /* If no suffix of str matches, we don't have a match */
453             return GMX_NO_WCMATCH;
454         }
455         else if ((*pattern == '?' && *str != 0) || *pattern == *str)
456         {
457             ++str;
458         }
459         else
460         {
461             return GMX_NO_WCMATCH;
462         }
463         ++pattern;
464     }
465     /* When the pattern runs out, we have a match if the string has ended. */
466     return (*str == 0) ? 0 : GMX_NO_WCMATCH;
467 }
468
469 char *wrap_lines(const char *buf, int line_width, int indent, gmx_bool bIndentFirst)
470 {
471     char    *b2;
472     int      i, i0, i2, j, b2len, lspace = 0, l2space = 0;
473     gmx_bool bFirst, bFitsOnLine;
474
475     /* characters are copied from buf to b2 with possible spaces changed
476      * into newlines and extra space added for indentation.
477      * i indexes buf (source buffer) and i2 indexes b2 (destination buffer)
478      * i0 points to the beginning of the current line (in buf, source)
479      * lspace and l2space point to the last space on the current line
480      * bFirst is set to prevent indentation of first line
481      * bFitsOnLine says if the first space occurred before line_width, if
482      * that is not the case, we have a word longer than line_width which
483      * will also not fit on the next line, so we might as well keep it on
484      * the current line (where it also won't fit, but looks better)
485      */
486
487     b2    = NULL;
488     b2len = strlen(buf)+1+indent;
489     snew(b2, b2len);
490     i0 = i2 = 0;
491     if (bIndentFirst)
492     {
493         for (i2 = 0; (i2 < indent); i2++)
494         {
495             b2[i2] = ' ';
496         }
497     }
498     bFirst = TRUE;
499     do
500     {
501         l2space = -1;
502         /* find the last space before end of line */
503         for (i = i0; ((i-i0 < line_width) || (l2space == -1)) && (buf[i]); i++)
504         {
505             b2[i2++] = buf[i];
506             /* remember the position of a space */
507             if (buf[i] == ' ')
508             {
509                 lspace  = i;
510                 l2space = i2-1;
511             }
512             /* if we have a newline before the line is full, reset counters */
513             if (buf[i] == '\n' && buf[i+1])
514             {
515                 i0     = i+1;
516                 b2len += indent;
517                 srenew(b2, b2len);
518                 /* add indentation after the newline */
519                 for (j = 0; (j < indent); j++)
520                 {
521                     b2[i2++] = ' ';
522                 }
523             }
524         }
525         /* If we are at the last newline, copy it */
526         if (buf[i] == '\n' && !buf[i+1])
527         {
528             b2[i2++] = buf[i++];
529         }
530         /* if we're not at the end of the string */
531         if (buf[i])
532         {
533             /* check if one word does not fit on the line */
534             bFitsOnLine = (i-i0 <= line_width);
535             /* reset line counters to just after the space */
536             i0 = lspace+1;
537             i2 = l2space+1;
538             /* if the words fit on the line, and we're beyond the indentation part */
539             if ( (bFitsOnLine) && (l2space >= indent) )
540             {
541                 /* start a new line */
542                 b2[l2space] = '\n';
543                 /* and add indentation */
544                 if (indent)
545                 {
546                     if (bFirst)
547                     {
548                         line_width -= indent;
549                         bFirst      = FALSE;
550                     }
551                     b2len += indent;
552                     srenew(b2, b2len);
553                     for (j = 0; (j < indent); j++)
554                     {
555                         b2[i2++] = ' ';
556                     }
557                     /* no extra spaces after indent; */
558                     while (buf[i0] == ' ')
559                     {
560                         i0++;
561                     }
562                 }
563             }
564         }
565     }
566     while (buf[i]);
567     b2[i2] = '\0';
568
569     return b2;
570 }
571
572 char **split(char sep, const char *str)
573 {
574     char **ptr = NULL;
575     int    n, nn, nptr = 0;
576
577     if (str == NULL)
578     {
579         return NULL;
580     }
581     nn = strlen(str);
582     for (n = 0; (n < nn); n++)
583     {
584         if (str[n] == sep)
585         {
586             nptr++;
587         }
588     }
589     snew(ptr, nptr+2);
590     nptr = 0;
591     while (*str != '\0')
592     {
593         while ((*str != '\0') && (*str == sep))
594         {
595             str++;
596         }
597         if (*str != '\0')
598         {
599             snew(ptr[nptr], 1+strlen(str));
600             n = 0;
601             while ((*str != '\0') && (*str != sep))
602             {
603                 ptr[nptr][n] = *str;
604                 str++;
605                 n++;
606             }
607             ptr[nptr][n] = '\0';
608             nptr++;
609         }
610     }
611     ptr[nptr] = NULL;
612
613     return ptr;
614 }
615
616
617 gmx_large_int_t
618 str_to_large_int_t(const char *str, char **endptr)
619 {
620     int              sign = 1;
621     gmx_large_int_t  val  = 0;
622     char             ch;
623     const char      *p;
624
625     p = str;
626     if (p == NULL)
627     {
628         *endptr = NULL;
629         return 0;
630     }
631
632     /* Strip off initial white space */
633     while (isspace(*p))
634     {
635         p++;
636     }
637     /* Conform to ISO C99 - return original pointer if string does not contain a number */
638     if (*str == '\0')
639     {
640         *endptr = (char *)str;
641     }
642
643     if (*p == '-')
644     {
645         p++;
646         sign *= -1;
647     }
648
649     while ( ((ch = *p) != '\0') && isdigit(ch) )
650     {
651         /* Important to add sign here, so we dont overflow in final multiplication */
652         ch  = (ch-'0')*sign;
653         val = val*10 + ch;
654         if (ch != val%10)
655         {
656             /* Some sort of overflow has occured, set endptr to original string */
657             *endptr = (char *)str;
658             errno   = ERANGE;
659             return(0);
660         }
661         p++;
662     }
663
664     *endptr = (char *)p;
665
666     return val;
667 }
668
669 char *gmx_strsep(char **stringp, const char *delim)
670 {
671     char *ret;
672     int   len = strlen(delim);
673     int   i, j = 0;
674     int   found = 0;
675
676     if (!*stringp)
677     {
678         return NULL;
679     }
680     ret = *stringp;
681     do
682     {
683         if ( (*stringp)[j] == '\0')
684         {
685             found    = 1;
686             *stringp = NULL;
687             break;
688         }
689         for (i = 0; i < len; i++)
690         {
691             if ( (*stringp)[j] == delim[i])
692             {
693                 (*stringp)[j] = '\0';
694                 *stringp      = *stringp+j+1;
695                 found         = 1;
696                 break;
697             }
698         }
699         j++;
700     }
701     while (!found);
702
703     return ret;
704 }