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