1 |
#include <stdio.h> |
2 |
#include <stdlib.h> |
3 |
#include <stdarg.h> |
4 |
#include <sys/types.h> |
5 |
#include <unistd.h> |
6 |
#include <glib.h> |
7 |
|
8 |
GString * |
9 |
libmt_read_line (int sock) |
10 |
{ |
11 |
char c; |
12 |
GString *gs; |
13 |
|
14 |
gs = g_string_sized_new (30); /* put another initial value if 30 is not optimal */ |
15 |
c = '\0'; |
16 |
while (c != '\n') |
17 |
{ |
18 |
int l = read (sock, &c, sizeof (char)); |
19 |
if (l != sizeof (char)) |
20 |
{ |
21 |
g_string_free (gs, TRUE); |
22 |
return (NULL); |
23 |
} |
24 |
gs = g_string_append_c (gs, c); |
25 |
} |
26 |
return (gs); |
27 |
} |
28 |
|
29 |
char ** |
30 |
libmt_convert_line_to_array (GString * gs) |
31 |
{ |
32 |
char **a; |
33 |
int p0, p1, p2, n; |
34 |
p1 = 0; |
35 |
p2 = 0; |
36 |
n = 0; |
37 |
/* Count the number of separators */ |
38 |
for (p1 = 0; gs->str[p1]; p1++) |
39 |
{ |
40 |
if (gs->str[p1] == ' ') |
41 |
{ |
42 |
n++; |
43 |
/* Test here if the separator is escaped */ |
44 |
/* Notice that the following can deal with "\\\\\\ " strings */ |
45 |
p2 = p1 - 1; |
46 |
while (p2 >= 0) |
47 |
{ |
48 |
if (gs->str[p2] != '\\') |
49 |
break; |
50 |
p2--; |
51 |
} |
52 |
p2++; |
53 |
if ((p1 - p2) % 2) |
54 |
n--; |
55 |
} |
56 |
} |
57 |
|
58 |
a = g_malloc ((n + 2) * sizeof (char *)); |
59 |
|
60 |
p0 = 0; |
61 |
n = 0; |
62 |
for (p1 = 0; gs->str[p1]; p1++) |
63 |
{ |
64 |
if (gs->str[p1] == ' ') |
65 |
{ |
66 |
p2 = p1 - 1; |
67 |
while (p2 >= p0) |
68 |
{ |
69 |
if (gs->str[p2] != '\\') |
70 |
break; |
71 |
p2--; |
72 |
} |
73 |
p2++; |
74 |
if (!((p1 - p2) % 2)) |
75 |
{ |
76 |
a[n] = g_strndup (&(gs->str[p0]), p1 - p0); |
77 |
n++; |
78 |
p0 = p1 + 1; |
79 |
} |
80 |
} |
81 |
} |
82 |
a[n++] = g_strdup (&(gs->str[p0])); |
83 |
a[n] = NULL; |
84 |
|
85 |
/* remove backslashs */ |
86 |
for (n = 0; a[n]; n++) |
87 |
{ |
88 |
p1 = 0; |
89 |
for (p2 = 0; a[n][p2]; p2++) |
90 |
{ |
91 |
if (a[n][p2] == '\\') |
92 |
{ |
93 |
p2++; |
94 |
} |
95 |
a[n][p1++] = a[n][p2]; |
96 |
if (!a[n][p2]) |
97 |
break; |
98 |
} |
99 |
} |
100 |
return (a); |
101 |
} |
102 |
|
103 |
GString * |
104 |
libmt_convert_array_to_line (char **a) |
105 |
{ |
106 |
GString *gs = g_string_sized_new (30); |
107 |
int n = 0; |
108 |
|
109 |
for (n = 0; a[n]; n++) |
110 |
{ |
111 |
int i; |
112 |
for (i = 0; a[n][i]; i++) |
113 |
{ |
114 |
if ((a[n][i] == ' ') || (a[n][i] == '\\')) |
115 |
gs = g_string_append_c (gs, '\\'); |
116 |
gs = g_string_append_c (gs, a[n][i]); |
117 |
} |
118 |
gs = g_string_append_c (gs, ' '); |
119 |
} |
120 |
return (gs); |
121 |
} |
122 |
|
123 |
GString * |
124 |
libmt_new_message_string (int n, ...) |
125 |
{ |
126 |
va_list args; |
127 |
char **a; |
128 |
int i; |
129 |
GString *gs; |
130 |
|
131 |
va_start (args, n); |
132 |
|
133 |
a = g_malloc ((n + 1) * sizeof (char *)); |
134 |
for (i = 0; i < n; i++) |
135 |
{ |
136 |
a[i] = va_arg (args, char *); |
137 |
} |
138 |
va_end (args); |
139 |
a[n] = NULL; |
140 |
|
141 |
gs = libmt_convert_array_to_line (a); |
142 |
g_free (a); |
143 |
return (gs); |
144 |
} |