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...
[simgrid.git] / src / xbt / xbt_str.c
index f259b6a..11a50b8 100644 (file)
@@ -1,12 +1,17 @@
 
-/* Copyright (C) 2007 Malek Cherier. All rights reserved.                  */
+/* xbt_str.c - various helping functions to deal with strings               */
+
+/* Copyright (C) 2005-2007 Malek Cherier, Martin Quinson.                   */
+/* All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
  * 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"
@@ -16,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. 
@@ -33,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";
@@ -64,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. 
@@ -81,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";
@@ -108,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. 
@@ -125,11 +130,91 @@ 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
+long getline(char **buf, size_t *n, FILE *stream) {
+   
+   int i, ch;
+   
+   if (!*buf) {
+     *buf = xbt_malloc(512);
+     *n = 512;
+   }
+   
+   if (feof(stream))
+     return (ssize_t)-1;
+   
+   for (i=0; (ch = fgetc(stream)) != EOF; i++)  {
+       
+      if (i >= (*n) + 1)
+       *buf = xbt_realloc(*buf, *n += 512);
+       
+      (*buf)[i] = ch;
+       
+      if ((*buf)[i] == '\n')  {
+        i++;
+        (*buf)[i] = '\0';
+        break;
+      }      
+   }
+      
+   if (i == *n) 
+     *buf = xbt_realloc(*buf, *n += 1);
+   
+   (*buf)[i] = '\0';
+
+   return (ssize_t)i;
+}
+
+#endif /* HAVE_GETLINE */