]> icculus.org git repositories - divverent/nexuiz.git/blob - scmenu/source/util/altstring.qc
This was certainly the wrong solution to a problem I dont see.
[divverent/nexuiz.git] / scmenu / source / 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                 lOut = strcat( lOut, lLetter ); // no need for strzone since there are 16 buffers and we use 2 in the for
69         }
70
71         return String_Zone( lOut ); // return a strzoned string
72 };
73 #else
74 float( string pStr )    Util_GetAltStringCount =
75 {
76         return altstr_count( pStr );
77 };
78 string( string pStr, float pCount ) Util_GetAltStringItem =
79 {
80         return String_Zone( altstr_get( pStr, pCount ) );
81 };
82 #endif
83
84
85 /*
86 ===================
87 Util_SetAltStringItem
88 ===================
89 */
90 #ifdef UTILALTSTRING
91 string( string pAlt, float pIndex, string pSet ) Util_SetAltStringItem =
92 {
93         local float lCount;
94         local float lPosition;
95         local float lStart;
96         local float lLength;
97         local string lLetter;
98         local string lOut;
99
100         pSet = String_Zone( pSet );
101
102         pIndex = pIndex*2 + 1; // number of ' until item starts
103         lCount = lPosition = 0;
104         for( lLength = strlen( pAlt ) ; lPosition < lLength && lCount < pIndex ; lPosition = lPosition + 1 ) {
105                 lLetter = substring( pAlt, lPosition, 1 );
106
107                 if( lLetter == "'" )
108                         lCount = lCount + 1;
109                 else if( lLetter == "\\" )
110                         lPosition = lPosition + 1;
111         }
112
113         if( lCount != pIndex )
114                 return pAlt;
115
116         lStart = lPosition;
117         // find the end of it
118         for( ; lPosition < lLength ; lPosition++ ) {
119                 lLetter = substring( pAlt, lPosition, 1 );
120
121                 if( lLetter == "'" )
122                         break;
123                 else if( lLetter == "\\" )
124                         lPosition = lPosition + 1;
125         }
126
127         lOut = String_Substring( pAlt, 0, lStart );
128         pSet = Util_AltStringPrepare( String_Normal( pSet ) );
129         lOut = strcat( lOut, String_Normal( pSet ), substring( pAlt, lPosition, lLength - lPosition ) );
130
131         return String_Set( pAlt, lOut );
132 };
133 #else
134 string( string pAlt, float pIndex, string pSet ) Util_SetAltStringItem =
135 {
136         return String_Set( pAlt, altstr_set( pAlt, pIndex, pSet ) );
137 };
138 #endif
139
140 /*
141 ===================
142 Util_DelAltStringItem
143 ===================
144 */
145 string( string pAlt, float pIndex ) Util_DelAltStringItem =
146 {
147         local float lCount;
148         local float lPosition;
149         local float lStart;
150         local float lLength;
151         local string lLetter;
152         local string lOut;
153
154         pIndex = pIndex*2 + 1; // number of ' until item starts
155         lCount = lPosition = 0;
156         for( lLength = strlen( pAlt ) ; lPosition < lLength && lCount < pIndex ; lPosition = lPosition + 1 ) {
157                 lLetter = substring( pAlt, lPosition, 1 );
158
159                 if( lLetter == "'" )
160                         lCount = lCount + 1;
161                 else if( lLetter == "\\" )
162                         lPosition = lPosition + 1;
163         }
164
165         if( lCount != pIndex )
166                 return pAlt;
167
168         lStart = lPosition;
169         // find the end of it
170         for( ; lPosition < lLength ; lPosition++ ) {
171                 lLetter = substring( pAlt, lPosition, 1 );
172
173                 if( lLetter == "'" )
174                         break;
175                 else if( lLetter == "\\" )
176                         lPosition = lPosition + 1;
177         }
178
179         if( lStart > 0 )
180                 lOut = substring( pAlt, 0, lStart - 1 );
181         if( lPosition < lLength - 1)
182                 lOut = strcat( lOut, substring( pAlt, lPosition + 1, lLength - lPosition - 1) );
183
184         return String_Set( pAlt, lOut );
185 };
186
187 /*
188 ===================
189 Util_AltStringPrepare
190 ===================
191 */
192 #ifdef UTILALTSTRING
193 string( string pString ) Util_AltStringPrepare =
194 {
195         local string lOut, lChar;
196         local float lPos, lLength;
197
198         pString = String_Zone( pString );
199
200         lOut = "";
201         lLength = strlen( pString );
202         for( lPos = 0; lPos < lLength  ; lPos = lPos + 1 ) {
203                 lChar = substring( pString, lPos, 1 );
204                 if( lChar == "'" )
205                         lChar = "\\'";
206                 lOut = strcat( lOut, lChar );
207         }
208
209         String_Free( pString );
210         return String_Zone( lOut );
211 };
212 #else
213 string( string pString ) Util_AltStringPrepare =
214 {
215         return String_Zone( altstr_prepare( pString ) );
216 };
217 #endif
218
219 /*
220 ===================
221 Util_AltStringPush
222 ===================
223 */
224 string( string pAlt, string pPush ) Util_AltStringPush =
225 {
226         return String_Set( pAlt, strcat( "'", String_Normal( Util_AltStringPrepare( pPush ) ), "'", pAlt ) );
227 };
228
229 /*
230 ===================
231 Util_GetAltStringTop
232 ===================
233 */
234 string( string pAlt ) Util_GetAltStringTop =
235 {
236         return Util_GetAltStringItem( pAlt, 0 );
237 };
238
239 /*
240 ===================
241 Util_AltStringPop
242 ===================
243 */
244 string( string pAlt ) Util_AltStringPop =
245 {
246         local float lPos, lCount, lLength;
247         local string lChar;
248
249         lCount = 0;
250         lLength = strlen( pAlt );
251         for( lPos = 0 ; lPos < lLength && lCount < 2 ; lPos++) {
252                 lChar = substring( pAlt, lPos, 1 );
253                 if( lChar == "\\" )
254                         lPos++;
255                 else if( lChar == "'" )
256                         lCount++;
257         }
258
259         return String_Set( pAlt, substring( pAlt, lPos, lLength - lPos ) ); // no - 1 because this time we dont break after the '
260 };