summaryrefslogtreecommitdiff
path: root/legacy/ecore/src/e_str.c
blob: 16d570ffc620b9a822510b228ee326c8c65023b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "e_str.h"
#include "e_mem.h"

/* string manipulation conveience calls */

void
e_string_free_list(char **slist, int num)
{
   int                 i;

   for (i = 0; i < num; i++)
      FREE(slist[i]);
   FREE(slist);
}

char               *
e_string_dup(char *str)
{
   if (!str)
      return NULL;
   return strdup(str);
}

int
e_string_cmp(char *s1, char *s2)
{
   if ((!s1) || (!s2))
      return 0;
   if (!strcmp(s1, s2))
      return 1;
   return 0;
}

int
e_string_case_cmp(char *s1, char *s2)
{
   if ((!s1) || (!s2))
      return 0;
   if (!strcasecmp(s1, s2))
      return 1;
   return 0;
}

int
e_string_length(char *str)
{
   return strlen(str);
}

void
e_string_clear(char *str)
{
   str[0] = 0;
}

void
e_string_cat(char *str, char *cat)
{
   strcat(str, cat);
}

void
e_string_cat_n(char *str, char *cat, int start, int len)
{
   strncat(str, &(cat[start]), len);
}

char               *
e_string_escape(char *str)
{
   char                buf[8192];
   int                 i, j, len;

   e_string_clear(buf);
   len = e_string_length(str);
   for (i = 0, j = 0; i < len; i++)
     {
	switch (str[i])
	  {
	  case ' ':
	  case '"':
	  case '\'':
	  case '\\':
	  case '(':
	  case ')':
	  case '[':
	  case ']':
	  case '$':
	  case '`':
	  case '~':
	  case '!':
	  case '#':
	  case '&':
	  case '*':
	  case '?':
	  case '|':
	  case ';':
	  case '<':
	  case '>':
	     buf[j++] = '\\';
	     buf[j++] = str[i];
	     break;
	  default:
	     buf[j++] = str[i];
	     break;
	  }
     }
   buf[j] = 0;
   return e_string_dup(buf);
}

char               *
e_string_build(char *format, char **rep_list, int rep_num)
{
   char                buf[16384], *s;
   int                 i;

   e_string_clear(buf);
   s = format;
   i = 0;
   while (*s)
     {
	if (s[0] == '%')
	  {
	     if (s[1] == '%')
		e_string_cat(buf, "%");
	     else
	       {
		  int                 j;

		  for (j = 0; j < rep_num; j += 2)
		    {
		       if (s[1] == rep_list[j][0])
			 {
			    char               *ss;

			    ss = e_string_escape(rep_list[j + 1]);
			    e_string_cat(buf, ss);
			    FREE(ss);
			    break;
			 }
		    }
	       }
	     s++;
	  }
	else
	   strncat(buf, s, 1);
	s++;
     }
   return e_string_dup(buf);
}