@@ -197,19 +197,7 @@ static int cws_set_uint64_stat_list(napi_env env, napi_value exports, struct sta
197197
198198 return 0 ;
199199}
200- /* DEPRECATED
201- #define TEXT_BUF_ALLOC_MAX_SZ 2048
202- static char *textBufAlloc(size_t len)
203- {
204- len++;
205-
206- if ((len>1)&&(len<TEXT_BUF_ALLOC_MAX_SZ))
207- return cws_malloc(len);
208200
209- return NULL;
210- }
211- #undef TEXT_BUF_ALLOC_MAX_SZ
212- */
213201static char * textBufAlloc (size_t * sz , struct js_cws_config_t * js_cws_config , size_t len )
214202{
215203 char * str_tmp ;
@@ -286,7 +274,8 @@ static int c_parse_util(
286274 void * * v_ser ,
287275 const char * c_xml ,
288276 size_t c_xml_size ,
289- enum c_parse_util_e c_parse_util
277+ enum c_parse_util_e c_parse_util ,
278+ char * * errMsg
290279)
291280{
292281
@@ -296,19 +285,30 @@ static int c_parse_util(
296285 cws_internal_soap_recycle (soap_internal );
297286 cws_recycle_config (config );
298287
299- if (!cws_parse_XML_soap_envelope (soap_internal , (char * )c_xml , (size_t )c_xml_size ))
288+ if (!cws_parse_XML_soap_envelope (soap_internal , (char * )c_xml , (size_t )c_xml_size )) {
289+ * errMsg = "Could not parse xml string. See xml errors for details" ;
300290 return -500 ;
291+ }
301292
302- if (cws_soap_serve (soap_internal ))
293+ if (cws_soap_serve (soap_internal )) {
294+ * errMsg = "Could not deserialize xml string. See xml errors for details" ;
303295 return -501 ;
296+ }
304297
305298 if (c_parse_util == IS_BSON ) {
306- if ((* v_ser = (void * )bsonSerialize (soap_internal )))
299+ if ((* v_ser = (void * )bsonSerialize (soap_internal ))) {
300+ * errMsg = "" ;
307301 return 0 ;
302+ }
308303
304+ * errMsg = "Could not serialize BSON" ;
309305 } else {
310- if ((* v_ser = (void * )getJson (soap_internal )))
306+ if ((* v_ser = (void * )getJson (soap_internal ))) {
307+ * errMsg = "" ;
311308 return 0 ;
309+ }
310+
311+ * errMsg = "Could not serialize JSON" ;
312312 }
313313
314314 return -503 ;
@@ -400,17 +400,15 @@ static void cws_js_throw(napi_env env, struct cws_js_err_t *caster, const char *
400400 napi_throw_error (env , caster -> err , caster -> buf );
401401}
402402
403- static int cws_add_function_util (napi_value * fn_out , napi_env env , napi_value exports , CWS_JS_FUNCTIONS_OBJ * function , void * data )
403+ static int cws_add_function_util (napi_env env , napi_value exports , CWS_JS_FUNCTIONS_OBJ * function , void * data )
404404{
405- napi_value * fnTmp , fn ;
406-
407- fnTmp = (fn_out )?fn_out :& fn ;
405+ napi_value fn ;
408406
409407 while (function -> function_name ) {
410- if (napi_create_function (env , NULL , 0 , function -> fn , data , fnTmp )!= napi_ok )
408+ if (napi_create_function (env , NULL , 0 , function -> fn , data , & fn )!= napi_ok )
411409 return 300 ;
412410
413- if (napi_set_named_property (env , exports , function -> function_name , * fnTmp )!= napi_ok )
411+ if (napi_set_named_property (env , exports , function -> function_name , fn )!= napi_ok )
414412 return 301 ;
415413
416414 function ++ ;
@@ -492,7 +490,7 @@ napi_value c_getBsonVersion(napi_env env, napi_callback_info info)
492490napi_value c_parseFromFile (napi_env env , napi_callback_info info )
493491{
494492 napi_value argv = NULL , res ;
495- char * filename , * text ;
493+ char * filename , * text , * errMsg ;
496494 size_t argc = 1 , filenameLen , textLen ;
497495 struct cws_js_err_t cws_js_err ;
498496 struct c_bson_serialized_t * bson_ser ;
@@ -540,8 +538,8 @@ napi_value c_parseFromFile(napi_env env, napi_callback_info info)
540538 )
541539
542540 JS_CWS_THROW_COND_GOTO (
543- (ERR = c_parse_util (js_cws_instance -> soap_internal , (void * * )& bson_ser , text , textLen , IS_BSON )),
544- "c_parse_util" , "BSON parse error @ c_parseFromFile" ,
541+ (ERR = c_parse_util (js_cws_instance -> soap_internal , (void * * )& bson_ser , text , textLen , IS_BSON , & errMsg )),
542+ "c_parse_util" , errMsg ,
545543 ERR , c_parseFromFile_exit1
546544 )
547545
@@ -561,10 +559,129 @@ napi_value c_parseFromFile(napi_env env, napi_callback_info info)
561559 return NULL ;
562560}
563561
562+ napi_value c_parse (napi_env env , napi_callback_info info )
563+ {
564+ napi_value argv = NULL , res ;
565+ char * text , * errMsg ;
566+ size_t argc = 1 , textLen ;
567+ struct cws_js_err_t cws_js_err ;
568+ struct c_bson_serialized_t * bson_ser ;
569+ struct js_cws_config_t * js_cws_instance ;
570+
571+ JS_CWS_THROW_COND (
572+ (ERR = (napi_get_cb_info (env , info , & argc , & argv , NULL , (void * * )& js_cws_instance )!= napi_ok )),
573+ "napi_get_cb_info" ,
574+ "Can't parse arguments. Wrong argument at c_parse" ,
575+ 100
576+ )
577+
578+ JS_CWS_THROW_COND (
579+ (ERR = (js_cws_instance == NULL )),
580+ "c_parse" ,
581+ "Fatal: js_cws_instance. Was expected NOT NULL" ,
582+ 101
583+ )
584+
585+ JS_CWS_THROW_COND (
586+ (ERR = (argc == 0 )),
587+ "c_parse" ,
588+ "Missing argument. Was expected: xml text" ,
589+ 102
590+ )
591+
592+ JS_CWS_THROW_COND (
593+ (ERR = (argc > 1 )),
594+ "c_parse" ,
595+ "Too many arguments" ,
596+ 103
597+ )
598+
599+ JS_CWS_THROW_COND (
600+ (ERR = ((text = js_cws_get_value_string_utf8 (& textLen , env , argv , js_cws_instance ))== NULL )),
601+ "js_cws_get_value_string_utf8" ,
602+ "Could not parse xml string. Wrong format or empty string or invalid utf-8 or no space in C string buffer" ,
603+ 104
604+ )
605+
606+ JS_CWS_THROW_COND (
607+ (ERR = c_parse_util (js_cws_instance -> soap_internal , (void * * )& bson_ser , text , textLen , IS_BSON , & errMsg )),
608+ "c_parse_util" , errMsg ,
609+ ERR
610+ )
611+
612+ JS_CWS_THROW_COND (
613+ (ERR = js_cws_new_array_buffer (& res , env , (void * )bson_ser -> bson , bson_ser -> bson_size )),
614+ "js_cws_new_array_buffer" , "Could not copy BSON bytes to JavaScript array buffer @ c_parse" ,
615+ ERR
616+ )
617+
618+ return res ;
619+ }
620+
621+ napi_value c_parseToJSON (napi_env env , napi_callback_info info )
622+ {
623+ napi_value argv = NULL , res ;
624+ char * text , * errMsg ;
625+ size_t argc = 1 , textLen ;
626+ struct cws_js_err_t cws_js_err ;
627+ struct c_json_str_t * json_ser ;
628+ struct js_cws_config_t * js_cws_instance ;
629+
630+ JS_CWS_THROW_COND (
631+ (ERR = (napi_get_cb_info (env , info , & argc , & argv , NULL , (void * * )& js_cws_instance )!= napi_ok )),
632+ "napi_get_cb_info" ,
633+ "Can't parse arguments. Wrong argument at c_parseToJSON" ,
634+ 200
635+ )
636+
637+ JS_CWS_THROW_COND (
638+ (ERR = (js_cws_instance == NULL )),
639+ "c_parseToJSON" ,
640+ "Fatal: js_cws_instance. Was expected NOT NULL" ,
641+ 201
642+ )
643+
644+ JS_CWS_THROW_COND (
645+ (ERR = (argc == 0 )),
646+ "c_parseToJSON" ,
647+ "Missing argument. Was expected: xml text" ,
648+ 202
649+ )
650+
651+ JS_CWS_THROW_COND (
652+ (ERR = (argc > 1 )),
653+ "c_parseToJSON" ,
654+ "Too many arguments" ,
655+ 203
656+ )
657+
658+ JS_CWS_THROW_COND (
659+ (ERR = ((text = js_cws_get_value_string_utf8 (& textLen , env , argv , js_cws_instance ))== NULL )),
660+ "js_cws_get_value_string_utf8" ,
661+ "Could not parse xml string. Wrong format or empty string or invalid utf-8 or no space in C string buffer" ,
662+ 204
663+ )
664+
665+ JS_CWS_THROW_COND (
666+ (ERR = c_parse_util (js_cws_instance -> soap_internal , (void * * )& json_ser , text , textLen , IS_JSON , & errMsg )),
667+ "c_parse_util" , errMsg ,
668+ ERR
669+ )
670+
671+ JS_CWS_THROW_COND (
672+ (ERR = (napi_create_string_utf8 (env , json_ser -> json , json_ser -> json_len , & res )!= napi_ok )),
673+ "napi_create_string_utf8" ,
674+ "napi_create_string_utf8 @ c_parseToJSON. Error on parsing JSON string" ,
675+ 205
676+ )
677+
678+ return res ;
679+ }
680+
564681napi_value c_parseFromFileJSON (napi_env env , napi_callback_info info )
565682{
566683 napi_value argv = NULL , res ;
567- char * filename , * text ;
684+ char * filename , * text , * errMsg ;
568685 size_t argc = 1 , filenameLen , textLen ;
569686 struct cws_js_err_t cws_js_err ;
570687 struct c_json_str_t * json_ser ;
@@ -612,8 +729,8 @@ napi_value c_parseFromFileJSON(napi_env env, napi_callback_info info)
612729 )
613730
614731 JS_CWS_THROW_COND_GOTO (
615- (ERR = c_parse_util (js_cws_instance -> soap_internal , (void * * )& json_ser , text , textLen , IS_JSON )),
616- "c_parse_util" , "BSON parse error @ c_parseFromFileJSON" ,
732+ (ERR = c_parse_util (js_cws_instance -> soap_internal , (void * * )& json_ser , text , textLen , IS_JSON , & errMsg )),
733+ "c_parse_util" , errMsg ,
617734 ERR , c_parseFromFileJSON_exit1
618735 )
619736
@@ -1007,6 +1124,8 @@ CWS_JS_FUNCTIONS_OBJ CWS_JS_CREATE_FUNCTIONS[] = {
10071124 SET_JS_FN_CALL (getJson ),
10081125 SET_JS_FN_CALL (getStatistics ),
10091126 SET_JS_FN_CALL (getError ),
1127+ SET_JS_FN_CALL (parse ),
1128+ SET_JS_FN_CALL (parseToJSON ),
10101129 SET_JS_FN_CALL (parseFromFile ),
10111130 SET_JS_FN_CALL (parseFromFileJSON ),
10121131 SET_JS_FN_CALL (saveToFile ),
@@ -1042,7 +1161,7 @@ napi_value c_create(napi_env env, napi_callback_info info)
10421161
10431162 JS_CWS_THROW_COND ((js_cws_instance = js_cws_config_init ())== NULL , "js_cws_config_init" , "Could not alloc JSWITSML 2.1 C instance" , 101 )
10441163
1045- if ((err = cws_add_function_util (NULL , env , res , CWS_JS_CREATE_FUNCTIONS , js_cws_instance ))) {
1164+ if ((err = cws_add_function_util (env , res , CWS_JS_CREATE_FUNCTIONS , js_cws_instance ))) {
10461165 js_cws_config_free (& js_cws_instance );
10471166
10481167 JS_CWS_THROW ("cws_add_function_util" , "Could add functions @ c_create constructor" , 102 )
@@ -1072,7 +1191,7 @@ napi_value Init(napi_env env, napi_value exports)
10721191 struct cws_js_err_t cws_js_err ;
10731192
10741193 JS_CWS_THROW_COND (
1075- (err = cws_add_function_util (NULL , env , exports , CWS_JS_INIT_FUNCTIONS , NULL )),
1194+ (err = cws_add_function_util (env , exports , CWS_JS_INIT_FUNCTIONS , NULL )),
10761195 "cws_add_function_util" ,
10771196 "Could not initialize C functions" ,
10781197 err
0 commit comments