Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
further cleanups in the xbt_str module: add a proper prefix to the exported functions...
authormquinson <mquinson@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Thu, 26 Apr 2007 10:49:45 +0000 (10:49 +0000)
committermquinson <mquinson@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Thu, 26 Apr 2007 10:49:45 +0000 (10:49 +0000)
git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@3455 48e7efb5-ca39-0410-a469-dd3cf9ba447f

include/xbt/str.h
src/xbt/xbt_str.c

index 6068247..e9259a4 100644 (file)
 
 SG_BEGIN_DECL()
 
+/* snprintf related functions */
 XBT_PUBLIC(int) asprintf  (char **ptr, const char *fmt, /*args*/ ...) _XBT_GNUC_PRINTF(2,3);
 XBT_PUBLIC(int) vasprintf (char **ptr, const char *fmt, va_list ap);
 XBT_PUBLIC(char*) bprintf   (const char*fmt, ...) _XBT_GNUC_PRINTF(1,2);
 
+/* the gettext function. It gets redefined here only if not yet available */
 #if defined(_WIN32) || !defined(__GNUC__)
 XBT_PUBLIC(long) getline(char **lineptr, size_t *n, FILE *stream);
 #endif
 
 /* Trim related functions */
-XBT_PUBLIC(char*) rtrim(char* s, const char* char_list);
-XBT_PUBLIC(char*) ltrim( char* s, const char* char_list);
-XBT_PUBLIC(char*) trim(char* s, const char* char_list);
+XBT_PUBLIC(char*) xbt_str_rtrim(char* s, const char* char_list);
+XBT_PUBLIC(char*) xbt_str_ltrim( char* s, const char* char_list);
+XBT_PUBLIC(char*) xbt_str_trim(char* s, const char* char_list);
+
+/* */
+XBT_PUBLIC(void) xbt_str_strip_spaces(char *);
+
 
 SG_END_DECL()
 
index ac798bf..11a50b8 100644 (file)
@@ -8,8 +8,10 @@
  * under the terms of the license (GNU LGPL) which comes with this package. 
  */
 
-/* Returns a copy of a string without leading whitespaces (ltrim), trailing whitespaces (rtrim), 
- * or both leading and trailing whitespaces (trim).
+/* Returns the string without leading whitespaces (xbt_str_ltrim), 
+ * trailing whitespaces (xbt_str_rtrim),
+ * or both leading and trailing whitespaces (xbt_str_trim). 
+ * (in-place modification of the string)
  */
   
 #include "xbt/misc.h"
@@ -19,8 +21,8 @@
 
 /**  @brief Strip whitespace (or other characters) from the end of a string.
  *
- * The function rtrim() returns a string with whitespace stripped from the end of s. 
- * By default (without the second parameter char_list), rtrim() will strip these characters :
+ * This function returns a string with whitespace stripped from the end of s. 
+ * By default (without the second parameter char_list), xbt_str_rtrim() will strip these characters :
  *     
  *     - " "           (ASCII 32       (0x20)) space. 
  *     - "\t"          (ASCII 9        (0x09)) tab. 
@@ -36,7 +38,7 @@
  * function returns the string with whitespace stripped from the end.
  */
 char*
-rtrim(char* s, const char* char_list)
+xbt_str_rtrim(char* s, const char* char_list)
 {
        char* cur = s;
        const char* __char_list = " \t\n\r\x0B";
@@ -67,8 +69,8 @@ rtrim(char* s, const char* char_list)
 
 /**  @brief Strip whitespace (or other characters) from the beginning of a string.
  *
- * The function ltrim() returns a string with whitespace stripped from the beginning of s. 
- * By default (without the second parameter char_list), ltrim() will strip these characters :
+ * This function returns a string with whitespace stripped from the beginning of s. 
+ * By default (without the second parameter char_list), xbt_str_ltrim() will strip these characters :
  *     
  *     - " "           (ASCII 32       (0x20)) space. 
  *     - "\t"          (ASCII 9        (0x09)) tab. 
@@ -84,7 +86,7 @@ rtrim(char* s, const char* char_list)
  * function returns the string with whitespace stripped from the beginning.
  */
 char*
-ltrim( char* s, const char* char_list)
+xbt_str_ltrim( char* s, const char* char_list)
 {
        char* cur = s;
        const char* __char_list = " \t\n\r\x0B";
@@ -111,8 +113,8 @@ ltrim( char* s, const char* char_list)
 
 /**  @brief Strip whitespace (or other characters) from the end and the begining of a string.
  *
- * The function trim() returns a string with whitespace stripped from the end and the begining of s. 
- * By default (without the second parameter char_list), trim() will strip these characters :
+ * This returns a string with whitespace stripped from the end and the begining of s. 
+ * By default (without the second parameter char_list), xbt_str_trim() will strip these characters :
  *     
  *     - " "           (ASCII 32       (0x20)) space. 
  *     - "\t"          (ASCII 9        (0x09)) tab. 
@@ -128,12 +130,56 @@ ltrim( char* s, const char* char_list)
  * function returns the string with whitespace stripped from the end and the begining.
  */
 char* 
-trim(char* s, const char* char_list ){
+xbt_str_trim(char* s, const char* char_list ){
        
        if(!s)
                return NULL;
                
-    return ltrim(rtrim(s,char_list),char_list);
+    return xbt_str_ltrim(xbt_str_rtrim(s,char_list),char_list);
+}
+
+/**  @brief Replace double whitespaces (but no other characters) from the string.
+ *
+ * The function modifies the string so that each time that several spaces appear,
+ * they are replaced by a single space. It will only do so for spaces (ASCII 32, 0x20). 
+ *
+ * @param s The string to strip. Modified in place.
+ *
+ */
+void
+xbt_str_strip_spaces(char *s) {
+  char *p = s;
+  int   e = 0;
+
+  if (!s)
+    return;
+
+  while (1) {
+    if (!*p)
+      goto end;
+    
+    if (*p != ' ')
+      break;
+    
+    p++;
+  }
+  
+  e = 1;
+  
+  do {
+    if (e)
+      *s++ = *p;
+    
+    if (!*++p)
+      goto end;
+    
+    if (e ^ (*p!=' '))
+      if ((e = !e))
+       *s++ = ' ';
+  } while (1);
+
+ end:
+  *s = '\0';
 }
    
 #ifndef HAVE_GETLINE