2 /* xbt_str.c - various helping functions to deal with strings */
4 /* Copyright (C) 2005-2007 Malek Cherier, Martin Quinson. */
5 /* All rights reserved. */
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.
11 /* Returns the string without leading whitespaces (xbt_str_ltrim),
12 * trailing whitespaces (xbt_str_rtrim),
13 * or both leading and trailing whitespaces (xbt_str_trim).
14 * (in-place modification of the string)
18 #include "xbt/sysdep.h"
19 #include "xbt/str.h" /* headers of these functions */
22 /** @brief Strip whitespace (or other characters) from the end of a string.
24 * This function returns a string with whitespace stripped from the end of s.
25 * By default (without the second parameter char_list), xbt_str_rtrim() will strip these characters :
27 * - " " (ASCII 32 (0x20)) space.
28 * - "\t" (ASCII 9 (0x09)) tab.
29 * - "\n" (ASCII 10 (0x0A)) line feed.
30 * - "\r" (ASCII 13 (0x0D)) carriage return.
31 * - "\0" (ASCII 0 (0x00)) NULL.
32 * - "\x0B" (ASCII 11 (0x0B)) vertical tab.
34 * @param s The string to strip.
35 * @param char_list A string which contains the characters you want to strip.
37 * @return If the specified is NULL the function returns NULL. Otherwise the
38 * function returns the string with whitespace stripped from the end.
41 xbt_str_rtrim(char* s, const char* char_list)
44 const char* __char_list = " \t\n\r\x0B";
45 char white_char[256] = {1,0};
52 white_char[(unsigned char)*__char_list++] = 1;
56 white_char[(unsigned char)*char_list++] = 1;
63 while(white_char[(unsigned char)*cur] && (cur >= s))
70 /** @brief Strip whitespace (or other characters) from the beginning of a string.
72 * This function returns a string with whitespace stripped from the beginning of s.
73 * By default (without the second parameter char_list), xbt_str_ltrim() will strip these characters :
75 * - " " (ASCII 32 (0x20)) space.
76 * - "\t" (ASCII 9 (0x09)) tab.
77 * - "\n" (ASCII 10 (0x0A)) line feed.
78 * - "\r" (ASCII 13 (0x0D)) carriage return.
79 * - "\0" (ASCII 0 (0x00)) NULL.
80 * - "\x0B" (ASCII 11 (0x0B)) vertical tab.
82 * @param s The string to strip.
83 * @param char_list A string which contains the characters you want to strip.
85 * @return If the specified is NULL the function returns NULL. Otherwise the
86 * function returns the string with whitespace stripped from the beginning.
89 xbt_str_ltrim( char* s, const char* char_list)
92 const char* __char_list = " \t\n\r\x0B";
93 char white_char[256] = {1,0};
100 white_char[(unsigned char)*__char_list++] = 1;
104 white_char[(unsigned char)*char_list++] = 1;
108 while(*cur && white_char[(unsigned char)*cur])
111 return memmove(s,cur, strlen(cur));
114 /** @brief Strip whitespace (or other characters) from the end and the begining of a string.
116 * This returns a string with whitespace stripped from the end and the begining of s.
117 * By default (without the second parameter char_list), xbt_str_trim() will strip these characters :
119 * - " " (ASCII 32 (0x20)) space.
120 * - "\t" (ASCII 9 (0x09)) tab.
121 * - "\n" (ASCII 10 (0x0A)) line feed.
122 * - "\r" (ASCII 13 (0x0D)) carriage return.
123 * - "\0" (ASCII 0 (0x00)) NULL.
124 * - "\x0B" (ASCII 11 (0x0B)) vertical tab.
126 * @param s The string to strip.
127 * @param char_list A string which contains the characters you want to strip.
129 * @return If the specified is NULL the function returns NULL. Otherwise the
130 * function returns the string with whitespace stripped from the end and the begining.
133 xbt_str_trim(char* s, const char* char_list ){
138 return xbt_str_ltrim(xbt_str_rtrim(s,char_list),char_list);
141 /** @brief Replace double whitespaces (but no other characters) from the string.
143 * The function modifies the string so that each time that several spaces appear,
144 * they are replaced by a single space. It will only do so for spaces (ASCII 32, 0x20).
146 * @param s The string to strip. Modified in place.
150 xbt_str_strip_spaces(char *s) {
186 long getline(char **buf, size_t *n, FILE *stream) {
191 *buf = xbt_malloc(512);
198 for (i=0; (ch = fgetc(stream)) != EOF; i++) {
201 *buf = xbt_realloc(*buf, *n += 512);
205 if ((*buf)[i] == '\n') {
213 *buf = xbt_realloc(*buf, *n += 1);
220 #endif /* HAVE_GETLINE */