{ # <- first loop
"A": { # <- second loop
"0": { # <- 3rd loop
"Question": "QQ",
"Time": "xx-xx-xxxx xx:xx:xx",
"AAA": {
"BBB": [
"",
""
]
},
"CCC": ""
},
"1": {
"Question": "SS",
"Time": "xx-xx-xxxx xx:xx:xx",
"AAA": {
"BBB": [
"",
""
]
},
"CCC": ""
}
"B": {
"0": {
"Question": "OAOOAO",
"Time": "xx-xx-xxxx xx:xx:xx",
"AAA": {
"BBB": [
"",
""
]
},
"CCC": ""
},
"1": {
"Question": "^^",
"Time": "xx-xx-xxxx xx:xx:xx",
"AAA": {
"BBB": [
"",
""
]
},
"CCC": ""
}
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <json-c/json.h>
#include <json-c/json_inttypes.h>
#include <json-c/json_object.h>
#include <json-c/json_tokener.h>
#include <json-c/json_util.h>
#define DEBUG_ENV
#ifdef DEBUG_ENV
#define DEBUGF(fmt,args...) printf(fmt ,##args)
#else
#define DEBUGF(fmt,args...)
#endif
#define BUF 60*1024*1024 //60M
int ReadFile(char *path, char **jsondata)
{
int fp, ndata;
char *data = malloc(BUF * sizeof(char));
fp = open(path, O_RDONLY);
if (fp == -1)
{
printf("open file error! %s\n", __FUNCTION__);
goto ERR;
}
ndata = read (fp, data, BUF);
if (ndata < 0)
{
perror("read file error !");
goto ERR;
}
//printf("data=%s\n", data);
*jsondata = data;
return 0;
ERR:
close(fp);
return 1;
}
//#define QUESTION
#define WRITE_TO_FILE
unsigned int TC=0; //total count
int AnalysisObject(char **data, int(* WD)(char *name, int fo, char *info, int ff))
{
json_object* jobj = json_tokener_parse(*data);
json_object* jobj2;
json_object* jobj3;
enum json_type type;
char *data2, *data3;
unsigned int count=0;
char str[256];
if(WD != NULL) WD("", 1, "", 0); //open file
#ifdef QUESTION
if(WD != NULL) WD("", 0, "Count,Time,Question\n", 0);
#else
if(WD != NULL) WD("", 0, "Count,Time\n", 0);
#endif
json_object_object_foreach(jobj, key, val) //fisrt loop
{
type = json_object_get_type(val);
switch (type)
{
case json_type_object:
//printf("key:%s, value: %s\n", key, json_object_get_string(val));
DEBUGF("SN:%s\n", key);
//snprintf(str, strlen("SN:")+strlen(key)+2, "SN:%s\n", key);
//if(WD != NULL) WD("", 0, str, 0);
jobj2 = json_tokener_parse(json_object_get_string(val));
json_object_object_foreach(jobj2, jkey, jval) // second loop
{
count++; TC++;
type = json_object_get_type(jval);
switch (type)
{
case json_type_object:
DEBUGF("key:%s,", jkey);
//snprintf(str, strlen(jkey)+2, "%s,", jkey);
sprintf(str, "%d,", count);
if (WD != NULL) WD("", 0, str, 0);
jobj3 = json_tokener_parse(json_object_get_string(jval)); // 3rd loop
json_object_object_foreach(jobj3, jkey, jval)
{
if (!strncmp("Time", jkey, strlen("Time")))
{
#ifdef QUESTION
DEBUGF("time:%s,", json_object_get_string(jval));
snprintf(str, strlen(json_object_get_string(jval))+2, "%s,", json_object_get_string(jval));
if(WD != NULL) WD("", 0, str, 0);
#else
DEBUGF("time:%s\n", json_object_get_string(jval));
snprintf(str, strlen(json_object_get_string(jval))+2, "%s\n", json_object_get_string(jval));
if(WD != NULL) WD("", 0, str, 0);
#endif
}
}
#ifdef QUESTION
json_object_object_foreach(jobj3, jjkey, jjval)
{
if (!strncmp("Question", jjkey, strlen("Question")))
{
DEBUGF("question:%s,", json_object_get_string(jjval));
DEBUGF("length:%ld\n", strlen(json_object_get_string(jjval)));
snprintf(str, strlen(json_object_get_string(jjval))+2, "%s,", json_object_get_string(jjval));
if(WD != NULL) WD("", 0, str, 0);
snprintf(str, strlen("length:")+strlen(json_object_get_string(jjval))+2, "length:%ld\n", strlen(json_object_get_string(jjval)));
if(WD != NULL) WD("", 0, str, 0);
}
}
#endif
break;
}
}
break;
}
}
if(WD != NULL) WD("", 0, "", 1); //close file
json_object_put(jobj3);
json_object_put(jobj2);
json_object_put(jobj);
return 0;
}
int dir_type(struct stat stat_buff) {
// check file type from struct stat
if (S_ISDIR(stat_buff.st_mode)) {
return 1;
}
if (S_ISREG(stat_buff.st_mode)) {
return 2;
}
return 0; // consider as unknown file type
}
int ReadDir(char *pathname, char farray[][512], unsigned int *fcount)
{
DIR* dir;
struct dirent* dp;
char next[512];
static unsigned int i=0;
char *fname;
if ((dir = opendir(pathname)) == NULL) {
printf("%s: opendir error", pathname);
return 1;
}
// read directory info
while ((dp = readdir(dir)) != NULL) {
sprintf(next, "%s/%s", pathname, dp->d_name);
if (strcmp(dp->d_name, ".DS_Store") == 0 || strcmp(dp->d_name, "..") == 0 || strcmp(dp->d_name, ".") == 0) {
// skip current folder, father folder
continue;
}
struct stat stat_buff;
if (stat(next, &stat_buff) < 0) {
printf("%s: stat error", pathname);
continue;
}
// read file type
int ret_stat = dir_type(stat_buff);
switch (ret_stat) {
case 1:
//printf("Dir => %s\n", next);
ReadDir(next, farray, fcount); // for directory read child node
break;
case 2:
//printf("File => path = %s; uid = %d; size = %ld; create_time = %s", next, stat_buff.st_uid, stat_buff.st_size, ctime(&(stat_buff.st_mtime)));
fname=strstr(next, ".json");
if (fname!=NULL)
{
//printf("Fpath = %s, len=%ld\n", next, strlen(next));
strncpy(farray[i], next, 512);
i++;
}
break;
}
}
*fcount = i;
closedir(dir);
return 0;
}
int WriteData(char *fname, int f_open, char *data, int f_close)
{
static int fd;
static char name[256];
if (strlen(fname) > 0)
{
strncpy(name, fname, 251);
strcat(name, ".csv");
}
if(f_open)
fd=open(name, O_RDWR | O_CREAT, 0666);
if (fd == -1)
{
printf("open file error!, %s", __FUNCTION__);
goto ERR;
}
if (strlen(data) > 0)
if (write(fd, data, strlen(data)) < 0)
{
printf("wirte data error");
goto ERR;
}
if(f_close)
close(fd);
return 0;
ERR:
close (fd);
return 1;
}
int main(int argc, char **argv)
{
char *data;
char fname[1024][512];// file count max:1024, path+filename max:512
char *tmp_fname;
unsigned int fcount;
int i=0;
if( argc < 2 )
{
printf("provide a json path\n");
printf("it will transfer the csv file according to json file name\n");
return 1;
}
if(ReadDir(argv[1], fname, &fcount)>0)
return 1;
printf("%d, name=%s\n", 0, fname[0]);
printf("%d, name=%s\n", 1, fname[1]);
printf("%d, fcount=%d", 2, fcount);
for(i=0; i < fcount; i++)
{
if (strlen(fname[i]) > 0)
{
printf("fname[%d]=%s\n", i, fname[i]);
if (ReadFile(fname[i], &data) > 0)
goto ERR;
tmp_fname=strtok(fname[i], ".");
if (WriteData(tmp_fname, 0, "", 0) > 0)
goto ERR;
#ifdef WRITE_TO_FILE
AnalysisObject(&data, WriteData);
#else
AnalysisObject(&data, NULL);
#endif
data=NULL;
}
}
printf("There are %d users, total_count=%d\n", i, TC);
free(data);
#endif
return 0;
}
沒有留言:
張貼留言