Created
March 18, 2016 02:23
-
-
Save JimHokanson/334f51ac77e80ac6ab79 to your computer and use it in GitHub Desktop.
Return output of jsmn json tokenizer to Matlab via mex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include "matrix.h" | |
#include "mex.h" | |
#include "jsmn.h" | |
// mex jsmn_mex.c jsmn.c | |
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[] ) | |
{ | |
// str = fileread(file_path); | |
// token_info = jsmn_mex(str) | |
// | |
// token_info: see jsmn.h structure | |
int n; | |
int r2; | |
char *json_string; | |
jsmn_parser p; | |
jsmntok_t *t; | |
size_t string_byte_length; | |
json_string = mxArrayToString(prhs[0]); | |
string_byte_length = mxGetNumberOfElements(prhs[0]); | |
//TODO: Allow a memory hungry allocation of tokens (1/3 of file length?) | |
//Initial pass through, get # of tokens | |
jsmn_init(&p); | |
n = jsmn_parse(&p, json_string, string_byte_length, NULL, 0); | |
//Allocate output and reparse | |
t = mxMalloc(n*sizeof(jsmntok_t)); | |
jsmn_init(&p); | |
r2 = jsmn_parse(&p, json_string, strlen(json_string), t, n); | |
plhs[0] = mxCreateNumericArray(0, 0, mxUINT32_CLASS, mxREAL); | |
mxSetData(plhs[0], t); | |
mxSetM(plhs[0], sizeof(jsmntok_t)/4); | |
mxSetN(plhs[0], n); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment