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