static int http_response_process(void * arg) { struct json_object *resp; if(ghttp_process(request)==ghttp_done) { resp=json_tokener_parse(ghttp_get_body(request)); resp_ret.type=json_object_get_int(json_object_object_get(resp,RESP_TYPE)); resp_ret.result=json_object_get_int(json_object_object_get(resp,RESP_RESULT)); if(resp_ret.type==REQ_REGISTER) { json_object_object_del(resp, RESP_TYPE); json_object_to_file((char *)arg,resp); } else { if((resp_ret.type==REQ_SUBMIT)&&(!resp_ret.result)) { sprintf(qrcode_buf,format_qrcode,machine_id,*p_order_id,info_version); pid_t pid=vfork(); if(!pid) execlp("qrencode","qrencode","-s 6","-m 2","-lH","-oqrcode.png",qrcode_buf,0); else wait(NULL); } } json_object_put(resp); return 0; } else return -1; }
/* Added functions for supporting features */ int createproject (const char *file, int ndirs, const char *srcdirs[]) { json_object *proj = NULL; int success = 0; json_object *dirs = NULL; int i; /* Build only json file */ proj = json_object_new_object(); if (proj) { json_object_object_add(proj, NAME, json_object_new_string(file)); json_object_object_add(proj, NOFSRCDIRS, json_object_new_int(ndirs)); dirs = json_object_new_array(); for (i = 1; i <= ndirs; i++) { json_object_array_add(dirs, json_object_new_string(srcdirs[i - 1])); } json_object_object_add(proj, SRCDIRS, dirs); } json_object_to_file(file, proj); json_object_put(proj); openproject(file); return success; }
static void test_write_to_file() { json_object *jso; jso = json_tokener_parse("{" "\"foo\":1234," "\"foo1\":\"abcdefghijklmnopqrstuvwxyz\"," "\"foo2\":\"abcdefghijklmnopqrstuvwxyz\"," "\"foo3\":\"abcdefghijklmnopqrstuvwxyz\"," "\"foo4\":\"abcdefghijklmnopqrstuvwxyz\"," "\"foo5\":\"abcdefghijklmnopqrstuvwxyz\"," "\"foo6\":\"abcdefghijklmnopqrstuvwxyz\"," "\"foo7\":\"abcdefghijklmnopqrstuvwxyz\"," "\"foo8\":\"abcdefghijklmnopqrstuvwxyz\"," "\"foo9\":\"abcdefghijklmnopqrstuvwxyz\"" "}"); const char *outfile = "json.out"; int rv = json_object_to_file(outfile, jso); printf("%s: json_object_to_file(%s, jso)=%d\n", (rv == 0) ? "OK" : "FAIL", outfile, rv); if (rv == 0) stat_and_cat(outfile); printf("\n"); const char *outfile2 = "json2.out"; rv = json_object_to_file_ext(outfile2, jso, JSON_C_TO_STRING_PRETTY); printf("%s: json_object_to_file_ext(%s, jso, JSON_C_TO_STRING_PRETTY)=%d\n", (rv == 0) ? "OK" : "FAIL", outfile2, rv); if (rv == 0) stat_and_cat(outfile2); }
void set_json_val(char *name, char *key) { json_object *pobj = NULL; pobj = json_object_from_file("./conf.json"); SetValByKey(pobj, name, key); json_object_to_file("./conf.json", pobj); json_object_put(pobj); }
json_return *get_val(char *name) { json_object *pobj = NULL; pobj = json_object_from_file("./conf.json"); json_return *back; back = GetValByKey(pobj, name); json_object_to_file("./conf.json", pobj); json_object_put(pobj); return back; }
int main (int argc, char *argv[]) { char * filename; if (argc < 4 || argc < 3 || argc < 2 || argc < 1) show_usage(argv[0], TRUE, -1); filename = argv[1]; if (file_exists(filename) != 1) { file_write(filename, "{ }"); } char * cNewValue = argv[argc-1]; json_object * jobj = json_object_from_file(filename); json_object * jobj2 = jobj; json_object * jLast; json_object * jobj3; int i; char *key; //skip the first argument and the last argument for(i = 2; i < argc-1; i++) { key = argv[i]; jLast = jobj2; if (i == argc-2) { jobj3 = json_object_new_string(argv[argc-1]); json_object_get(jobj3); json_object_object_add(jLast, key, (jobj3)); json_object_put(jobj3); break; } //this gets an existing field, if it exists, otherwise creates a new one. if (json_object_object_get_ex(jLast, key, &jobj2) != TRUE) { jobj2 = json_object_new_object(); } json_object_object_add(jLast, key, json_object_get(jobj2)); } json_object_to_file(filename, jobj); exit(0); }
bool LaunchPoint::toFile() const { // NOTE: we only support persisting dynamic launchpoints at this point if (!isRemovable() || isDefault()) return false; // dynamic launch points have appinfo files named by their launchpointid's std::string filePath = Settings::LunaSettings()->lunaLaunchPointsPath; if (filePath[filePath.size()-1] != '/') filePath += "/"; filePath += launchPointId(); // persist a launch points appinfo json_object* json = toJSON(); int res = json_object_to_file((char*)filePath.c_str(), json); if (json && !is_error(json)) json_object_put(json); return res != -1; }
GDALDataset *ARGDataset::CreateCopy( const char *pszFilename, GDALDataset *poSrcDS, int /* bStrict */ , char ** /* papszOptions */ , GDALProgressFunc /* pfnProgress */ , void * /*pProgressData */ ) { const int nBands = poSrcDS->GetRasterCount(); if( nBands != 1 ) { CPLError( CE_Failure, CPLE_NotSupported, "ARG driver doesn't support %d bands. Must be 1 band.", nBands ); return nullptr; } CPLString pszDataType; int nPixelOffset = 0; GDALDataType eType = poSrcDS->GetRasterBand(1)->GetRasterDataType(); if( eType == GDT_Unknown || eType == GDT_CInt16 || eType == GDT_CInt32 || eType == GDT_CFloat32 || eType == GDT_CFloat64 ) { CPLError( CE_Failure, CPLE_NotSupported, "ARG driver doesn't support data type %s.", GDALGetDataTypeName(eType) ); return nullptr; } else if (eType == GDT_Int16) { pszDataType = "int16"; nPixelOffset = 2; } else if (eType == GDT_Int32) { pszDataType = "int32"; nPixelOffset = 4; } else if (eType == GDT_Byte) { pszDataType = "uint8"; nPixelOffset = 1; } else if (eType == GDT_UInt16) { pszDataType = "uint16"; nPixelOffset = 2; } else if (eType == GDT_UInt32) { pszDataType = "uint32"; nPixelOffset = 4; } else if (eType == GDT_Float32) { pszDataType = "float32"; nPixelOffset = 4; } else if (eType == GDT_Float64) { pszDataType = "float64"; nPixelOffset = 8; } double adfTransform[6]; poSrcDS->GetGeoTransform( adfTransform ); const char *pszWKT = poSrcDS->GetProjectionRef(); OGRSpatialReference oSRS; OGRErr nErr = oSRS.importFromWkt(pszWKT); if (nErr != OGRERR_NONE) { CPLError( CE_Failure, CPLE_NotSupported, "Cannot import spatial reference WKT from source dataset."); return nullptr; } int nSrs = 0; if (oSRS.GetAuthorityCode("PROJCS") != nullptr) { nSrs = atoi(oSRS.GetAuthorityCode("PROJCS")); } else if (oSRS.GetAuthorityCode("GEOGCS") != nullptr) { nSrs = atoi(oSRS.GetAuthorityCode("GEOGCS")); } else { // could not determine projected or geographic code // default to EPSG:3857 if no code could be found nSrs = 3857; } /********************************************************************/ /* Create JSON companion file. */ /********************************************************************/ const CPLString osJSONFilename = GetJsonFilename(pszFilename); json_object *poJSONObject = json_object_new_object(); char **pszTokens = poSrcDS->GetMetadata(); const char *pszLayer = CSLFetchNameValue(pszTokens, "LAYER"); if ( pszLayer == nullptr) { // Set the layer json_object_object_add(poJSONObject, "layer", json_object_new_string( CPLGetBasename(osJSONFilename) )); } else { // Set the layer json_object_object_add(poJSONObject, "layer", json_object_new_string( pszLayer )); } // Set the type json_object_object_add(poJSONObject, "type", json_object_new_string("arg")); // Set the datatype json_object_object_add(poJSONObject, "datatype", json_object_new_string(pszDataType)); const int nXSize = poSrcDS->GetRasterXSize(); const int nYSize = poSrcDS->GetRasterYSize(); // Set the number of rows json_object_object_add(poJSONObject, "rows", json_object_new_int(nYSize)); // Set the number of columns json_object_object_add(poJSONObject, "cols", json_object_new_int(nXSize)); // Set the xmin json_object_object_add(poJSONObject, "xmin", json_object_new_double(adfTransform[0])); // Set the ymax json_object_object_add(poJSONObject, "ymax", json_object_new_double(adfTransform[3])); // Set the cellwidth json_object_object_add(poJSONObject, "cellwidth", json_object_new_double(adfTransform[1])); // Set the cellheight json_object_object_add(poJSONObject, "cellheight", json_object_new_double(-adfTransform[5])); // Set the xmax json_object_object_add(poJSONObject, "xmax", json_object_new_double(adfTransform[0] + nXSize * adfTransform[1])); // Set the ymin json_object_object_add(poJSONObject, "ymin", json_object_new_double(adfTransform[3] + nYSize * adfTransform[5])); // Set the xskew json_object_object_add(poJSONObject, "xskew", json_object_new_double(adfTransform[2])); // Set the yskew json_object_object_add(poJSONObject, "yskew", json_object_new_double(adfTransform[4])); if (nSrs > 0) { // Set the epsg json_object_object_add(poJSONObject, "epsg", json_object_new_int(nSrs)); } if (json_object_to_file(const_cast<char *>(osJSONFilename.c_str()), poJSONObject) < 0) { CPLError( CE_Failure, CPLE_NotSupported, "ARG driver can't write companion file."); json_object_put(poJSONObject); poJSONObject = nullptr; return nullptr; } json_object_put(poJSONObject); poJSONObject = nullptr; VSILFILE *fpImage = VSIFOpenL(pszFilename, "wb"); if (fpImage == nullptr) { CPLError( CE_Failure, CPLE_NotSupported, "ARG driver can't create data file %s.", pszFilename); // remove JSON file VSIUnlink( osJSONFilename.c_str() ); return nullptr; } // only 1 raster band GDALRasterBand *poSrcBand = poSrcDS->GetRasterBand( 1 ); #ifdef CPL_LSB bool bNative = false; #else bool bNative = true; #endif RawRasterBand *poDstBand = new RawRasterBand( fpImage, 0, nPixelOffset, nPixelOffset * nXSize, eType, bNative, nXSize, nYSize, RawRasterBand::OwnFP::NO); int nXBlockSize, nYBlockSize; poSrcBand->GetBlockSize(&nXBlockSize, &nYBlockSize); void *pabyData = CPLMalloc(nXBlockSize * nPixelOffset); // convert any blocks into scanlines for (int nYBlock = 0; nYBlock * nYBlockSize < nYSize; nYBlock++) { for (int nYScanline = 0; nYScanline < nYBlockSize; nYScanline++) { if ((nYScanline+1) + nYBlock * nYBlockSize > poSrcBand->GetYSize() ) { continue; } for (int nXBlock = 0; nXBlock * nXBlockSize < nXSize; nXBlock++) { int nXValid; if( (nXBlock+1) * nXBlockSize > poSrcBand->GetXSize() ) nXValid = poSrcBand->GetXSize() - nXBlock * nXBlockSize; else nXValid = nXBlockSize; CPLErr eErr = poSrcBand->RasterIO(GF_Read, nXBlock * nXBlockSize, nYBlock * nYBlockSize + nYScanline, nXValid, 1, pabyData, nXBlockSize, 1, eType, 0, 0, nullptr); if (eErr != CE_None) { CPLError(CE_Failure, CPLE_AppDefined, "Error reading."); CPLFree( pabyData ); delete poDstBand; VSIFCloseL( fpImage ); return nullptr; } eErr = poDstBand->RasterIO(GF_Write, nXBlock * nXBlockSize, nYBlock * nYBlockSize + nYScanline, nXValid, 1, pabyData, nXBlockSize, 1, eType, 0, 0, nullptr); if (eErr != CE_None) { CPLError(CE_Failure, CPLE_AppDefined, "Error writing."); CPLFree( pabyData ); delete poDstBand; VSIFCloseL( fpImage ); return nullptr; } } } } CPLFree( pabyData ); delete poDstBand; VSIFCloseL( fpImage ); return reinterpret_cast<GDALDataset *>( GDALOpen( pszFilename, GA_ReadOnly ) ); }
int main(int argc, char *argv[]) { /* Usage: ./a.out SRC1 SRC2 * * merge json document SRC1 and SRC2 into one. * * if SRC1 and SRC2 has the same key, the value in SRC1 will be * overwritten. */ struct json_object *json1; struct json_object *json2; struct json_object *json3; int i; long nsec; char src1[BUF_MAX], src2[BUF_MAX]; { FILE *fp; fp = fopen(argv[1], "r"); if (!fp) { fprintf(stderr, "error: cannot open %s\n", argv[1]); return 1; } fread(src1, 1, BUF_MAX, fp); fclose(fp); fp = fopen(argv[2], "r"); if (!fp) { fprintf(stderr, "error: cannot open %s\n", argv[2]); return 1; } fread(src2, 1, BUF_MAX, fp); fclose(fp); } DIFFTIME(nsec) { for (i = 0; i < 10000; i++) { json1 = json_tokener_parse(src1); json2 = json_tokener_parse(src2); json3 = json_object_new_object(); //json_object_array_add(json3, json1); //json_object_array_add(json3, json2); //json_object_to_file("/dev/stdout", json3); { json_object_object_foreach(json1, key, val) { //fprintf(stderr, "adding %s\n", key); json_object_object_add(json3, key, val); json_object_get(val); } } { json_object_object_foreach(json2, key, val) { //fprintf(stderr, "adding %s\n", key); json_object_object_add(json3, key, val); json_object_get(val); } } #if 0 if (i == 10000 - 1) json_object_to_file("/dev/stdout", json3); #endif /* 0 */ json_object_put(json3); json_object_put(json1); json_object_put(json2); }
int refreshtoken() { char buff[1024]; char ATfile[1024]; char Refresh_Token[100]; json_object *json_get; sprintf(ATfile,"%s/Access_Token",confpath); if (!(json_get = json_object_from_file(ATfile))) { //如果当前目录下面没有.Access_Token文件,那么直接调用gettoken return gettoken(); } else { json_object *jrefresh_token; json_object_object_get_ex(json_get, "refresh_token",&jrefresh_token); strcpy(Refresh_Token, json_object_get_string(jrefresh_token)); json_object_put(json_get); snprintf(buff, sizeof(buff) - 1, "https://openapi.baidu.com/oauth/2.0/token?" "grant_type=refresh_token&" "refresh_token=%s&" "client_id=%s&" "client_secret=%s", Refresh_Token, ak, sk); FILE *tpfile = tmpfile(); if (!tpfile) { int lasterrno = errno; errorlog("create temp file error:%s\n", strerror(errno)); return -lasterrno; } Http *r = Httpinit(buff); if (r == NULL) { int lasterrno = errno; errorlog("can't resolve domain:%s\n", strerror(errno)); fclose(tpfile); return -lasterrno; } r->method = get; r->writefunc = savetofile; r->writeprame = tpfile; if ((errno = request(r)) != CURLE_OK) { errorlog("network error:%d\n", errno); fclose(tpfile); Httpdestroy(r); return -EPROTO; } Httpdestroy(r); json_get = json_object_from_FILE(tpfile); fclose(tpfile); if (json_get == NULL) { errorlog("json_object_from_FILE filed!\n"); return -EPROTO; } json_object *jerror_code; if (json_object_object_get_ex(json_get, "error_code",&jerror_code)) { int errorno = json_object_get_int(jerror_code) ; json_object_put(json_get); return handleerror(errorno); } json_object *jaccess_token; if (json_object_object_get_ex(json_get, "access_token",&jaccess_token)) { //找到access_token了,存到文件里面 strcpy(Access_Token, json_object_get_string(jaccess_token)); json_object_to_file(ATfile, json_get); json_object_put(json_get); } else { puts("Authorization error!"); json_object_put(json_get); errno = EPERM; return -errno; } } return 0; }
/*获得Access_Token * 我曾经把自己模拟成浏览器手动post用户名密码,开始挺好使的 * 后来不行了……因为登录次数多了居然要输验证码了!!! */ int gettoken() { char code[200]; char buff[1024]; char ATfile[1024]; json_object *json_get; sprintf(ATfile,"%s/Access_Token",confpath); if (!(json_get = json_object_from_file(ATfile))) { //如果当前目录下面没有.Access_Token文件,那么重新获取 fprintf(stderr, "You have to login first!\n"); if (fork() == 0) { snprintf(buff, sizeof(buff) - 1, "x-www-browser " //什么?你说没有x-www-browser?那可不关我的事,你自己搞定吧 "-new-tab " "\"http://openapi.baidu.com/oauth/2.0/authorize?" "response_type=code&" "client_id=%s&" "redirect_uri=oob&" "scope=netdisk&" "display=page\"" , ak); system(buff); exit(errno); } else { puts("please copy the authorization code from the browser:"); scanf("%199s", code); sprintf(buff, "https://openapi.baidu.com/oauth/2.0/token?" "grant_type=authorization_code&" "code=%s&" "client_id=%s&" "client_secret=%s&" "redirect_uri=oob" , code, ak, sk); FILE *tpfile = tmpfile(); if (!tpfile) { int lasterrno = errno; errorlog("create temp file error:%s\n", strerror(errno)); return -lasterrno; } Http *r = Httpinit(buff); if (r == NULL) { int lasterrno = errno; errorlog("can't resolve domain:%s\n", strerror(errno)); fclose(tpfile); return -lasterrno; } r->method = get; r->writefunc = savetofile; r->writeprame = tpfile; if ((errno = request(r)) != CURLE_OK) { errorlog("network error:%d\n", errno); fclose(tpfile); Httpdestroy(r); return -EPROTO; } Httpdestroy(r); json_get = json_object_from_FILE(tpfile); fclose(tpfile); if (json_get == NULL) { errorlog("json_object_from_FILE filed!\n"); return -EPROTO; } json_object *jerror_code; if (json_object_object_get_ex(json_get, "error_code",&jerror_code)) { int errorno = json_object_get_int(jerror_code) ; json_object_put(json_get); return handleerror(errorno); } json_object *jaccess_token; if (json_object_object_get_ex(json_get, "access_token",&jaccess_token)) { //找到access_token了,存到文件里面 strcpy(Access_Token, json_object_get_string(jaccess_token)); json_object_to_file(ATfile, json_get); json_object_put(json_get); } else { puts("Authorization error!"); remove(ATfile); json_object_put(json_get); errno = EPERM; return -errno; } } } else { //如果文件里面存在,直接读取文件,当然,这里没有考虑有不怀好意的人修改我的文件的情况 json_object *jaccess_token; json_object_object_get_ex(json_get, "access_token",&jaccess_token); strcpy(Access_Token, json_object_get_string(jaccess_token)); json_object_put(json_get); } return 0; }