Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ac798bfca7337374af5b57406d316407e03e399d
[simgrid.git] / src / xbt / xbt_str.c
1
2 /* xbt_str.c - various helping functions to deal with strings               */
3
4 /* Copyright (C) 2005-2007 Malek Cherier, Martin Quinson.                   */
5 /* All rights reserved.                                                     */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. 
9  */
10
11 /* Returns a copy of a string without leading whitespaces (ltrim), trailing whitespaces (rtrim), 
12  * or both leading and trailing whitespaces (trim).
13  */
14   
15 #include "xbt/misc.h"
16 #include "xbt/sysdep.h"
17 #include "xbt/str.h" /* headers of these functions */
18 #include "portable.h"
19
20 /**  @brief Strip whitespace (or other characters) from the end of a string.
21  *
22  * The function rtrim() returns a string with whitespace stripped from the end of s. 
23  * By default (without the second parameter char_list), rtrim() will strip these characters :
24  *      
25  *      - " "           (ASCII 32       (0x20)) space. 
26  *      - "\t"          (ASCII 9        (0x09)) tab. 
27  *      - "\n"          (ASCII 10       (0x0A)) line feed. 
28  *      - "\r"          (ASCII 13       (0x0D)) carriage return. 
29  *      - "\0"          (ASCII 0        (0x00)) NULL. 
30  *      - "\x0B"        (ASCII 11       (0x0B)) vertical tab. 
31  *
32  * @param s The string to strip.
33  * @param char_list A string which contains the characters you want to strip.
34  *
35  * @return If the specified is NULL the function returns NULL. Otherwise the
36  * function returns the string with whitespace stripped from the end.
37  */
38 char*
39 rtrim(char* s, const char* char_list)
40 {
41         char* cur = s;
42         const char* __char_list = " \t\n\r\x0B";
43         char white_char[256] = {1,0};
44         
45         if(!s)
46                 return NULL;
47
48         if(!char_list){
49                 while(*__char_list) {
50                         white_char[(unsigned char)*__char_list++] = 1;
51                 }
52         }else{
53                 while(*char_list) {
54                         white_char[(unsigned char)*char_list++] = 1;
55                 }
56         }
57
58         while(*cur)
59                 ++cur;
60
61         while(white_char[(unsigned char)*cur] && (cur >= s))
62                 --cur;
63
64         *++cur = '\0';
65         return s;
66 }
67
68 /**  @brief Strip whitespace (or other characters) from the beginning of a string.
69  *
70  * The function ltrim() returns a string with whitespace stripped from the beginning of s. 
71  * By default (without the second parameter char_list), ltrim() will strip these characters :
72  *      
73  *      - " "           (ASCII 32       (0x20)) space. 
74  *      - "\t"          (ASCII 9        (0x09)) tab. 
75  *      - "\n"          (ASCII 10       (0x0A)) line feed. 
76  *      - "\r"          (ASCII 13       (0x0D)) carriage return. 
77  *      - "\0"          (ASCII 0        (0x00)) NULL. 
78  *      - "\x0B"        (ASCII 11       (0x0B)) vertical tab. 
79  *
80  * @param s The string to strip.
81  * @param char_list A string which contains the characters you want to strip.
82  *
83  * @return If the specified is NULL the function returns NULL. Otherwise the
84  * function returns the string with whitespace stripped from the beginning.
85  */
86 char*
87 ltrim( char* s, const char* char_list)
88 {
89         char* cur = s;
90         const char* __char_list = " \t\n\r\x0B";
91         char white_char[256] = {1,0};
92         
93         if(!s)
94                 return NULL;
95
96         if(!char_list){
97                 while(*__char_list) {
98                         white_char[(unsigned char)*__char_list++] = 1;
99                 }
100         }else{
101                 while(*char_list) {
102                         white_char[(unsigned char)*char_list++] = 1;
103                 }
104         }
105
106         while(*cur && white_char[(unsigned char)*cur])
107                 ++cur;
108
109         return strcpy(s,cur);
110 }
111
112 /**  @brief Strip whitespace (or other characters) from the end and the begining of a string.
113  *
114  * The function trim() returns a string with whitespace stripped from the end and the begining of s. 
115  * By default (without the second parameter char_list), trim() will strip these characters :
116  *      
117  *      - " "           (ASCII 32       (0x20)) space. 
118  *      - "\t"          (ASCII 9        (0x09)) tab. 
119  *      - "\n"          (ASCII 10       (0x0A)) line feed. 
120  *      - "\r"          (ASCII 13       (0x0D)) carriage return. 
121  *      - "\0"          (ASCII 0        (0x00)) NULL. 
122  *      - "\x0B"        (ASCII 11       (0x0B)) vertical tab. 
123  *
124  * @param s The string to strip.
125  * @param char_list A string which contains the characters you want to strip.
126  *
127  * @return If the specified is NULL the function returns NULL. Otherwise the
128  * function returns the string with whitespace stripped from the end and the begining.
129  */
130 char* 
131 trim(char* s, const char* char_list ){
132         
133         if(!s)
134                 return NULL;
135                 
136     return ltrim(rtrim(s,char_list),char_list);
137 }
138    
139 #ifndef HAVE_GETLINE
140 long getline(char **buf, size_t *n, FILE *stream) {
141    
142    int i, ch;
143    
144    if (!*buf) {
145      *buf = xbt_malloc(512);
146      *n = 512;
147    }
148    
149    if (feof(stream))
150      return (ssize_t)-1;
151    
152    for (i=0; (ch = fgetc(stream)) != EOF; i++)  {
153         
154       if (i >= (*n) + 1)
155         *buf = xbt_realloc(*buf, *n += 512);
156         
157       (*buf)[i] = ch;
158         
159       if ((*buf)[i] == '\n')  {
160          i++;
161          (*buf)[i] = '\0';
162          break;
163       }      
164    }
165       
166    if (i == *n) 
167      *buf = xbt_realloc(*buf, *n += 1);
168    
169    (*buf)[i] = '\0';
170
171    return (ssize_t)i;
172 }
173
174 #endif /* HAVE_GETLINE */