NativeEnumerated.c vars NULL init and check
[com/asn1c.git] / libasn1fix / asn1fix_cstring.c
1 #include "asn1fix_internal.h"
2
3 struct _cstring_pattern {
4         char *start;
5         size_t length;
6 };
7 static int _asn1f_cstring_find_line_pattern(char *s, struct _cstring_pattern *);
8
9 int
10 asn1f_fix_cstring(arg_t *arg) {
11         asn1p_expr_t *expr = arg->expr;
12         int r_value = 0;
13
14         if(expr->value && expr->value->type == ATV_STRING) {
15                 struct _cstring_pattern cp;
16                 char *buf = (char *)expr->value->value.string.buf;
17                 int buflen = expr->value->value.string.size;
18                 int start = 0;
19
20                 DEBUG("(%s) for line %d", expr->Identifier, expr->_lineno);
21
22                 while(_asn1f_cstring_find_line_pattern(buf + start, &cp)) {
23                         assert(cp.length);
24                         memmove(cp.start, cp.start + cp.length,
25                                 buflen - ((cp.start + cp.length) - buf));
26                         buflen -= cp.length;
27                         start = cp.start - buf;
28                         buf[buflen] = '\0';
29                 }
30         }
31
32         return r_value;
33 }
34
35 /*
36  * If a string has a newline, the tabulation and spaces before and
37  * after it must be eliminated.
38  */
39 static int
40 _asn1f_cstring_find_line_pattern(char *s, struct _cstring_pattern *cp) {
41         int newline_found = 0;
42
43         cp->start = NULL;
44
45         for(;;s++) {
46                 switch(*s) {
47                 case '\r': case '\n':
48                         newline_found = 1;
49                         /* Fall through */
50                 case ' ': case '\t':
51                         if(cp->start == NULL)
52                                 cp->start = s;
53                         continue;
54                 case '\0':
55                 default:
56                         if(newline_found) {
57                                 cp->length = (size_t)(s - cp->start);
58                                 return 1;
59                         }
60
61                         cp->start = NULL;
62                         if(*s == '\0')
63                                 break;
64                         continue;
65                 }
66                 break;
67         }
68
69         return 0;
70 }
71
72