Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
06a7e558feae09d9b1d370434aeead7f3a7e8048
[simgrid.git] / src / nws_portability / Include / strutil.h
1 /* $Id$ */
2
3
4 #ifndef STRUTIL_H
5 #define STRUTIL_H
6
7
8 /*
9 ** This module defines some useful routines for maniputing strings.
10 */
11
12 #include <string.h>     /* strlen() strncpy() */
13 #include <sys/types.h>  /* size_t */
14
15
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
20
21 /*
22 ** Replaces the characters in #string# with their lower- or upper-case
23 ** equivalents.  #toWhatCase# specifies whether the result to be ENTIRELY
24 ** UPPER, entirely lower, iNITIAL lOWER, or Initial Upper.
25 */
26 typedef enum {ALL_LOWER, ALL_UPPER, INITIAL_LOWER, INITIAL_UPPER} CaseTypes;
27 void
28 strcase(char *string,
29         CaseTypes toWhatCase);
30
31
32 /*
33 ** Returns 1 or 0 depending on whether or not the first #len# characters of
34 ** #string# matches #pattern#, which may contain wildcard characters (*).
35 */
36 int
37 strnmatch(const char *string,
38           const char *pattern,
39           size_t len);
40 #define strmatch(string,pattern) strnmatch(string, pattern, strlen(string))
41
42
43 /*
44 ** Copies a "token" -- a series of characters deliminated by one of the chars
45 ** in #delim# -- from #source# to the #len#-long string #dest#.  Terminates
46 ** #dest# with a null character.  Returns the position within #source# of the
47 ** character after the token in #end#.  Skips any leading delimiters in
48 ** #source#.  Returns 0 if the end of source is reached without finding any
49 ** non-delimiter characters, 1 otherwise.
50 */
51 int
52 strntok(char *dest,
53         const char *source,
54         int len,
55         const char *delim,
56         const char **end);
57 #define GETTOK(dest,source,delim,end) strntok(dest,source,sizeof(dest),delim,end)
58 #define GETWORD(dest,source,end) GETTOK(dest,source," \t\n",end)
59
60
61 /*
62  * Define strnlen in case there is no such a function in the library
63  * (sometimes there is not the definition in the include so we define it
64  * anyway).
65  */
66 size_t
67 strnlen(const char *s, size_t maxlen);
68
69 /*
70 ** Calls strncpy() passing #dest#, #src#, and #len#, then places a terminating
71 ** character in the last (len - 1) byte of #dest#.
72 */
73 #define zstrncpy(dest,src,len) \
74   do {strncpy(dest, src, len); dest[len - 1] = '\0'; if (1) break;} while (0)
75 #define SAFESTRCPY(dest,src) zstrncpy(dest, src, sizeof(dest))
76
77
78 /*
79 ** Catenates the #count#-long set of source strings into #dest#.  Copies at
80 ** most #len# characters, including the null terminator.
81 */
82 int
83 vstrncpy(char *dest,
84          size_t len,
85          int count,
86          ...);
87
88
89 #ifdef __cplusplus
90 }
91 #endif
92
93
94 #endif