blob: b8a38833b38e6522974c42ecb12924e18aa949ad [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001#include "asn1fix_internal.h"
2
3struct _cstring_pattern {
4 char *start;
5 int length;
6};
7static int _asn1f_cstring_find_line_pattern(char *s, struct _cstring_pattern *);
8
9int
10asn1f_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 = expr->value->value.string.buf;
17 int buflen = expr->value->value.string.size;
18 int start = 0;
19
20 DEBUG("%s(%s) for line %d", __func__,
21 expr->Identifier, expr->_lineno);
22
23 while(_asn1f_cstring_find_line_pattern(buf + start, &cp)) {
24 assert(cp.length);
25 memmove(cp.start, cp.start + cp.length,
26 buflen - ((cp.start + cp.length) - buf));
27 buflen -= cp.length;
28 start = cp.start - buf;
29 buf[buflen] = '\0';
30 }
31 }
32
33 return r_value;
34}
35
36/*
37 * If a string has a newline, the tabulation and spaces before and
38 * after it must be eliminated.
39 */
40static int
41_asn1f_cstring_find_line_pattern(char *s, struct _cstring_pattern *cp) {
42 int newline_found = 0;
43
44 cp->start = NULL;
45
46 for(;;s++) {
47 switch(*s) {
48 case '\r': case '\n':
49 newline_found = 1;
50 /* Fall through */
51 case ' ': case '\t':
52 if(cp->start == NULL)
53 cp->start = s;
54 continue;
55 case '\0':
56 default:
57 if(newline_found) {
58 cp->length = s - cp->start;
59 return 1;
60 }
61
62 cp->start = NULL;
63 if(*s == '\0')
64 break;
65 continue;
66 }
67 break;
68 }
69
70 return 0;
71}
72
73