Explorar o código

Add string_escape.c

git-svn-id: https://klish.googlecode.com/svn/trunk@197 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev %!s(int64=13) %!d(string=hai) anos
pai
achega
a72d7ba242
Modificáronse 1 ficheiros con 56 adicións e 0 borrados
  1. 56 0
      lub/string/string_escape.c

+ 56 - 0
lub/string/string_escape.c

@@ -0,0 +1,56 @@
+/*
+ * string_escape.c
+ */
+#include "private.h"
+
+#include <stdlib.h>
+#include <string.h>
+/*--------------------------------------------------------- */
+char *
+lub_string_decode(const char *string)
+{
+    const char *s = string;
+    char *res, *p;
+    int esc = 0;
+
+    if (!string)
+        return NULL;
+
+    /* Allocate enough memory for result */
+    p = res = malloc(strlen(string) + 1);
+
+    while (*s) {
+        if (!esc) {
+            if ('\\' == *s)
+                esc = 1;
+            else
+                *p = *s;
+        } else {
+            switch (*s) {
+            case 'r':
+            case 'n':
+                *p = '\n';
+                break;
+            case 't':
+                *p = '\t';
+                break;
+            default:
+                *p = *s;
+                break;
+            }
+            esc = 0;
+        }
+        if (!esc)
+            p++;
+        s++;
+    }
+    *p = '\0';
+
+    /* Optimize the memory allocated for result */
+    p = lub_string_dup(res);
+    free(res);
+
+    return p;
+}
+
+/*--------------------------------------------------------- */