rac_string.c
Go to the documentation of this file.
1 
25 #include "stdint.h"
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 
30 #if defined(_USE_WIN_API)
31 #include <windows.h>
32 #endif
33 
34 #include "dn_common.h"
35 #include "rac_string.h"
36 
43 static int
44 SplitRacStr(const char* chSrc, char** chSplit)
45 {
46  int iCnt = 1, iBracket = 0;
47  char* chPos;
48 
49  *chSplit = (char*)malloc(strlen(chSrc) + 1);
50  if(*chSplit == NULL) return -1;
51 
52  strcpy(*chSplit, chSrc);
53  chPos = *chSplit;
54 
55  while(*chPos != '\0') {
56  switch(*chPos) {
57  case '(':
58  iBracket++;
59  break;
60  case ')':
61  iBracket--;
62  if(iBracket < 0) {
63  free(*chSplit);
64  return -1;
65  }
66  break;
67  case ',':
68  if(iBracket == 0) {
69  *chPos = '\0';
70  iCnt++;
71  }
72  break;
73  default:
74  break;
75  }
76 
77  chPos++;
78  }
79 
80  if(iBracket != 0) {
81  free(*chSplit);
82  return -1;
83  }
84 
85  return iCnt;
86 }
87 
95 HRESULT
96 ConvertRacStr2Variant(uint16_t vt, const char* chSrc, VARIANT* pvargDest)
97 {
98  HRESULT hr = S_OK;
99  int i, iCnt;
100  char *chSplit, *chPos, *chTmp, *chNext;
101  wchar_t *wchTmp;
102  VARIANT vntTmp;
103  void *parray;
104 
105  if((chSrc == NULL) || (pvargDest == NULL)) {
106  return E_INVALIDARG;
107  }
108 
109  if(vt != VT_BSTR) {
110  iCnt = SplitRacStr(chSrc, &chSplit);
111  if(iCnt < 0) {
112  return E_INVALIDARG;
113  }
114  } else {
115  iCnt = 1;
116  chSplit = (char*)malloc(strlen(chSrc) + 1);
117  if(chSplit == NULL) return E_OUTOFMEMORY;
118  strcpy(chSplit, chSrc);
119  }
120 
121  VariantInit(&vntTmp);
122  VariantClear(pvargDest);
123 
124  if(vt == (VT_VARIANT | VT_ARRAY)) {
125  pvargDest->vt = VT_VARIANT | VT_ARRAY;
126  pvargDest->parray = SafeArrayCreateVector(VT_VARIANT, 0, iCnt);
127  SafeArrayAccessData(pvargDest->parray, &parray);
128  for(i = 0, chPos = chSplit;
129  (i < iCnt) && SUCCEEDED(hr);
130  i++)
131  {
132  chNext = chPos + strlen(chPos) + 1;
133 
134  chTmp = strchr(chPos, '(');
135  if(chTmp == NULL) { hr = E_INVALIDARG; break; }
136 
137  *chTmp = '\0';
138  if(strspn(chPos, " ") != (chTmp - chPos)) {
139  hr = E_INVALIDARG;
140  break;
141  }
142 
143  chPos = chTmp + 1;
144 
145  chTmp = strrchr(chPos, ')');
146  if(chTmp == NULL) { hr = E_INVALIDARG; break; }
147 
148  *chTmp = '\0';
149  if(*(chTmp + 1) != '\0') {
150  if(strspn(chTmp + 1, " ") != strlen(chTmp + 1)) {
151  hr = E_INVALIDARG;
152  break;
153  }
154  }
155 
156  chTmp = strchr(chPos, ',');
157  if(chTmp == NULL) { hr = E_INVALIDARG; break; }
158 
159  *chTmp = '\0';
160  if(strspn(chPos, " 0123456789") != (chTmp - chPos)) {
161  hr = E_INVALIDARG;
162  break;
163  }
164 
165  VariantInit((VARIANT*)parray + i);
166  hr = ConvertRacStr2Variant(atoi(chPos), chTmp + 1, (VARIANT*)parray + i);
167 
168  chPos = chNext;
169  }
170  SafeArrayUnaccessData(pvargDest->parray);
171  }
172  else if(vt & VT_ARRAY) {
173  pvargDest->vt = vt;
174  pvargDest->parray = SafeArrayCreateVector(vt ^ VT_ARRAY, 0, iCnt);
175  SafeArrayAccessData(pvargDest->parray, &parray);
176  for(i = 0, chPos = chSplit;
177  (i < iCnt) && SUCCEEDED(hr);
178  i++, chPos += strlen(chPos) + 1)
179  {
180  wchTmp = ConvertMultiByte2WideChar(chPos);
181  if(wchTmp == NULL) { hr = E_OUTOFMEMORY; break; }
182 
183  if(vt == (VT_BSTR | VT_ARRAY)) {
184  *((BSTR*)parray + i) = SysAllocString(wchTmp);
185  } else {
186  vntTmp.vt = VT_BSTR;
187  vntTmp.bstrVal = SysAllocString(wchTmp);
188  hr = VariantChangeType(&vntTmp, &vntTmp, 0, vt ^ VT_ARRAY);
189  if(SUCCEEDED(hr)) {
190  memcpy((char*)parray + i * pvargDest->parray->cbElements,
191  &vntTmp.iVal, pvargDest->parray->cbElements);
192  }
193  }
194  VariantClear(&vntTmp);
195  free(wchTmp);
196  }
197  SafeArrayUnaccessData(pvargDest->parray);
198  }
199  else {
200  if(iCnt > 1) {
201  hr = E_INVALIDARG;
202  } else {
203  wchTmp = ConvertMultiByte2WideChar(chSplit);
204  if(wchTmp == NULL) { hr = E_OUTOFMEMORY; }
205  else {
206  vntTmp.vt = VT_BSTR;
207  vntTmp.bstrVal = SysAllocString(wchTmp);
208  hr = VariantChangeType(pvargDest, &vntTmp, 0, vt);
209  VariantClear(&vntTmp);
210  free(wchTmp);
211  }
212  }
213  }
214 
215  VariantClear(&vntTmp);
216  free(chSplit);
217 
218  return hr;
219 }
220 
227 HRESULT
228 ConvertVariant2RacStr(VARIANT varSrc, char** chDest)
229 {
230  HRESULT hr = S_OK;
231 
232  if(chDest == NULL) {
233  return E_INVALIDARG;
234  }
235 
236  *chDest = NULL;
237 
238  if(varSrc.vt == (VT_VARIANT | VT_ARRAY)) {
239  int32_t i, lbnd, ubnd, cnt;
240  uint32_t len = 0;
241  char chTmp[10], *chPoint;
242  VARIANT *pvarTmp;
243 
244  SafeArrayGetLBound(varSrc.parray, 1, &lbnd);
245  SafeArrayGetUBound(varSrc.parray, 1, &ubnd);
246  cnt = ubnd - lbnd + 1;
247 
248  SafeArrayAccessData(varSrc.parray, (void**)&pvarTmp);
249  for(i = 0; i < cnt; i++) {
250  hr = ConvertVariant2RacStr(pvarTmp[i], &chPoint);
251  if(FAILED(hr)) { if(*chDest != NULL) { free(*chDest); } break; }
252 
253  len += sprintf(chTmp, "%d,", pvarTmp[i].vt);
254  len += strlen(chPoint) + ((i == cnt - 1) ? 3 : 4);
255  *chDest = (char*)realloc(*chDest, len);
256  if(*chDest == NULL) { hr = E_OUTOFMEMORY; free(chPoint); break; }
257 
258  if(i == 0) *chDest[0] = '\0';
259 
260  strcat(*chDest, "(");
261  strcat(*chDest, chTmp);
262  strcat(*chDest, chPoint);
263  strcat(*chDest, (i == cnt - 1) ? ")" : "),");
264 
265  free(chPoint);
266  }
268  }
269  else if(varSrc.vt & VT_ARRAY) {
270  int32_t i, lbnd, ubnd, cnt;
271  uint32_t len = 0;
272  char *chPoint;
273  void *parray;
274  VARIANT *pvarTmp;
275 
276  SafeArrayGetLBound(varSrc.parray, 1, &lbnd);
277  SafeArrayGetUBound(varSrc.parray, 1, &ubnd);
278  cnt = ubnd - lbnd + 1;
279 
280  SafeArrayAccessData(varSrc.parray, &parray);
281  if(varSrc.vt == (VT_BSTR | VT_ARRAY)) {
282  for(i = 0; i < cnt; i++) {
283  chPoint = ConvertWideChar2MultiByte(*((BSTR*)parray + i));
284  if(chPoint == NULL) { hr = E_OUTOFMEMORY; break; }
285 
286  len += strlen(chPoint) + ((i == cnt - 1) ? 1 : 2);
287  *chDest = (char*)realloc(*chDest, len);
288  if(*chDest == NULL) { hr = E_OUTOFMEMORY; free(chPoint); break; }
289 
290  if(i == 0) *chDest[0] = '\0';
291 
292  strcat(*chDest, chPoint);
293  strcat(*chDest, (i == cnt - 1) ? "" : ",");
294 
295  free(chPoint);
296  }
297  } else {
298  pvarTmp = (VARIANT*)malloc(sizeof(VARIANT) * cnt);
299  if(pvarTmp == NULL) { hr = E_OUTOFMEMORY; }
300  else {
301  memset(pvarTmp, 0, sizeof(VARIANT) * cnt);
302  for(i = 0; i < cnt; i++) {
303  pvarTmp[i].vt = varSrc.vt ^ VT_ARRAY;
304  memcpy(&pvarTmp[i].iVal, (char*)parray + i * varSrc.parray->cbElements,
305  varSrc.parray->cbElements);
306  hr = VariantChangeType(&pvarTmp[i], &pvarTmp[i], 0, VT_BSTR);
307  if(FAILED(hr)) break;
308  }
309 
310  if(SUCCEEDED(hr)) {
311  for(i = 0; i < cnt; i++) {
312  chPoint = ConvertWideChar2MultiByte(pvarTmp[i].bstrVal);
313  if(chPoint == NULL) { hr = E_OUTOFMEMORY; break; }
314 
315  len += strlen(chPoint) + ((i == cnt - 1) ? 1 : 2);
316  *chDest = (char*)realloc(*chDest, len);
317  if(*chDest == NULL) { hr = E_OUTOFMEMORY; free(chPoint); break; }
318 
319  if(i == 0) *chDest[0] = '\0';
320 
321  strcat(*chDest, chPoint);
322  strcat(*chDest, (i == cnt - 1) ? "" : ",");
323 
324  free(chPoint);
325  }
326  }
327 
328  for(i = 0; i < cnt; i++) {
329  VariantClear(&pvarTmp[i]);
330  }
331  free(pvarTmp);
332  }
333  }
335  }
336  else {
337  VARIANT varTmp;
338  VariantInit(&varTmp);
339  hr = VariantChangeType(&varTmp, &varSrc, 0, VT_BSTR);
340  if(SUCCEEDED(hr)) {
341  *chDest = ConvertWideChar2MultiByte(varTmp.bstrVal);
342  if(*chDest == NULL) hr = E_OUTOFMEMORY;
343  }
344  VariantClear(&varTmp);
345  }
346 
347  return hr;
348 }
unsigned uint32_t
Definition: stdint.h:43
HRESULT ConvertRacStr2Variant(uint16_t vt, const char *chSrc, VARIANT *pvargDest)
Converts RAC string to VARIANT.
Definition: rac_string.c:96
_DN_EXP_COMMON HRESULT SafeArrayUnaccessData(SAFEARRAY *psa)
Unaccesses the SAFEARRAY.
Definition: dn_common.c:353
#define S_OK
Succeeded.
Definition: dn_common.h:89
_DN_EXP_COMMON HRESULT SafeArrayGetLBound(SAFEARRAY *psa, uint16_t nDim, int32_t *plLbound)
Gets the lower bound of SAFEARRAY.
Definition: dn_common.c:278
wchar_t * BSTR
Definition: dn_common.h:239
static int SplitRacStr(const char *chSrc, char **chSplit)
Splits RAC string.
Definition: rac_string.c:44
unsigned short uint16_t
Definition: stdint.h:41
BSTR bstrVal
Definition: dn_common.h:318
#define FAILED(hr)
A macro that returns TRUE/FALSE. If hr is less than zero, then returns TRUE.
Definition: dn_common.h:77
RAC(Robot Action Command) String API file.
#define E_INVALIDARG
Failed because some arguments are invalid.
Definition: dn_common.h:131
uint32_t cbElements
Definition: dn_common.h:297
_DN_EXP_COMMON HRESULT SafeArrayGetUBound(SAFEARRAY *psa, uint16_t nDim, int32_t *plUbound)
Gets the upper bound of SAFEARRAY.
Definition: dn_common.c:298
int32_t HRESULT
Definition: dn_common.h:61
HRESULT ConvertVariant2RacStr(VARIANT varSrc, char **chDest)
Converts VARIANT to RAC string.
Definition: rac_string.c:228
#define E_OUTOFMEMORY
Failed because there is no enough memory space.
Definition: dn_common.h:125
SAFEARRAY * parray
Definition: dn_common.h:325
#define SUCCEEDED(hr)
A macro that returns TRUE/FALSE. If hr is zero or more, then returns TRUE.
Definition: dn_common.h:71
uint16_t vt
Definition: dn_common.h:308
Common API file.
_DN_EXP_COMMON void VariantInit(VARIANT *pvarg)
Initializes the VARIANT.
Definition: dn_common.c:368
_DN_EXP_COMMON HRESULT VariantChangeType(VARIANT *pvargDest, VARIANT *pvarSrc, uint16_t wFlags, uint16_t vt)
Changes the source variant to destination variant with the indicated type.
A type definition for the multi type variable.
Definition: dn_common.h:306
_DN_EXP_COMMON BSTR SysAllocString(const wchar_t *sz)
Allocates and returns BSTR.
Definition: dn_common.c:61
_DN_EXP_COMMON void VariantClear(VARIANT *pvarg)
Clears the VARIANT.
Definition: dn_common.c:382
int int32_t
Definition: stdint.h:42
int16_t iVal
Definition: dn_common.h:311
_DN_EXP_COMMON char * ConvertWideChar2MultiByte(const wchar_t *chSrc)
Converts wide string to string.
_DN_EXP_COMMON SAFEARRAY * SafeArrayCreateVector(uint16_t vt, int32_t lLbound, uint32_t cElements)
Allocates and returns SAFEARRAY.
Definition: dn_common.c:138
_DN_EXP_COMMON wchar_t * ConvertMultiByte2WideChar(const char *chSrc)
Converts string to wide string.
_DN_EXP_COMMON HRESULT SafeArrayAccessData(SAFEARRAY *psa, void **ppvData)
Accesses the SAFEARRAY and gets the pointer of array data.
Definition: dn_common.c:336


bcap_core
Author(s): DENSO WAVE INCORPORATED
autogenerated on Mon Jun 10 2019 13:12:20