]> icculus.org git repositories - divverent/nexuiz.git/blob - data/menuqc/util/altstring.qc
give menu source its own directory
[divverent/nexuiz.git] / data / menuqc / util / altstring.qc
1 // NG-Menu
2 // util/altstring.qc
3
4 #ifdef UTILALTSTRING
5 /*
6 ===================
7 Util_GetAltStringCount
8 ===================
9 */
10 float( string pStr )    Util_GetAltStringCount =
11 {
12         local float lLength;
13         local float lCount;
14         local float lPosition;
15         local string lLetter;
16
17         lPosition = lCount = 0;
18         for( lLength = strlen( pStr ) ; lPosition < lLength ; lPosition++ ) {
19                 lLetter = substring( pStr, lPosition, 1 );
20                 if( lLetter == "'" )
21                         lCount++;
22                 else if( lLetter == "\\" )
23                         lPosition++;
24         }
25
26         return floor( lCount / 2 );
27 };
28
29 /*
30 ===================
31 Util_GetAltStringItem
32 ===================
33 */
34 string( string pStr, float pCount ) Util_GetAltStringItem =
35 {
36         local float lCount;
37         local float lPosition;
38         local float lLength;
39         local string lLetter;
40         local string lOut;
41
42         pCount = pCount*2 + 1; // number of ' until item starts
43         lCount = lPosition = 0;
44         for( lLength = strlen( pStr ) ; lPosition < lLength && lCount < pCount ; lPosition++ ) {
45                 lLetter = substring( pStr, lPosition, 1 );
46
47                 if( lLetter == "'" )
48                         lCount = lCount + 1;
49                 else if( lLetter == "\\" )
50                         lPosition++;
51         }
52
53         if( lCount != pCount )
54                 return String_Create();
55
56         for( lOut = "" ; lPosition < lLength ; lPosition++ ) {
57                 lLetter = substring( pStr, lPosition, 1 );
58
59                 if( lLetter == "'" )
60                         break;
61                 else if( lLetter == "\\" ) {
62                         lPosition++;
63                         if( lPosition >= lLength )
64                                 break;
65                         lLetter = substring( pStr, lPosition, 1 );
66                 }
67
68                 // no need for strzone since there are 16 buffers and we use 2 in the for
69                 lOut = strcat( lOut, lLetter );
70         }
71
72         return String_Zone( lOut ); // return a strzoned string
73 };
74 #else
75 float( string pStr )    Util_GetAltStringCount =
76 {
77         return altstr_count( pStr );
78 };
79 string( string pStr, float pCount ) Util_GetAltStringItem =
80 {
81         return String_Zone( altstr_get( pStr, pCount ) );
82 };
83 #endif
84
85
86 /*
87 ===================
88 Util_SetAltStringItem
89 ===================
90 */
91 #ifdef UTILALTSTRING
92 string( string pAlt, float pIndex, string pSet ) Util_SetAltStringItem =
93 {
94         local float lCount;
95         local float lPosition;
96         local float lStart;
97         local float lLength;
98         local string lLetter;
99         local string lOut;
100
101         pSet = String_Zone( pSet );
102
103         pIndex = pIndex*2 + 1; // number of ' until item starts
104         lCount = lPosition = 0;
105         for( lLength = strlen( pAlt ) ; lPosition < lLength && lCount < pIndex ; lPosition = lPosition + 1 ) {
106                 lLetter = substring( pAlt, lPosition, 1 );
107
108                 if( lLetter == "'" )
109                         lCount = lCount + 1;
110                 else if( lLetter == "\\" )
111                         lPosition = lPosition + 1;
112         }
113
114         if( lCount != pIndex )
115                 return pAlt;
116
117         lStart = lPosition;
118         // find the end of it
119         for( ; lPosition < lLength ; lPosition++ ) {
120                 lLetter = substring( pAlt, lPosition, 1 );
121
122                 if( lLetter == "'" )
123                         break;
124                 else if( lLetter == "\\" )
125                         lPosition = lPosition + 1;
126         }
127
128         lOut = String_Substring( pAlt, 0, lStart );
129         pSet = String_Normal( Util_AltStringPrepare( String_Normal( pSet ) ) );
130         lOut = strcat( lOut, pSet, substring( pAlt, lPosition, lLength - lPosition ) );
131
132         return String_Set( pAlt, lOut );
133 };
134 #else
135 string( string pAlt, float pIndex, string pSet ) Util_SetAltStringItem =
136 {
137         return String_Set( pAlt, altstr_set( pAlt, pIndex, pSet ) );
138 };
139 #endif
140
141 /*
142 ===================
143 Util_DelAltStringItem
144 ===================
145 */
146 string( string pAlt, float pIndex ) Util_DelAltStringItem =
147 {
148         local float lCount;
149         local float lPosition;
150         local float lStart;
151         local float lLength;
152         local string lLetter;
153         local string lOut;
154
155         pIndex = pIndex*2 + 1; // number of ' until item starts
156         lCount = lPosition = 0;
157         for( lLength = strlen( pAlt ) ; lPosition < lLength && lCount < pIndex ; lPosition = lPosition + 1 ) {
158                 lLetter = substring( pAlt, lPosition, 1 );
159
160                 if( lLetter == "'" )
161                         lCount = lCount + 1;
162                 else if( lLetter == "\\" )
163                         lPosition = lPosition + 1;
164         }
165
166         if( lCount != pIndex )
167                 return pAlt;
168
169         lStart = lPosition;
170         // find the end of it
171         for( ; lPosition < lLength ; lPosition++ ) {
172                 lLetter = substring( pAlt, lPosition, 1 );
173
174                 if( lLetter == "'" )
175                         break;
176                 else if( lLetter == "\\" )
177                         lPosition = lPosition + 1;
178         }
179
180         if( lStart > 0 )
181                 lOut = substring( pAlt, 0, lStart - 1 );
182         if( lPosition < lLength - 1)
183                 lOut = strcat( lOut, substring( pAlt, lPosition + 1, lLength - lPosition - 1) );
184
185         return String_Set( pAlt, lOut );
186 };
187
188 /*
189 ===================
190 Util_InsAltStringItem
191 ===================
192 */
193 #ifdef UTILALTSTRING
194 string( string pAlt, float pIndex, string pSet ) Util_InsAltStringItem =
195 {
196         local float lCount;
197         local float lPosition;
198         local float lLength;
199         local string lLetter;
200         local string lOut;
201
202         pSet = String_Zone( pSet );
203
204         pIndex = pIndex*2 + 2; // number of ' until item starts
205         lCount = lPosition = 0;
206         for( lLength = strlen( pAlt ) ; lPosition < lLength && lCount < pIndex ; lPosition = lPosition + 1 ) {
207                 lLetter = substring( pAlt, lPosition, 1 );
208
209                 if( lLetter == "'" )
210                         lCount = lCount + 1;
211                 else if( lLetter == "\\" )
212                         lPosition = lPosition + 1;
213         }
214
215         if( lCount != pIndex )
216                 return pAlt;
217
218         lOut = String_Substring( pAlt, 0, lPosition );
219         pSet = String_Normal( Util_AltStringPrepare( String_Normal( pSet ) ) );
220         lOut = strcat( lOut, "'", pSet, "'", substring( pAlt, lPosition, lLength - lPosition ) );
221
222         return String_Set( pAlt, lOut );
223 };
224 #else
225 string( string pAlt, float pIndex, string pSet ) Util_InsAltStringItem =
226 {
227         return String_Set( pAlt, altstr_ins( pAlt, pIndex, pSet ) );
228 };
229 #endif
230
231 /*
232 ===================
233 Util_AltStringPrepare
234 ===================
235 */
236 #ifdef UTILALTSTRING
237 string( string pString ) Util_AltStringPrepare =
238 {
239         local string lOut, lChar;
240         local float lPos, lLength;
241
242         pString = String_Zone( pString );
243
244         lOut = "";
245         lLength = strlen( pString );
246         for( lPos = 0; lPos < lLength  ; lPos = lPos + 1 ) {
247                 lChar = substring( pString, lPos, 1 );
248                 if( lChar == "'" )
249                         lChar = "\\'";
250                 lOut = strcat( lOut, lChar );
251         }
252
253         String_Free( pString );
254         return String_Zone( lOut );
255 };
256 #else
257 string( string pString ) Util_AltStringPrepare =
258 {
259         return String_Zone( altstr_prepare( pString ) );
260 };
261 #endif
262
263 /*
264 ===================
265 Util_AltStringPush
266 ===================
267 */
268 string( string pAlt, string pPush ) Util_AltStringPush =
269 {
270         return String_Set( pAlt, strcat( "'", altstr_prepare( pPush ), "'", pAlt ) );
271 };
272
273 /*
274 ===================
275 Util_AltStringPushBack
276 ===================
277 */
278 string( string pAlt, string pPush )     Util_AltStringPushBack =
279 {
280         return String_Set( pAlt, strcat( pAlt, "'", altstr_prepare( pPush ), "'" ) );
281 };
282
283 /*
284 ===================
285 Util_GetAltStringTop
286 ===================
287 */
288 string( string pAlt ) Util_GetAltStringTop =
289 {
290         return Util_GetAltStringItem( pAlt, 0 );
291 };
292
293 /*
294 ===================
295 Util_AltStringPop
296 ===================
297 */
298 string( string pAlt ) Util_AltStringPop =
299 {
300         local float lPos, lCount, lLength;
301         local string lChar;
302
303         lCount = 0;
304         lLength = strlen( pAlt );
305         for( lPos = 0 ; lPos < lLength && lCount < 2 ; lPos++) {
306                 lChar = substring( pAlt, lPos, 1 );
307                 if( lChar == "\\" )
308                         lPos++;
309                 else if( lChar == "'" )
310                         lCount++;
311         }
312
313         return String_Set( pAlt, substring( pAlt, lPos, lLength - lPos ) ); // no - 1 because this time we dont break after the '
314 };