bvstone

YAJL Update

Posted:

YAJL Update

A while back I was trying to find out if the YAJL port from Scott Klement had a function that would return a pointer to node data as well as get the length.  See this thread:

http://archive.midrange.com/rpg400-l/201604/msg00138.html

What Scott, Barbara (and a little bit of me) came up with were to create two new subprocedures.  Following additions to the member YAJLR4 in QRPGLESRC:

      *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      * YAJL_GET_STRING_UTF8(): Retrieve a pointer to the raw UTF-8
      *                         data for a YAJL string
      *
      *   node = (input) YAJL tree node
      *
      * returns a pointer to a C-style, zero-terminated string
      *      or *NULL if the node is not a string
      *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     P YAJL_GET_STRING_UTF8...
     P                 B                   export
     D                 PI              *
     D   node                              like(yajl_val) value

     D nv              ds                  likeds(yajl_val_t)
     D                                     based(node)

      /free
       if YAJL_IS_STRING(node);
         return nv.string;
       else;
         return *null;
       endif;
      /end-free
     P                 E

      *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      * YAJL_GET_STRING_BUF(): Retrieve a pointer to the YAJL String
      *
      *   node = (input) YAJL tree node
      *   buf = (input) pointer to buffer to load
      *   bufSize = (input) size of buffer to load
      *
      * returns size of buffer or -1 for error
      *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     P YAJL_GET_STRING_BUF...
     P                 B                   export
     D                 PI            10U 0
     D   node                              like(yajl_val) value
     D   buf                           *   value
     D   bufSize                     10U 0 value

     D memcpy          pr              *   extproc('memcpy')
     D   dst                           *   value
     D   src                           *   value
     D   len                         10u 0 value

     d strlen          PR            10u 0 extproc('strlen')
     d  p                              *   value

     D nv              ds                  likeds(yajl_val_t)
     D                                     based(node)
     D str             s               *
     D len             s             10u 0
      /free
       if YAJL_IS_STRING(node);
          init_gconv();
          str = yajl_iconv_toLocal( gConv: nv.string );
          len = strlen(str);

          if (len > bufSize);
            len = bufSize;
          endif;

          memcpy(buf:str:len);
          yajl_iconv_string_free(str);
          return len;
       else;
          return -1;
       endif;
      /end-free
     P                 E

The YAJL_GET_STRING_UTF8 is exactly how Scott suggested.

The YAJL_GET_STRING_BUF is a little different.  Instead of returning a boolean (indicator) it returns the size of the string (or -1 for an error).

Here's an example of how I am using it:

 val = YAJL_object_find(node:'data');
 
 data@ = %alloc(MAX_SIZE);

 if (data@ = *null);
   //error
 endif;

 rc = yajl_get_string_buf(val:data@:MAX_SIZE);

 // do stuff with the data

 dealloc(en) l_data@;

After the call to yajl_get_string_buf, the variable rc will contain the size of the data and data@ will point to the data, converted from UTF8 to the jobs CCSID.

Other changes made were to the following additions to the /copy member YAJL_H in QRPGLESRC:

      *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      * YAJL_GET_STRING_UTF8(): Retrieve a pointer to the raw UTF-8
      *                         data for a YAJL string
      *
      *   node = (input) YAJL tree node
      *
      * returns a pointer to a C-style, zero-terminated string
      *      or *NULL if the node is not a string
      *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     D YAJL_GET_STRING_UTF8...
     D                 PR              *
     D   node                              like(yajl_val) value

      *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      * YAJL_GET_STRING_BUF(): Retrieve a pointer to the YAJL String
      *
      *   node = (input) YAJL tree node
      *   buf = (input) pointer to buffer to load
      *   bufSize = (input) size of buffer to load
      *
      * returns size of buffer or -1 for error
      *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     D YAJL_GET_STRING_BUF...
     D                 PR            10u 0
     D   node                              like(yajl_val) value
     D   buf                           *   value
     D   bufSize                     10u 0 value

And finally the updated binder langauge member YAJLR4 in QSRVSRC:

STRPGMEXP  SIGNATURE('YAJLR4 1.1      ')
  export symbol(yajl_buf_parse)
  export symbol(yajl_stmf_parse)
  export symbol(yajl_buf_load_tree)
  export symbol(yajl_stmf_load_tree)
  export symbol(yajl_is_array)
  export symbol(yajl_is_false)
  export symbol(yajl_is_null)
  export symbol(yajl_is_number)
  export symbol(yajl_is_object)
  export symbol(yajl_is_string)
  export symbol(yajl_is_true)
  export symbol(yajl_get_number)
  export symbol(yajl_get_string)
  export symbol(yajl_array_elem)
  export symbol(yajl_array_loop)
  export symbol(yajl_array_size)
  export symbol(yajl_object_elem)
  export symbol(yajl_object_find)
  export symbol(yajl_object_loop)
  export symbol(yajl_object_size)
  export symbol(yajl_genOpen)
  export symbol(yajl_genClose)
  export symbol(yajl_beginObj)
  export symbol(yajl_endObj)
  export symbol(yajl_beginArray)
  export symbol(yajl_endArray)
  export symbol(yajl_addChar)
  export symbol(yajl_addNum)
  export symbol(yajl_addBool)
  export symbol(yajl_addNull)
  export symbol(yajl_copyBuf)
  export symbol(yajl_saveBuf)
  export symbol(yajl_writeStdout)
  export symbol(yajl_stdin_load_tree)
  export symbol(yajl_addCharEx)
  export symbol(yajl_addCharStmf)
  export symbol(yajl_exbuf_new)
  export symbol(yajl_exbuf_concat_ptr)
  export symbol(yajl_exbuf_concat)
  export symbol(yajl_exbuf_free)
  export symbol(yajl_save_string_stmf)
  export symbol(yajl_getBuf)
  export symbol(yajl_tree_free_rpg)
  export symbol(yajl_get_string_utf8)
  export symbol(yajl_get_string_buf)
endpgmexp
STRPGMEXP  PGMLVL(*PRV) SIGNATURE('YAJLR4 1.0      ')
  export symbol(yajl_buf_parse)
  export symbol(yajl_stmf_parse)
  export symbol(yajl_buf_load_tree)
  export symbol(yajl_stmf_load_tree)
  export symbol(yajl_is_array)
  export symbol(yajl_is_false)
  export symbol(yajl_is_null)
  export symbol(yajl_is_number)
  export symbol(yajl_is_object)
  export symbol(yajl_is_string)
  export symbol(yajl_is_true)
  export symbol(yajl_get_number)
  export symbol(yajl_get_string)
  export symbol(yajl_array_elem)
  export symbol(yajl_array_loop)
  export symbol(yajl_array_size)
  export symbol(yajl_object_elem)
  export symbol(yajl_object_find)
  export symbol(yajl_object_loop)
  export symbol(yajl_object_size)
  export symbol(yajl_genOpen)
  export symbol(yajl_genClose)
  export symbol(yajl_beginObj)
  export symbol(yajl_endObj)
  export symbol(yajl_beginArray)
  export symbol(yajl_endArray)
  export symbol(yajl_addChar)
  export symbol(yajl_addNum)
  export symbol(yajl_addBool)
  export symbol(yajl_addNull)
  export symbol(yajl_copyBuf)
  export symbol(yajl_saveBuf)
  export symbol(yajl_writeStdout)
  export symbol(yajl_stdin_load_tree)
  export symbol(yajl_addCharEx)
  export symbol(yajl_addCharStmf)
  export symbol(yajl_exbuf_new)
  export symbol(yajl_exbuf_concat_ptr)
  export symbol(yajl_exbuf_concat)
  export symbol(yajl_exbuf_free)
  export symbol(yajl_save_string_stmf)
  export symbol(yajl_getBuf)
  export symbol(yajl_tree_free_rpg)
endpgmexp

 


Last edited 05/27/2016 at 16:04:14



Latest Posts:

Non HTTPS Callbacks Removed from GreenTools for G Suite (G4G) Non HTTPS Callbacks Removed from GreenTools for G Suite (G4G)
Posted by April 15, 2018
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
IBM i Related Survey Available IBM i Related Survey Available
Posted by April 7, 2018
IBM Power Systems >> (QGPL) IBM i
BVSTools Releases Braintree Webhook Open Source Application - Node.js BVSTools Releases Braintree Webhook Open Source Application - Node.js
Posted by April 5, 2018
Programming >> Open Source
BVSTools Now Offering Web Services (BETA) BVSTools Now Offering Web Services (BETA)
Posted by April 3, 2018
BVSTools >> BVSTools Announcements
Creating a Reverse SSL Proxy Using RPG on the IBM i - Part 2 Creating a Reverse SSL Proxy Using RPG on the IBM i - Part 2
Posted by March 29, 2018
Programming >> Web Programming
Still on V7R1 or Earlier?  Here's Why You Should Upgrade NOW! Still on V7R1 or Earlier? Here's Why You Should Upgrade NOW!
Posted by February 21, 2018
IBM Power Systems >> (QGPL) IBM i
Converting a MMDDYY date format to YYMMDD for Sorting Using SQL Converting a MMDDYY date format to YYMMDD for Sorting Using SQL
Posted by February 16, 2018
Programming >> RPG Programming
Moving All Files from a Google Drive Folder to the Trash Using GreenTools for Google Apps (G4G) Moving All Files from a Google Drive Folder to the Trash Using GreenTools for Google Apps (G4G)
Posted by February 3, 2018
BVSTools >> BVSTools Software Discussion >> GreenTools for G Suite (Google Apps) (G4G) Specific Discussion
Removing Unwanted Characters from a String with RPG Removing Unwanted Characters from a String with RPG
Posted by February 2, 2018
Programming >> RPG Programming
GETURI Version 8.10 Released GETURI Version 8.10 Released
Posted by January 23, 2018
BVSTools >> BVSTools Announcements >> Get URI (GETURI) Specific Announcements
BVSTools is Now a Sponsor of ALLAS400S.com BVSTools is Now a Sponsor of ALLAS400S.com
Posted by January 17, 2018
BVSTools >> BVSTools Announcements
Service Pack SP171219 Released for G4PP v3.10 Service Pack SP171219 Released for G4PP v3.10
Posted by December 20, 2017
BVSTools >> BVSTools Announcements >> GreenTools for PayPal (G4PP) Specific Announcements
GETURI Now Requires Minimum OS of V7R1 or Higher GETURI Now Requires Minimum OS of V7R1 or Higher
Posted by December 12, 2017
BVSTools >> BVSTools Software Discussion >> Get URI (GETURI) Specific Discussion
Need Help Updating Your Software Versions?  We Can Help. Need Help Updating Your Software Versions? We Can Help.
Posted by December 8, 2017
BVSTools >> BVSTools Announcements
Parsing the Response from GreenTools for PayPal (G4PP) Refund Transaction Parsing the Response from GreenTools for PayPal (G4PP) Refund Transaction
Posted by November 27, 2017
BVSTools >> BVSTools Software Discussion >> GreenTools for PayPal (G4PP) Specific Discussion

Reply




Copyright 1983-2018 BVSTools
GreenBoard(v3) Powered by the eRPG SDK, MAILTOOL Plus!, GreenTools for Google Apps, jQuery, jQuery UI, BlockUI, CKEditor and running on the IBM i (AKA AS/400, iSeries, System i).