Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 118 lines (108 sloc) 2.803 kB
fd65cee @hzeller o add basic zita filter.
authored
1 // -------------------------------------------------------------------------
2 //
3 // Copyright (C) 2009-2010 Fons Adriaensen <fons@linuxaudio.org>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 //
19 // -------------------------------------------------------------------------
20
21
22 #include <ctype.h>
23
24
25 #define SQUOTE 39
26 #define DQUOTE 34
27 #define BSLASH 92
28
29
30 // See the header file for a description.
31
32 int sstring (const char *srce, char *dest, int size)
33 {
34 int i, j;
35 unsigned int c, ef, qf;
36
37 if (size < 0) return 0;
38 i = j = ef = qf = 0;
39 while (true)
40 {
41 if (j == size)
42 {
43 // Non-zero char in last position of dest, return error.
44 dest [0] = 0;
45 return 0;
46 }
47 c = srce [i++];
48 // Tabs are converted to spaces.
49 if (isblank (c)) c = ' ';
50 if (iscntrl (c))
51 {
52 // Control character. If within quotes or escaped return error,
53 // else terminate scanning.
54 if (qf | ef)
55 {
56 dest [0] = 0;
57 return 0;
58 }
59 dest [j] = 0;
60 return --i;
61 }
62 if (ef)
63 {
64 // Escaped character. If tab, insert space, else
65 // insert the character as is. Reset escape flag.
66 dest [j++] = c;
67 ef = 0;
68 continue;
69 }
70 if (c == BSLASH)
71 {
72 // An '\'. If in single quotes, treat as normal
73 // character, else set the escape flag.
74 if (qf == SQUOTE) dest [j++] = c;
75 else ef = c;
76 continue;
77 }
78 if ((c == SQUOTE) || (c == DQUOTE))
79 {
80 // Single or double quote. If we have a leading quote,
81 // terminate scanning if this one matches it, or return
82 // error if not. Else set as leading quote.
83 if (c == qf)
84 {
85 dest [j] = 0;
86 return i;
87 }
88 if (qf || j)
89 {
90 dest [0] = 0;
91 return 0;
92 }
93 qf = c;
94 continue;
95 }
96 if (c == ' ')
97 {
98 // Skip if leading space, insert if quoted, otherwise
99 // terminate scanning.
100 if (qf)
101 {
102 dest [j++] = ' ';
103 continue;
104 }
105 if (j)
106 {
107 dest [j++] = 0;
108 return --i;
109 }
110 continue;
111 }
112 // Normal character.
113 dest [j++] = c;
114 }
115 return 0;
116 }
117
Something went wrong with that request. Please try again.