Forums >> Programming >> RPG Programming
YAJL Update
by: bvstone

Jump to: 






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




Reply




Copyright 1983-2017 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).