Line data Source code
1 : /*
2 : * GPAC - Multimedia Framework C SDK
3 : *
4 : * Authors: Jean Le Feuvre
5 : * Copyright (c) Telecom ParisTech 2000-2012
6 : * All rights reserved
7 : *
8 : * This file is part of GPAC / common tools sub-project
9 : *
10 : * GPAC is free software; you can redistribute it and/or modify
11 : * it under the terms of the GNU Lesser General Public License as published by
12 : * the Free Software Foundation; either version 2, or (at your option)
13 : * any later version.
14 : *
15 : * GPAC is distributed in the hope that it will be useful,
16 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 : * GNU Lesser General Public License for more details.
19 : *
20 : * You should have received a copy of the GNU Lesser General Public
21 : * License along with this library; see the file COPYING. If not, write to
22 : * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23 : *
24 : */
25 :
26 : #ifndef GPAC_DISABLE_CORE_TOOLS
27 :
28 : #include <gpac/token.h>
29 :
30 : static GFINLINE s32 gf_tok_is_char_in_set(const char TestChar, const char *TestSet)
31 : {
32 : u32 i, Len;
33 46736 : Len = (u32) strlen(TestSet);
34 128881 : for (i=0; i<Len; i++) {
35 136394 : if (TestChar == TestSet[i]) return 1;
36 : }
37 : return 0;
38 : }
39 :
40 : GF_EXPORT
41 4903 : s32 gf_token_get(const char *Buffer, s32 Start, const char *Separator, char *Container, s32 ContainerSize)
42 : {
43 : s32 i, start, end, Len;
44 :
45 4903 : Len = (s32) strlen( Buffer );
46 7733 : for (i=Start; i<Len; i++ ) {
47 15226 : if (!gf_tok_is_char_in_set(Buffer[i], Separator)) break;
48 : }
49 : start = i;
50 4903 : if (i == Len) return( -1 );
51 :
52 34440 : for (i=start; i<Len; i++) {
53 78246 : if (gf_tok_is_char_in_set(Buffer[i], Separator)) break;
54 : }
55 : end = i-1;
56 :
57 39223 : for (i=start; ((i<=end) && (i< start+(ContainerSize-1))); i++) {
58 34440 : Container[i-start] = Buffer[i];
59 : }
60 4783 : Container[i-start] = 0;
61 :
62 4783 : return (end+1);
63 : }
64 :
65 : GF_EXPORT
66 31 : s32 gf_token_get_strip(const char *Buffer, s32 Start, const char *Separator, const char *strip_set, char *Container, s32 ContainerSize)
67 : {
68 : u32 i, k, len;
69 31 : s32 res = gf_token_get(Buffer, Start, Separator, Container, ContainerSize);
70 31 : if (!strip_set || (res<0)) return res;
71 : i=k=0;
72 31 : len = (u32) strlen(Container);
73 31 : while (strchr(strip_set, Container[i]) ) i++;
74 62 : while (len && strchr(strip_set, Container[len]) ) {
75 31 : Container[len]=0;
76 31 : len--;
77 : }
78 280 : while (k+i<=len) {
79 249 : Container[k] = Container[k+i];
80 249 : k++;
81 : }
82 31 : Container[k] = 0;
83 31 : return res;
84 : }
85 :
86 :
87 : GF_EXPORT
88 11836 : s32 gf_token_get_line(const char *Buffer, u32 Start, u32 Size, char *LineBuffer, u32 LineBufferSize)
89 : {
90 : u32 offset;
91 : s32 i, End, Total;
92 11836 : LineBuffer[0] = 0;
93 11836 : if (Start >= Size) return -1;
94 :
95 : offset = 2;
96 11817 : End = gf_token_find(Buffer, Start, Size, "\r\n");
97 11817 : if (End<0) {
98 266 : End = gf_token_find(Buffer, Start, Size, "\r");
99 266 : if (End<0) End = gf_token_find(Buffer, Start, Size, "\n");
100 266 : if (End < 0) return -1;
101 : offset = 1;
102 : }
103 :
104 11817 : Total = End - Start + offset;
105 11817 : if ((u32) Total >= LineBufferSize) Total = LineBufferSize;
106 11817 : for (i=0; i<Total; i++) LineBuffer[i] = Buffer[Start+i];
107 11817 : LineBuffer[i] = 0;
108 11817 : return (End + offset);
109 : }
110 :
111 : GF_EXPORT
112 13480 : s32 gf_token_find(const char *Buffer, u32 Start, u32 Size, const char *Pattern)
113 : {
114 : u32 i, j, flag;
115 : s32 Len;
116 :
117 13480 : if (Start >= Size) return -1;
118 :
119 13480 : Len = (u32) strlen(Pattern);
120 13480 : if ( Len <= 0 ) return -1;
121 13480 : if (Size - Start < (u32) Len) return -1;
122 :
123 886622 : for (i=Start; i<= Size-Len; i++) {
124 : flag = 0;
125 49234 : for (j=0; j< (u32) Len; j++) {
126 935856 : if (Buffer[i+j] != Pattern[j]) {
127 : flag = 1;
128 : break;
129 : }
130 : }
131 : //found
132 899570 : if (!flag) return i;
133 : }
134 : return -1;
135 : }
136 :
137 : #endif
|