ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/vendor/pxys2-2.1.0/pxyservd/src/tokenizer.c
Revision: 3253
Committed: Wed Apr 2 20:46:18 2014 UTC (10 years ago) by michael
Content type: text/x-csrc
File size: 2799 byte(s)
Log Message:
- Imported pxys2-2.1.0

File Contents

# Content
1 /* Copyright (C) 2001-2003 Stephane Thiell <mbuna@undernet.org>
2 *
3 * This file is part of pxyservd (from pxys)
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 */
20 #define RCSID "$Id: tokenizer.c,v 1.2 2006/09/04 12:48:08 spale Exp $"
21
22 /* #define TOKENIZER_TEST */
23
24 #define MAXTOKSIZE 64
25
26 #ifndef TOKENIZER_TEST
27 # ifdef HAVE_CONFIG_H
28 # include "config.h"
29 # endif
30 #endif
31
32 #include "tokenizer.h"
33 #include <stdio.h>
34 #include <stdlib.h>
35
36 #include <libbcl/memory.h>
37
38 static toktab ttab;
39
40 /* *MUST* be called before the first call of tokenize! */
41 void
42 tokenizer_init()
43 {
44 ttab.tok = (char **)tokenizer_malloc(sizeof(char *) * MAXTOKSIZE);
45 ttab.size = 0;
46 }
47
48 void
49 tokenizer_destroy()
50 {
51 tokenizer_free((char **)ttab.tok);
52 ttab.size = -1;
53 }
54
55 toktabptr
56 tokenize(char *s, int maxtok)
57 {
58 register int c, tog = 1, i = 0;
59 char * p;
60
61 /* Sanity checks */
62 if (NULL == s)
63 return NULL;
64
65 if (maxtok > MAXTOKSIZE)
66 maxtok = MAXTOKSIZE;
67
68 /* Fill the array */
69 for (p = s; i < maxtok && (c = (unsigned char)*p); p++)
70 {
71 if (c == ' ') /* if (isspace(c)) */
72 {
73 tog = 1;
74 *p = 0;
75 }
76 else if (tog)
77 {
78 tog = 0;
79 ttab.tok[i++] = p;
80 }
81 }
82
83 /* Set array size */
84 ttab.size = i;
85
86 return (i ? &ttab : NULL);
87 }
88
89 char*
90 untokenize(toktabptr ttab, int start)
91 {
92 char *p, *ret = NULL;
93 int i;
94
95 for (i = start + 1; i < ttab->size; i++)
96 {
97 p = ttab->tok[i];
98 while (*--p == '\0')
99 *p = ' ';
100 }
101
102 if (ttab->size > start)
103 ret = ttab->tok[start];
104
105 return ret;
106 }
107
108
109 #ifdef TOKENIZER_TEST
110 #define LINEINJECT_SIZE 50000000
111 int
112 main(void)
113 {
114 int i;
115 toktabptr t;
116 const char tpl[] = "AeFq0 P C0QwM :Hey abc def g h ijklmmmnop qq r s ttt uuuuuuuuuuuuuv vvvvv wwww x@yzzz\n";
117 char buf[512];
118
119 tokenizer_init();
120
121 for (i=0; i<LINEINJECT_SIZE; i++)
122 {
123 strcpy(buf, tpl);
124 tokenize(buf, 10);
125 }
126
127 strcpy(buf, tpl);
128
129 printf("%s", buf);
130
131 t = tokenize(buf, 10);
132
133 for (i=0; i<t->size; i++)
134 printf("tok[%d]=%s\n", i, t->tok[i]);
135
136 return(0);
137 }
138 #endif
139
140
141 /* tokenizer.c */