1 /*
2 * OpenBOR - http://www.chronocrash.com
3 * -----------------------------------------------------------------------
4 * All rights reserved. See LICENSE in OpenBOR root for license details.
5 *
6 * Copyright (c) 2004 - 2017 OpenBOR Team
7 */
8
9 // String
10 // 2017-04-26
11 // Caskey, Damon V.
12 //
13 // String manipulation and concatenation.
14
15 #include "scriptcommon.h"
16
17 //strinfirst(char string, char search_string);
openbor_strinfirst(ScriptVariant ** varlist,ScriptVariant ** pretvar,int paramCount)18 HRESULT openbor_strinfirst(ScriptVariant **varlist , ScriptVariant **pretvar, int paramCount)
19 {
20 char *tempstr = NULL;
21
22 if(paramCount < 2)
23 {
24 goto sif_error;
25 }
26
27 if(varlist[0]->vt != VT_STR || varlist[1]->vt != VT_STR)
28 {
29 printf("\n Error, strinfirst({string}, {search string}): Strinfirst must be passed valid {string} and {search string}. \n");
30 goto sif_error;
31 }
32
33 tempstr = strstr((char *)StrCache_Get(varlist[0]->strVal), (char *)StrCache_Get(varlist[1]->strVal));
34
35 if (tempstr != NULL)
36 {
37 ScriptVariant_ChangeType(*pretvar, VT_STR);
38 (*pretvar)->strVal = StrCache_CreateNewFrom(tempstr);
39 }
40 else
41 {
42 ScriptVariant_ChangeType(*pretvar, VT_INTEGER);
43 (*pretvar)->lVal = -1;
44 }
45 return S_OK;
46
47 sif_error:
48 *pretvar = NULL;
49 return E_FAIL;
50 }
51
52 //strinlast(char string, char search_string);
openbor_strinlast(ScriptVariant ** varlist,ScriptVariant ** pretvar,int paramCount)53 HRESULT openbor_strinlast(ScriptVariant **varlist , ScriptVariant **pretvar, int paramCount)
54 {
55 char *tempstr = NULL;
56
57 if(paramCount < 2)
58 {
59 goto sil_error;
60 }
61
62 if(varlist[0]->vt != VT_STR || varlist[1]->vt != VT_STR)
63 {
64 printf("\n Error, strinlast({string}, {search string}): Strinlast must be passed valid {string} and {search string}. \n");
65 goto sil_error;
66 }
67
68 // this definitely doesn't work??? it interprets a string cache index as a character
69 tempstr = strrchr((char *)StrCache_Get(varlist[0]->strVal), varlist[1]->strVal);
70
71 if (tempstr != NULL)
72 {
73 ScriptVariant_ChangeType(*pretvar, VT_STR);
74 (*pretvar)->strVal = StrCache_CreateNewFrom(tempstr);
75 }
76 else
77 {
78 ScriptVariant_ChangeType(*pretvar, VT_INTEGER);
79 (*pretvar)->lVal = -1;
80 }
81 return S_OK;
82 sil_error:
83 *pretvar = NULL;
84 return E_FAIL;
85 }
86
87 //strleft(char string, int i);
openbor_strleft(ScriptVariant ** varlist,ScriptVariant ** pretvar,int paramCount)88 HRESULT openbor_strleft(ScriptVariant **varlist , ScriptVariant **pretvar, int paramCount)
89 {
90 const char *src;
91 char *dst;
92 int srcLength, dstLength;
93
94 if(paramCount < 2)
95 {
96 goto sl_error;
97 }
98
99 if(varlist[0]->vt != VT_STR || varlist[1]->vt != VT_INTEGER)
100 {
101 printf("\n Error, strleft({string}, {characters}): Invalid or missing parameter. Strleft must be passed valid {string} and number of {characters}.\n");
102 goto sl_error;
103 }
104
105 src = StrCache_Get(varlist[0]->strVal);
106 srcLength = strlen(src);
107 dstLength = (srcLength < varlist[1]->lVal) ? srcLength : varlist[1]->lVal;
108 ScriptVariant_ChangeType(*pretvar, VT_STR);
109 (*pretvar)->strVal = StrCache_Pop(dstLength);
110 dst = StrCache_Get((*pretvar)->strVal);
111 memcpy(dst, src, dstLength);
112 dst[dstLength] = '\0';
113
114 return S_OK;
115 sl_error:
116 *pretvar = NULL;
117 return E_FAIL;
118 }
119
120 //strlength(char string);
openbor_strlength(ScriptVariant ** varlist,ScriptVariant ** pretvar,int paramCount)121 HRESULT openbor_strlength(ScriptVariant **varlist , ScriptVariant **pretvar, int paramCount)
122 {
123 if(paramCount < 1 || varlist[0]->vt != VT_STR)
124 {
125 goto strlength_error;
126 }
127
128 ScriptVariant_ChangeType(*pretvar, VT_INTEGER);
129 (*pretvar)->lVal = strlen((char *)StrCache_Get(varlist[0]->strVal));
130 return S_OK;
131
132 strlength_error:
133 printf("Error, strlength({string}): Invalid or missing parameter. Strlength must be passed a valid {string}.\n");
134 *pretvar = NULL;
135 return E_FAIL;
136 }
137
138 //strwidth(char string, int font);
openbor_strwidth(ScriptVariant ** varlist,ScriptVariant ** pretvar,int paramCount)139 HRESULT openbor_strwidth(ScriptVariant **varlist , ScriptVariant **pretvar, int paramCount)
140 {
141 LONG ltemp;
142 if(paramCount < 2 || varlist[0]->vt != VT_STR ||
143 FAILED(ScriptVariant_IntegerValue(varlist[1], <emp)))
144 {
145 goto strwidth_error;
146 }
147
148 ScriptVariant_ChangeType(*pretvar, VT_INTEGER);
149 (*pretvar)->lVal = font_string_width((int)ltemp, (char *)StrCache_Get(varlist[0]->strVal));
150 return S_OK;
151
152 strwidth_error:
153 printf("Error, strwidth({string}, {font}): Invalid or missing parameter.\n");
154 *pretvar = NULL;
155 return E_FAIL;
156 }
157
158 //strright(char string, int i);
openbor_strright(ScriptVariant ** varlist,ScriptVariant ** pretvar,int paramCount)159 HRESULT openbor_strright(ScriptVariant **varlist , ScriptVariant **pretvar, int paramCount)
160 {
161 char *tempstr = NULL;
162
163 if(paramCount < 2)
164 {
165 goto sr_error;
166 }
167
168 if(varlist[0]->vt != VT_STR || varlist[1]->vt != VT_INTEGER)
169 {
170 printf("\n Error, strright({string}, {characters}): Invalid or missing parameter. Strright must be passed valid {string} and number of {characters}.\n");
171 goto sr_error;
172 }
173
174 tempstr = (char *)StrCache_Get(varlist[0]->strVal);
175
176 if (tempstr && tempstr[0])
177 {
178 ScriptVariant_ChangeType(*pretvar, VT_STR);
179 (*pretvar)->strVal = StrCache_CreateNewFrom(&tempstr[varlist[1]->lVal]);
180 }
181 else
182 {
183 ScriptVariant_ChangeType(*pretvar, VT_INTEGER);
184 (*pretvar)->lVal = -1;
185 }
186
187 return S_OK;
188 sr_error:
189 *pretvar = NULL;
190 return E_FAIL;
191 }
192
193