Skip to content

Commit

Permalink
MatlabApi/private/gason*.*: pulling in latest version of gason, minor…
Browse files Browse the repository at this point in the history
… refactor
  • Loading branch information
pdollar committed Jan 11, 2016
1 parent 79d936c commit f1d9ba4
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 13 deletions.
13 changes: 10 additions & 3 deletions MatlabAPI/private/gason.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// https://github.com/vivkin/gason - pulled May 29, 2015
// https://github.com/vivkin/gason - pulled January 10, 2016
#include "gason.h"
#include <stdlib.h>

Expand Down Expand Up @@ -28,6 +28,8 @@ void *JsonAllocator::allocate(size_t size) {

size_t allocSize = sizeof(Zone) + size;
Zone *zone = (Zone *)malloc(allocSize <= JSON_ZONE_SIZE ? JSON_ZONE_SIZE : allocSize);
if (zone == nullptr)
return nullptr;
zone->used = allocSize;
if (allocSize <= JSON_ZONE_SIZE || head == nullptr) {
zone->next = head;
Expand Down Expand Up @@ -139,6 +141,7 @@ int jsonParse(char *s, char **endptr, JsonValue *value, JsonAllocator &allocator
JsonValue o;
int pos = -1;
bool separator = true;
JsonNode *node;
*endptr = s;

while (*s) {
Expand Down Expand Up @@ -316,11 +319,15 @@ int jsonParse(char *s, char **endptr, JsonValue *value, JsonAllocator &allocator
keys[pos] = o.toString();
continue;
}
tails[pos] = insertAfter(tails[pos], (JsonNode *)allocator.allocate(sizeof(JsonNode)));
if ((node = (JsonNode *) allocator.allocate(sizeof(JsonNode))) == nullptr)
return JSON_ALLOCATION_FAILURE;
tails[pos] = insertAfter(tails[pos], node);
tails[pos]->key = keys[pos];
keys[pos] = nullptr;
} else {
tails[pos] = insertAfter(tails[pos], (JsonNode *)allocator.allocate(sizeof(JsonNode) - sizeof(char *)));
if ((node = (JsonNode *) allocator.allocate(sizeof(JsonNode) - sizeof(char *))) == nullptr)
return JSON_ALLOCATION_FAILURE;
tails[pos] = insertAfter(tails[pos], node);
}
tails[pos]->value = o;
}
Expand Down
7 changes: 4 additions & 3 deletions MatlabAPI/private/gason.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// https://github.com/vivkin/gason - pulled May 29, 2015
// https://github.com/vivkin/gason - pulled January 10, 2016
#pragma once

#include <stdint.h>
Expand Down Expand Up @@ -30,7 +30,7 @@ union JsonValue {
: fval(x) {
}
JsonValue(JsonTag tag = JSON_NULL, void *payload = nullptr) {
assert((uint64_t)payload <= JSON_VALUE_PAYLOAD_MASK);
assert((uintptr_t)payload <= JSON_VALUE_PAYLOAD_MASK);
ival = JSON_VALUE_NAN_MASK | ((uint64_t)tag << JSON_VALUE_TAG_SHIFT) | (uintptr_t)payload;
}
bool isDouble() const {
Expand Down Expand Up @@ -97,7 +97,8 @@ inline JsonIterator end(JsonValue) {
XX(MISMATCH_BRACKET, "mismatch bracket") \
XX(UNEXPECTED_CHARACTER, "unexpected character") \
XX(UNQUOTED_KEY, "unquoted key") \
XX(BREAKING_BAD, "breaking bad")
XX(BREAKING_BAD, "breaking bad") \
XX(ALLOCATION_FAILURE, "allocation failure")

enum JsonErrno {
#define XX(no, str) JSON_##no,
Expand Down
25 changes: 18 additions & 7 deletions MatlabAPI/private/gasonMex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,24 +136,35 @@ ostrm& json( ostrm& S, const mxArray *M ) {
}
}

mxArray* mxCreateStringRobust( const char* str ) {
// convert char* to Matlab string (robust version of mxCreateString)
mxArray *M; ushort *c; mwSize n[2]={1,strlen(str)};
M=mxCreateCharArray(2,n); c=(ushort*) mxGetData(M);
for( siz i=0; i<n[1]; i++ ) c[i]=str[i]; return M;
}

char* mxArrayToStringRobust( const mxArray *M ) {
// convert Matlab string to char* (robust version of mxArrayToString)
if(!mxIsChar(M)) mexErrMsgTxt("String expected.");
ushort *c=(ushort*) mxGetData(M); char* str; siz n;
n=mxGetNumberOfElements(M); str=(char*) mxMalloc(n+1);
for( siz i=0; i<n; i++ ) str[i]=c[i]; str[n]=0; return str;
}

void mexFunction( int nl, mxArray *pl[], int nr, const mxArray *pr[] )
{
if( nr!=1 ) mexErrMsgTxt("One input expected.");
if( nl>1 ) mexErrMsgTxt("One output expected.");
if( mxGetClassID(pr[0])==mxCHAR_CLASS ) {
// object = mexFunction( string )
ushort *c=(ushort*) mxGetData(pr[0]); char* str; siz n;
n=mxGetNumberOfElements(pr[0]); str=(char*) mxMalloc(n+1);
for( siz i=0; i<n; i++ ) str[i]=c[i]; str[n]=0;
char *str = mxArrayToStringRobust(pr[0]);
char *endptr; JsonValue value; JsonAllocator allocator;
int status = jsonParse(str, &endptr, &value, allocator);
if( status != JSON_OK) mexErrMsgTxt(jsonStrError(status));
pl[0] = json( value ); mxFree(str);
pl[0] = json(value); mxFree(str);
} else {
// string = mexFunction( object )
ostrm S; S << std::setprecision(10); json(S,pr[0]);
std::string str=S.str(); mwSize n[2]={1,str.size()};
pl[0]=mxCreateCharArray(2,n); ushort *c=(ushort*) mxGetData(pl[0]);
for( siz i=0; i<n[1]; i++ ) c[i]=str[i];
pl[0]=mxCreateStringRobust(S.str().c_str());
}
}
Binary file modified MatlabAPI/private/gasonMex.mexa64
Binary file not shown.
Binary file modified MatlabAPI/private/gasonMex.mexmaci64
Binary file not shown.

0 comments on commit f1d9ba4

Please sign in to comment.