>>106829932
make sure to use -std=gnu89
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
typedef struct{char*n;int r;struct tm t;}S;
typedef struct{int s,r;S**h;}W;
S*wp(W*w,char*n){
int i=0;
for(;w->h && w->h[i];i++);
w->h=realloc(w->h,(i+2)*8);
w->h[i]=malloc(sizeof(S));
if(!n)
n=malloc(100),sprintf(n,"%d",i);
w->h[i+1]=0;
w->h[i]->n=n;
return w->h[i];
}
jk(FILE*fp,char*b){
char c;
fscanf(fp," %c",&c);
if(fscanf(fp," \"%[^\"]\" :",b) == 1)
return 1;
fscanf(fp,"}");
return 0;
}
sr(FILE*fp,S*s){
char b[99];
while(jk(fp,b)){
if(!strcmp(b,"count")){
fscanf(fp," %d",&s->r);
}
if(!strcmp(b,"date")){
fscanf(fp," \"%[^\"]\"",b);
strptime(b,"%Y-%02m-%02d",&s->t);
}
}
}
wr(FILE*fp,W*w){
char b[99];
while(jk(fp,b)){
if(!strcmp(b,"sets"))
fscanf(fp," %d",&w->s);
if(!strcmp(b,"reps"))
fscanf(fp," %d",&w->r);
if(!strcmp(b,"history")){
while(jk(fp,b)){
sr(fp,wp(w,strdup(b)));
}
}
}
}
char*fn="./data.json";
main(int argc,char**argv){
int i;
FILE*fp=fopen(fn, "r+");
W w={0};
if(fp) wr(fp,&w),fclose(fp);
for(i=1; i<argc; i++){
int r=atoi(argv[i]);
w.s++,w.r+=r;
S*s=wp(&w,0);
s->r=r;
time_t t=time(0);
s->t=*localtime(&t);
}
printf("Reps: %d\nSets: %d\n", w.r, w.s);
fp=fopen(fn,"w+");
fprintf(fp,"{\"sets\":%d,\"reps\":%d,\"history\":{",w.s,w.r);
for(i=0;w.h && w.h[i];i++){
S s=*w.h[i];
char buf[99];
strftime(buf,99,"%Y-%02m-%02d",&s.t);
fprintf(fp,"\"%s\":{\"date\":\"%s\",\"count\":%d}%s",s.n,buf,s.r,w.h[i+1]?",":"}}");
}
}