/[resiprocate]/main/resip/stack/test/testConvertRaw.cxx
ViewVC logotype

Contents of /main/resip/stack/test/testConvertRaw.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5948 - (show annotations) (download)
Fri Feb 17 15:30:16 2006 UTC (13 years, 9 months ago) by dworley
File MIME type: text/plain
File size: 5732 byte(s)
Setting more svn: properties, and adding EOLs to the ends of files
that need it.

1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <sys/types.h>
4 #include <unistd.h>
5 #include <fcntl.h>
6 #include <ctype.h>
7 #include <string>
8
9 const size_t indent = 4;
10
11 class ArGen
12 {
13 public:
14 ArGen(const char *arname = 0,
15 const int arnum = 0,
16 size_t wrapCol = 72);
17 ~ArGen();
18 void add(const unsigned char c);
19 void add(const unsigned char * buf, size_t sz);
20 void end();
21 int outputDef(int fd);
22 private:
23 void addRaw(const unsigned char c);
24 void addRaw(const char* str);
25 std::string code;
26 size_t mColumn;
27 size_t mNcols;
28 };
29
30 static std::string
31 toString(int v )
32 {
33 std::string s;
34 bool b = false;
35 for( int i = 1000000; i > 0 ; i/=10)
36 {
37 int p = v/i;
38 if (b = ( b || p || ( i == 1)))
39 s += '0' + p;
40 v = v % i;
41 }
42 return s;
43 }
44
45 ArGen::ArGen(const char *arname, int arnum, size_t wrapCol) :
46 mColumn(0),
47 mNcols(wrapCol)
48
49 {
50 if (!arname)
51 arname = "data";
52
53 code += "const char ";
54 code += arname;
55 if (arnum) code += toString(arnum);
56 code += "[] = {\n";
57 }
58
59
60
61 void
62 ArGen::addRaw(const unsigned char c)
63 {
64 code += c;
65 ++mColumn;
66 }
67
68 void ArGen::addRaw(const char* s)
69 {
70 for(size_t i = 0 ; s[i] ; ++i)
71 addRaw(s[i]);
72 }
73
74 /// bulk of formatting work done here.
75 void
76 ArGen::add(const unsigned char c)
77 {
78 // take care of 1st part of line
79 if (!mColumn)
80 addRaw(" \"");
81
82 // take care of switching modes and opening or closing any running
83 // quotations.
84
85 if (isalnum(c) || c == ' ' ||
86 ( ispunct(c) && c != '\\' && c != '"' ))
87 {
88 addRaw(c);
89 }
90 else
91 {
92 static const char hexv[] ="0123456789abcdef";
93 addRaw("\\x");
94 addRaw(hexv[(c&0xf0)>>8]);
95 addRaw(hexv[(c&0x0f)]);
96 }
97 if (mColumn > mNcols || c == 0x0a)
98 {
99 addRaw("\"\n");
100 mColumn = 0;
101 }
102 }
103
104 void
105 ArGen::add(const unsigned char *c , size_t sz)
106 {
107 for(size_t i = 0 ; i < sz ; ++i)
108 add(c[i]);
109 }
110
111 ArGen::~ArGen() {}
112
113
114 void
115 ArGen::end()
116 {
117
118 code += '\n';
119 code += "};\n";
120 }
121
122 int
123 ArGen::outputDef(int fd)
124 {
125 return write(fd,code.data(),code.size());
126 }
127
128 void
129 dumpraw(const char *path, const char * basename, int arNo)
130 {
131
132 if (access(path,R_OK))
133 {
134 perror(path);
135 return;
136 }
137 int fd = open(path,O_RDONLY);
138 if (fd < 0)
139 {
140 perror(path);
141 return;
142 }
143 int hdr = 0;
144 int s;
145 char unsigned buf[16 * 1024];
146 const int bsz = sizeof(buf)/sizeof(*buf);
147 const int nCols = 8;
148 int col = 0;
149 ArGen cg(basename,arNo);
150 while((s=read(fd,buf,bsz)) > 0)
151 {
152 cg.add(buf,s);
153 }
154 cg.end();
155
156 if (s < 0)
157 {
158 perror(path);
159 return;
160 }
161 cg.outputDef(1);
162
163 return;
164 }
165
166 const char * pName = 0;
167 int
168 usage()
169 {
170 printf("usage: %s [-n arraybasename] file ...\n",pName);
171 }
172 int
173 main(int argc, char * argv[])
174 {
175 pName = *argv;
176 const char * basename = 0;
177 int basearg = 1;
178 if (argc < 2) return usage();
179 if (argv[1][0] == '-' &&
180 argv[1][1] == 'n')
181 {
182 if (argc < 4) return usage();
183 basename = argv[2];
184 basearg = 3;
185 }
186 for (int i = basearg ; i < argc ; ++i )
187 dumpraw(argv[i],basename,i-basearg);
188 return 0;
189 }
190
191 /* ====================================================================
192 * The Vovida Software License, Version 1.0
193 *
194 * Copyright (c) 2000-2005 Vovida Networks, Inc. All rights reserved.
195 *
196 * Redistribution and use in source and binary forms, with or without
197 * modification, are permitted provided that the following conditions
198 * are met:
199 *
200 * 1. Redistributions of source code must retain the above copyright
201 * notice, this list of conditions and the following disclaimer.
202 *
203 * 2. Redistributions in binary form must reproduce the above copyright
204 * notice, this list of conditions and the following disclaimer in
205 * the documentation and/or other materials provided with the
206 * distribution.
207 *
208 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
209 * and "Vovida Open Communication Application Library (VOCAL)" must
210 * not be used to endorse or promote products derived from this
211 * software without prior written permission. For written
212 * permission, please contact vocal@vovida.org.
213 *
214 * 4. Products derived from this software may not be called "VOCAL", nor
215 * may "VOCAL" appear in their name, without prior written
216 * permission of Vovida Networks, Inc.
217 *
218 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
219 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
220 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
221 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
222 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
223 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
224 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
225 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
226 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
227 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
228 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
229 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
230 * DAMAGE.
231 *
232 * ====================================================================
233 *
234 * This software consists of voluntary contributions made by Vovida
235 * Networks, Inc. and many individuals on behalf of Vovida Networks,
236 * Inc. For more information on Vovida Networks, Inc., please see
237 * <http://www.vovida.org/>.
238 *
239 */
240

Properties

Name Value
svn:eol-style native
svn:mime-type text/plain

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27