#include "json.h"
#include <string.h>
+#include <Windows.h>
typedef char JSONText;
-#define wcharToHex(c,defaulterr) ((((c) >= L'0') & ((c) <= L'9')) ? (c) - L'0' : ((((c) >= L'a') & ((c) <= L'f')) ? (c) - L'a' : ((((c) >= L'A') &((c) <= L'F')) ? (c) - L'A' : (defaulterr))))
+#define wcharToHex(c,defaulterr) ((((c) >= L'0') & ((c) <= L'9')) ? (c) - L'0' : ((((c) >= L'a') & ((c) <= L'f')) ? (((c) - L'a') + 10) : ((((c) >= L'A') &((c) <= L'F')) ? (((c) - L'A') + 10) : (defaulterr))))
///
///json\83p\81[\83T\83I\83u\83W\83F\83N\83g
///
JSONUInt valueSize; //\95¶\8e\9a\97ñ\83T\83C\83Y
-
+ JSONStructEventHandler structHandler; //\8d\\91¢\91Ì\83C\83x\83\93\83g\83n\83\93\83h\83\89
- JSONDataEventHandler dataHandler; //\8d\\91¢\91Ì\83C\83x\83\93\83g\83n\83\93\83h\83\89
+ JSONDataEventHandler dataHandler; //\8d\\91¢\91Ì\83f\81[\83^\83C\83x\83\93\83g\83n\83\93\83h\83\89
JSONDataEndEventHandler dataEndHandler; //\8d\\91¢\91Ì\8fI\92[\83C\83x\83\93\83g\83n\83\93\83h\83\89
//
//\83f\83t\83H\83\8b\83g\82Ì\83C\83x\83\93\83g\83n\83\93\83h\83\89
//
+
+static JSONVoid defaultStructEventHandler(JSONVoid *option,JSONUInt thisHierarchy,JSONUInt valueType,JSONChar_P name);
+
static JSONVoid defaultDataEventHandler(JSONVoid *option,JSONUInt thisHierarchy,JSONUInt valueType,JSONChar_P name,JSONParameter parameter);
static JSONVoid defaultDataEndEventHandler(JSONVoid *option,JSONUInt thisHierarchy,JSONUInt endStructreType,JSONChar_P name);
+
//
//\83v\83\89\83C\83x\81[\83g\8aÖ\90\94
//
///
///json\95¶\8e\9a\97ñ\82Ì\8fI\92[\82ð\8eæ\93¾
///
-static JSONText* GetJSONStringEndUTF8(const JSONText *jsontext);
+static JSONResult GetJSONStringEndUTF8(const JSONText *jsontext,const JSONText **endptr);
///
//\83I\83u\83W\83F\83N\83g
case '{':
- parser->dataHandler(parser->option,JSONTypeRoot,JSONTypeObject,NULL,(JSONParameter)NULL);
+ parser->structHandler(parser->option,JSONTypeRoot,JSONTypeObject,NULL);
ParseObjectUTF8(parser,jsontext,&jsontext);
//\94z\97ñ
case '[':
- parser->dataHandler(parser->option,JSONTypeRoot,JSONTypeArray,NULL,(JSONParameter)NULL);
+ parser->structHandler(parser->option,JSONTypeRoot,JSONTypeArray,NULL);
ParseArrayUTF8(parser,jsontext,&jsontext);
jsontext++;
//\8fI\92[\95¶\8e\9a\83|\83C\83\93\83^\82ð\8eæ\93¾
- nameend = GetJSONStringEndUTF8(jsontext);
+ rslt = GetJSONStringEndUTF8(jsontext,&nameend);
+
+ if(rslt != JSON_ERR_OK){
+
+ goto end;
+
+ }
//\8fI\92[\95¶\8e\9a\83|\83C\83\93\83^\82ð\8c³\82É\95Ï\8a·\97p\95¶\8e\9a\97ñ\82Ì\92·\82³\82ð\8eæ\93¾
nameLen = getLenU8ToU16(jsontext,nameend);
//\83I\83u\83W\83F\83N\83g\96¼\82ð\89ð\90Í
rslt = ParseStringUTF8(name,nameLen,jsontext,nameend,&jsontext);
- jsontext = nameend;
-
+
//\90³\8fí\82Å\82Í\82È\82¢\8fê\8d\87\8fI\97¹
if(rslt != JSON_ERR_OK){
goto syntaxerr;
}
-
+ jsontext++;
+
//\8bó\94\92\82ð\83X\83L\83b\83v
jsontext += strspn(jsontext," \r\n\t");
+
+
//value\82ð\89ð\90Í
rslt = ParseValueUTF8(parser,JSONTypeObject,name,jsontext,&jsontext);
+
+ if(rslt != JSON_ERR_OK){
+
+ goto end;
+ }
+
+ (*endptr) = jsontext;
end:
//\95¶\8e\9a\97ñ\83o\83b\83t\83@\8aJ\95ú
//','\82ð\83X\83L\83b\83v
jsontext++;
- continue;
+ break;;
default:
//\8bó\94\92\82ð\83X\83L\83b\83v
jsontext += strspn(jsontext," \r\n\t");
- }while(JSONFalse);
+ }while(JSONTrue);
success:
rslt = JSON_ERR_OK;
//value\82ª\83I\83u\83W\83F\83N\83g\82Ì\8fê\8d\87
case '{':
- parser->dataHandler(parser->option,base,JSONTypeObject,name,(JSONParameter)NULL);
+ parser->structHandler(parser->option,base,JSONTypeObject,name);
rslt = ParseObjectUTF8(parser,jsontext,&jsontext);
//value\82ª\94z\97ñ\82Ì\8fê\8d\87
case '[':
- parser->dataHandler(parser->option,base,JSONTypeArray,name,(JSONParameter)NULL);
+ parser->structHandler(parser->option,base,JSONTypeArray,name);
rslt = ParseArrayUTF8(parser,jsontext,&jsontext);
parser->valueSize = sizeof(JSONChar) * 1024;
parser->value = (JSONChar_P)malloc(parser->valueSize);
- parser->dataHandler = defaultDataEventHandler;
- parser->dataEndHandler = defaultDataEndEventHandler;
+
+ JSONParser_setEventHandler(parser,defaultStructEventHandler,defaultDataEventHandler,defaultDataEndEventHandler);
+
return parser;
}
}
+///
+///\8d\\91¢\91Ì\83C\83x\83\93\83g\83n\83\93\83h\83\89\82ð\83Z\83b\83g
+///
+JSONVoid JSONParser_setStructHandler(JSONParser_P parser,JSONStructEventHandler handler){
+
+ parser->structHandler = handler;
+ return;
+}
///
///\83I\83u\83W\83F\83N\83g\83C\83x\83\93\83g\83n\83\93\83h\83\89\82ð\83Z\83b\83g
}
+extern JSONVoid JSONParser_setEventHandler(JSONParser_P parser,JSONStructEventHandler structHandler,JSONDataEventHandler dataHandler,JSONDataEndEventHandler endHandler){
+
+ parser->structHandler = structHandler;
+ parser->dataHandler = dataHandler;
+ parser->dataEndHandler = endHandler;
+
+ return;
+
+}
+
return;
}
+static JSONVoid defaultStructEventHandler(JSONVoid *option,JSONUInt thisHierarchy,JSONUInt valueType,JSONChar_P name){
+
+ return;
+}
+
static JSONResult ParseStringUTF8(JSONChar_P buf,JSONUInt buflen,const JSONText *jsontext,const JSONText *end,const JSONText **endptr){
JSONResult rslt = JSON_ERR_NOTINIT; //\8c\8b\89Ê
JSONChar_P indexptr; //\83C\83\93\83f\83b\83N\83X\83|\83C\83\93\83^
+ JSONUInt len;
+
+ len = U8ToU16(buf,buflen,jsontext,end);
//utf16\82É\95Ï\8a·
- if(U8ToU16(buf,buflen,jsontext,end) == 0){
+ if(len == 0){
goto converterr;
}
+
indexptr = buf;
while(indexptr[0] != L'\"'){
//\8dÅ\8cã\82Énull\95¶\8e\9a\82ð\91ã\93ü
buf[0] = L'\0';
- (*endptr) += strspn(end," \t\r\n");
+ (*endptr) = end + strspn(end," \t\r\n");
rslt = JSON_ERR_OK;
JSONResult rslt = JSON_ERR_NOTINIT;
- JSONText *end = GetJSONStringEndUTF8(jsontext);
+ JSONText *end;
- JSONUInt valueLen = getLenU8ToU16(jsontext,end);
+ JSONUInt valueLen;
+
+ jsontext++;
+
+
+ rslt = GetJSONStringEndUTF8(jsontext,&end);
+
+ if(rslt != JSON_ERR_OK){
+
+ return rslt;
+
+ }
+
+ valueLen = getLenU8ToU16(jsontext,end);
JSONParser_BufferExtend(parser,valueLen);
rslt = ParseStringUTF8(parser->value,valueLen,jsontext,end,&jsontext);
+ (*endptr) = jsontext;
return rslt;
}
JSONResult rslt;
- const JSONText *end = jsontext + strcspn(jsontext," \r\n\t");
+ const JSONText *end = jsontext + strcspn(jsontext," \r\n\t,}");
JSONUInt valueLen = getLenU8ToU16(jsontext,end);
parser->value[valueLen] = L'\0';
rslt = JSON_ERR_OK;
-
+ (*endptr) = end;
end:
return rslt;
return (allocator->heapFlag = ((sizeof(allocator->buffer) / sizeof(allocator->buffer[0])) -1 <= len)) ? (allocator->heapBuf = (JSONChar_P)malloc(sizeof(JSONChar) * len)) : allocator->buffer;
+}
+
+static JSONVoid JSONString_free(JSONStringAllocator_P allocator){
+
+ if(allocator->heapFlag){
+
+ free(allocator->heapBuf);
+ }
+
+}
+static JSONUInt U8ToU16(JSONChar_P buf,JSONUInt len,const JSONText *start,const JSONText *end){
+ return MultiByteToWideChar(CP_UTF8,0,start,end - start,buf,len);
+}
+
+static JSONUInt getLenU8ToU16(const JSONText *start,const JSONText *end){
+
+ return MultiByteToWideChar(CP_UTF8,0,start,end - start,NULL,0);
+
+}
+
+static JSONResult ParseLiteralNameUTF8(JSONText *literalName,const JSONText *jsontext,const JSONText **endptr){
+
+ JSONUInt len = strcspn(jsontext," \r\n\t");
+ JSONResult rslt = (strncmp(literalName,jsontext,len) == 0) ? JSON_ERR_OK : JSON_ERR_SYNTAX;
+
+
+ (*endptr) = jsontext + len;
+ return rslt;
+}
+
+static JSONResult GetJSONStringEndUTF8(const JSONText *jsontext,const JSONText **endptr){
+
+
+ for(;jsontext[0] != '\0' && jsontext[0] != '"';jsontext += jsontext[0] == '\\' ? 2 : 1);
+
+ if(jsontext[0] == '\0'){
+
+ return JSON_ERR_SYNTAX;
+
+ }
+
+ jsontext++;
+
+ (*endptr) = jsontext;
+ return JSON_ERR_OK;
+
+}
+
+static JSONVoid JSONParser_BufferExtend(JSONParser_P parser,JSONUInt length){
+
+ length *= sizeof(JSONChar_P);
+ if(parser->valueSize < length){
+ free(parser->value);
+ parser->value = (JSONChar_P)malloc(length);
+
+ }
+ return;
}
\ No newline at end of file
#include "nlib_include.h"
+typedef enum {
+
+ NicoMyList_Root = 0,
+ NicoMyList_mylist,
+ NicoMyList_mylistitem,
+ NicoMyList_item,
+ NicoMyList_item_data,
+
+}NicoMyListHierarchy;
+
+typedef struct {
+ UINT64 nord;
+ NLIB_RESULT errcode;
+ NicoMyListItem_P pitem;
+
+
+}NicoMyListItemContainer,*NicoMyListItemContainer_P;
+
///
///\83}\83C\83\8a\83X\83g\88ê\97\97\8eæ\93¾\8b¤\92Ê\8f\88\97\9d
///
static NLIB_RESULT NicoMyList_getList(NicoVideoAuth_P pAuth,LPCWSTR objectName,NicoMyListCallBack callback);
+static NicoMyListItem_P NicoMyListItem_new();
+
+static VOID NicoMyListItem_delete(NicoMyListItem_P *pitem);
+
+static JSONVoid myListItemStructEventHandler(JSONVoid *option,JSONUInt thisHierarchy,JSONUInt valueType,JSONChar_P name);
+
+static JSONVoid myListItemDataEventHandler(JSONVoid *option,JSONUInt thisHierarchy,JSONUInt valueType,JSONChar_P name,JSONParameter param);
+
+static JSONVoid myListItemDataEndEventHandler(JSONVoid *option,JSONUInt thisHierarchy,JSONUInt valueType,JSONChar_P param);
+
///
///\82Æ\82è\82 \82¦\82¸\83}\83C\83\8a\83X\83g\88ê\97\97\8eæ\93¾
///
DWORD readSize;
LPSTR readBuffer = NULL;
JSONParser_P jsonParser = NULL;
+ NicoMyListItemContainer itemContainer;
-
+ ZeroMemory(&itemContainer,sizeof(itemContainer));
wcscat(sendBuffer,pAuth->userSession);
hNicoVideoSession = WinHttpConnect(hHttpSession,WWW_NICO_VIDEO_DOMEINNAME,INTERNET_DEFAULT_HTTP_PORT,0);
if(WinHttpSendRequest(hGetMyList,sendBuffer,-1,WINHTTP_NO_REQUEST_DATA,0,0,0) == FALSE){
- rslt = NLIB_ERR_FAILED_NETWORK;
- goto end;
+ goto networkerr;
}
-
+
readBuffer = (LPSTR)malloc(LENGTH_65536 * 30);
jsonParser = JSONParser_new();
+ JSONParser_setEventHandler(jsonParser,myListItemStructEventHandler,myListItemDataEventHandler,myListItemDataEndEventHandler);
+
+ itemContainer.pitem = NicoMyListItem_new();
+
if(WinHttpReceiveResponse(hGetMyList,NULL) == FALSE){
- rslt = NLIB_ERR_FAILED_NETWORK;
- goto end;
+ goto networkerr;
}
if (WinHttpQueryHeaders(hGetMyList,WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER,WINHTTP_HEADER_NAME_BY_INDEX,&statusCode,&statusCodeSize,WINHTTP_NO_HEADER_INDEX) == FALSE){
- rslt = NLIB_GETPLAYERSTATUS_FAILED_CONNECT;
- goto end;
+ goto networkerr;
}
if (HTTP_STATUS_OK != statusCode){
- rslt = NLIB_GETPLAYERSTATUS_FAILED_CONNECT;
- goto end;
+ goto networkerr;
}
if(ReadHttpBody(hGetMyList,(LPBYTE)readBuffer,CHUNKMEMORY_MAXSIZE,&readSize) == FALSE){
+ goto networkerr;
+
+ }
+
+ readBuffer[readSize] = '\0';
- rslt = NLIB_GETPLAYERSTATUS_FAILED_CONNECT;
- goto end;
+ if(JSONParser_Parse(jsonParser,readBuffer,JSON_ENCODE_UTF8,&itemContainer) != JSON_ERR_OK){
+ goto jsonerr;
}
+
+
end:
free(readBuffer);
JSONParser_delete(&jsonParser);
+ NicoMyListItem_delete(&itemContainer.pitem);
WinHttpCloseHandle(hNicoVideoSession);
WinHttpCloseHandle(hGetMyList);
return rslt;
+
+networkerr:
+ rslt = NLIB_ERR_FAILED_NETWORK;
+ goto end;
+jsonerr:
+ rslt = NLIB_ERR_JSON_PARSE;
+ goto end;
+}
+
+static NicoMyListItem_P NicoMyListItem_new(){
+
+ NicoMyListItem_P self = (NicoMyListItem_P)calloc(1,sizeof(*self));
+
+ return self;
+}
+
+static VOID NicoMyListItem_delete(NicoMyListItem_P *pitem){
+
+ free(*pitem);
+ (*pitem) = NULL;
+
+ return;
}
+
+static JSONVoid myListItemStructEventHandler(JSONVoid *option,JSONUInt thisHierarchy,JSONUInt valueType,JSONChar_P name){
+
+ NicoMyListHierarchy nord = NicoMyList_Root;
+
+ NicoMyListItemContainer_P self = (NicoMyListItemContainer_P)option;
+
+ switch(self->nord){
+
+ case MAKENORD(NicoMyList_Root):
+
+ if(thisHierarchy == JSONTypeRoot && valueType == JSONTypeObject){
+
+ nord = NicoMyList_mylist;
+
+ }
+ break;
+
+ case MAKENORD1(NicoMyList_Root,NicoMyList_mylist):
+
+ if(thisHierarchy == JSONTypeObject && valueType == JSONTypeArray && wcscmp(name,L"mylistitem") == 0){
+
+ nord = NicoMyList_mylistitem;
+
+ }
+
+ break;
+
+ case MAKENORD2(NicoMyList_Root,NicoMyList_mylist,NicoMyList_mylistitem):
+
+ if(thisHierarchy == JSONTypeArray && valueType == JSONTypeObject){
+
+ nord = NicoMyList_item;
+
+ }
+
+ break;
+
+ case MAKENORD3(NicoMyList_Root,NicoMyList_mylist,NicoMyList_mylistitem,NicoMyList_item):
+
+ if(thisHierarchy == JSONTypeObject && valueType == JSONTypeObject && wcscmp(name,L"item_data") == 0){
+
+ nord = NicoMyList_item_data;
+
+ }
+
+ break;
+
+ }
+
+ self->nord = MAKENORD1(self->nord,nord);
+
+
+ return;
+}
+
+
+static JSONVoid myListItemDataEventHandler(JSONVoid *option,JSONUInt thisHierarchy,JSONUInt valueType,JSONChar_P name,JSONParameter param){
+
+ switch(valueType){
+
+
+ case JSONTypeDecimal:
+
+ dumpln(TEXT("name:[%s]:param[%s]"),name,(LPCWSTR)param);
+
+ break;
+
+ case JSONTypeString:
+
+ dumpln(TEXT("name:[%s]:param[%s]"),name,(LPCWSTR)param);
+
+ break;
+ }
+ return;
+}
+
+static JSONVoid myListItemDataEndEventHandler(JSONVoid *option,JSONUInt thisHierarchy,JSONUInt valueType,JSONChar_P name){
+
+ UPNORD(((NicoMyListItemContainer_P)option)->nord);
+
+ return;
+}
\ No newline at end of file