1
0

string_escape.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * string_escape.c
  3. */
  4. #include "private.h"
  5. #include <stdlib.h>
  6. #include <string.h>
  7. /*--------------------------------------------------------- */
  8. char *
  9. lub_string_decode(const char *string)
  10. {
  11. const char *s = string;
  12. char *res, *p;
  13. int esc = 0;
  14. if (!string)
  15. return NULL;
  16. /* Allocate enough memory for result */
  17. p = res = malloc(strlen(string) + 1);
  18. while (*s) {
  19. if (!esc) {
  20. if ('\\' == *s)
  21. esc = 1;
  22. else
  23. *p = *s;
  24. } else {
  25. switch (*s) {
  26. case 'r':
  27. case 'n':
  28. *p = '\n';
  29. break;
  30. case 't':
  31. *p = '\t';
  32. break;
  33. default:
  34. *p = *s;
  35. break;
  36. }
  37. esc = 0;
  38. }
  39. if (!esc)
  40. p++;
  41. s++;
  42. }
  43. *p = '\0';
  44. /* Optimize the memory allocated for result */
  45. p = lub_string_dup(res);
  46. free(res);
  47. return p;
  48. }
  49. /*--------------------------------------------------------- */