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 a copy of a string without leading whitespaces (ltrim), trailing whitespaces (rtrim),
12 * or both leading and trailing whitespaces (trim).
16 #include "xbt/sysdep.h"
17 #include "xbt/str.h" /* headers of these functions */
20 /** @brief Strip whitespace (or other characters) from the end of a string.
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 :
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.
32 * @param s The string to strip.
33 * @param char_list A string which contains the characters you want to strip.
35 * @return If the specified is NULL the function returns NULL. Otherwise the
36 * function returns the string with whitespace stripped from the end.
39 rtrim(char* s, const char* char_list)
42 const char* __char_list = " \t\n\r\x0B";
43 char white_char[256] = {1,0};
50 white_char[(unsigned char)*__char_list++] = 1;
54 white_char[(unsigned char)*char_list++] = 1;
61 while(white_char[(unsigned char)*cur] && (cur >= s))
68 /** @brief Strip whitespace (or other characters) from the beginning of a string.
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 :
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.
80 * @param s The string to strip.
81 * @param char_list A string which contains the characters you want to strip.
83 * @return If the specified is NULL the function returns NULL. Otherwise the
84 * function returns the string with whitespace stripped from the beginning.
87 ltrim( char* s, const char* char_list)
90 const char* __char_list = " \t\n\r\x0B";
91 char white_char[256] = {1,0};
98 white_char[(unsigned char)*__char_list++] = 1;
102 white_char[(unsigned char)*char_list++] = 1;
106 while(*cur && white_char[(unsigned char)*cur])
109 return strcpy(s,cur);
112 /** @brief Strip whitespace (or other characters) from the end and the begining of a string.
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 :
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.
124 * @param s The string to strip.
125 * @param char_list A string which contains the characters you want to strip.
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.
131 trim(char* s, const char* char_list ){
136 return ltrim(rtrim(s,char_list),char_list);
140 long getline(char **buf, size_t *n, FILE *stream) {
145 *buf = xbt_malloc(512);
152 for (i=0; (ch = fgetc(stream)) != EOF; i++) {
155 *buf = xbt_realloc(*buf, *n += 512);
159 if ((*buf)[i] == '\n') {
167 *buf = xbt_realloc(*buf, *n += 1);
174 #endif /* HAVE_GETLINE */