Skip to content

Commit

Permalink
feat : list command
Browse files Browse the repository at this point in the history
  • Loading branch information
lshtar13 committed Sep 1, 2023
1 parent 11cf93b commit 26596a6
Show file tree
Hide file tree
Showing 10 changed files with 218 additions and 52 deletions.
4 changes: 2 additions & 2 deletions include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

#define CMDSIZE 512
#define URLSIZE 512
#define PATHSIZE 512
#define PATHSIZE 1024

#define VALUESIZE 32
#define ERRORISZE 1024
Expand Down Expand Up @@ -74,7 +74,7 @@ extern char cookie[PATHSIZE];
extern char repos[PATHSIZE];
extern char archives[PATHSIZE];

int parseOpt(int argc, char *argv[], const char targetOpt[], const int optNum, char *optArg[], char *caches[]);
int parseOpt(int argc, char *argv[], const char targetOpt[], const int optNum, char *optArg[], char *caches[], char flags[]);
void userLogin(const char home[]);
void userLogout(const char home[]);
char *getExtension(char *target);
Expand Down
1 change: 1 addition & 0 deletions include/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ int deleteTestcasesHTTP(const char home[], const char repoID[], cJSON *testcases
int userProblemScoreHTTP(const char home[], const char repoID[], const char problemID[],const char userName[], cJSON **responseJson);
int userRepoScoreHTTP(const char home[], const char repoID[],const char userName[], cJSON **responseJson);
int problemScoreHTTP(const char home[], const char repoID[],const char problemID[],cJSON **responseJson);
int getAllReposHTTP(const char home[], cJSON **responseJson);
#endif
4 changes: 2 additions & 2 deletions lib/common.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "common.h"

int parseOpt(int argc, char *argv[], const char targetOpt[], const int optNum,
char *optArg[], char *caches[])
char *optArg[], char *caches[], char flags[])
{
int c, result = 0;
int flags[MAXOPT] = {0};
// int flags[MAXOPT] = {0};

while ((c = getopt(argc, argv, targetOpt)) != -1)
for (int i = 0; i < optNum; ++i)
Expand Down
51 changes: 51 additions & 0 deletions lib/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -972,4 +972,55 @@ int deleteTestcasesHTTP(const char home[], const char repoID[], cJSON *testcases
}

return 0;
}

int getAllReposHTTP(const char home[], cJSON **responseJson)
{
char url[URLSIZE], response[BUFSIZE];
CURL *curl;
CURLcode res;
struct curl_slist *list = NULL;
long stat;

memset(url, 0, URLSIZE);
sprintf(url, "%s/repos", home);

curl = curl_easy_init();

if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_PORT, 4000L);

curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 5000L);

curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookie);
list = curl_slist_append(list, "Accept: */*");
list = curl_slist_append(list, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);

char response[BUFSIZE];
writeidx = 0;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, response);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, plainWrite);

res = curl_easy_perform(curl);
if (res != CURLE_OK) {
return -1;
}

curl_easy_getinfo(curl, CURLINFO_HTTP_CODE, &stat);
if(stat == 401){
curl_easy_cleanup(curl);
userLogin(home);
return getAllReposHTTP(home,responseJson);
}

*responseJson = (cJSON*) cJSON_Parse(response);
curl_easy_cleanup(curl);
} else {
return -1;
}

return 0;
}
Empty file added localhost
Empty file.
1 change: 0 additions & 1 deletion miniotest.txt

This file was deleted.

30 changes: 20 additions & 10 deletions src/manage.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "common.h"

int listInfo(int argc, char*argv[]);
int listRepos(const char home[]);
int listProblems(const char home[],const char repoName[]);
int createUser(const char home[], const char username[], const char password[]);
int createUsers(const char home[], const char location[]);
int enrollUser(const char home[], const char username[], const char repoName[]);
Expand All @@ -13,16 +14,22 @@ int repoScore(const char home[],const char repoName[], const char location[]);
static int list(int argc, char*argv[])
{
char error[ERRORISZE];
char home[VALUESIZE], problemName[VALUESIZE], repoName[VALUESIZE];
char *values[] = {home, repoName, problemName,NULL};
char *cache[] = {homeCache, NULL, NULL};
char home[VALUESIZE], repoName[VALUESIZE];
char *values[] = {home, repoName};
char *cache[] = {homeCache, NULL};
char flags[2] = {0};

fprintf(stderr,"list : ");

int opts = parseOpt(argc,argv,"h:r:p:",3,values,cache);
int opts = parseOpt(argc,argv,"h:r:",2,values,cache,flags);

switch(opts){
case 3: case 2: case 1: case 0:
listInfo(opts,values);
case 1:
listRepos(home);
break;

case 2:
listProblems(home,repoName);
break;

default:
Expand All @@ -44,9 +51,10 @@ static int score(int argc, char*argv[])
char home[VALUESIZE]={0},userName[VALUESIZE]={0},repoName[VALUESIZE]={0},problemName[VALUESIZE]={0},location[PATHSIZE]={0};
char *values[] = {home, userName, repoName,problemName,location};
char *cache[] = {homeCache,NULL,repoCache,NULL,locationCache};
char flags[5] = {0};

fprintf(stderr,"score : ");
int opts = parseOpt(argc,argv,"h:u:r:p:l:",5,values,cache);
int opts = parseOpt(argc,argv,"h:u:r:p:l:",5,values,cache,flags);
if(!home[0] || !repoName[0]){
sprintf(error,"missing opts");
goto exception;
Expand Down Expand Up @@ -79,9 +87,10 @@ static int enroll(int argc, char*argv[])
char home[VALUESIZE]={0},username[VALUESIZE]={0},repoName[VALUESIZE]={0},location[PATHSIZE]={0};
char *values[] = {home, username, repoName,location};
char *cache[] = {homeCache, NULL, repoCache,NULL};
char flags[4] = {0};

fprintf(stderr,"enroll : ");
int opts = parseOpt(argc,argv,"h:u:r:l:",4,values,cache);
int opts = parseOpt(argc,argv,"h:u:r:l:",4,values,cache,flags);
if(!home[0]){
sprintf(error,"missing home");
goto exception;
Expand Down Expand Up @@ -116,9 +125,10 @@ static int create(int argc, char*argv[])
char home[VALUESIZE]={0},username[VALUESIZE]={0},password[VALUESIZE]={0},location[PATHSIZE]={0};
char *values[] = {home, username, password,location};
char *cache[] = {homeCache, NULL, NULL,NULL};
char flags[4] = {0};

fprintf(stderr,"create : ");
int opts = parseOpt(argc,argv,"h:u:p:l:",4,values,cache);
int opts = parseOpt(argc,argv,"h:u:p:l:",4,values,cache,flags);
if(!home[0]){
sprintf(error,"missing home");
goto exception;
Expand Down
112 changes: 88 additions & 24 deletions src/manage.service.c
Original file line number Diff line number Diff line change
@@ -1,42 +1,106 @@
#include "common.h"
#include "http.h"

int printInfo(const char path[])
int updateInfo(const char home[])
{
struct info info;
getInfoByPath(path,&info);
char error[ERRORISZE];
char path[PATHSIZE]; sprintf(path,"%s",repos);

fprintf(stderr,"title : %s\n",info.title);
fprintf(stderr,"description : %s\n\n",info.description);
cJSON *response = NULL, *content = NULL;
if(getAllReposHTTP(home,&response)<0 || !response || !(content = cJSON_GetObjectItem(response,"content"))){
fprintf(stderr,"fail to get repo info in http connection ...");
return -1;
}

return 0;
int repoNum = cJSON_GetArraySize(content);
for(int i = 0; i<repoNum; ++i){
cJSON *repo = cJSON_GetArrayItem(content,i);
struct info repoInfo;
if (!repo)
continue;

cJSON *repoId = cJSON_GetObjectItem(repo,"repoId");
cJSON *repoName = cJSON_GetObjectItem(repo,"repoName");
if(!repoId || !repoName){
fprintf(stderr,"faile to get repo info ...");
return -1;
}

exception:
return -1;
char id[IDSIZE]; sprintf(id,"%d",repoId->valueint);repoInfo.id = id;
char name[VALUESIZE]; sprintf(name,"%s",repoName->valuestring);repoInfo.title = name;
char path[PATHSIZE]; sprintf(path,"%s/%s/%s",repos,home,repoInfo.title);
mkdir(path, S_IRWXU|S_IRWXO);setInfo(home,repoInfo.title,NULL,&repoInfo);

cJSON *problems = NULL;
if(getReposHTTP(home,repoInfo.id,&problems)<0 || !problems || !(problems = cJSON_GetObjectItem(problems,"Problem")))
continue;

cJSON *problem = NULL;
int problemNum = cJSON_GetArraySize(problems);
for(int i = 0; i<problemNum; ++i){
cJSON *problem = cJSON_GetArrayItem(problems,i);
if(!problem)
continue;
char id[IDSIZE]; sprintf(id,"%d",cJSON_GetObjectItem(problem,"id")->valueint);
char title[VALUESIZE]; sprintf(title,"%s",cJSON_GetObjectItem(problem,"title")->valuestring);
char description[STRSIZE]; sprintf(description,"%s",cJSON_GetObjectItem(problem,"text")->valuestring);
struct info problemInfo = {.id = id, .title = title, .description = description};
char path[PATHSIZE]; sprintf(path,"%s/%s/%s/%s",repos,home,repoInfo.title,problemInfo.title);
mkdir(path, S_IRWXU|S_IRWXO);setInfo(home,repoInfo.title,problemInfo.title,&problemInfo);
}
}

return 0;
}

int listInfo(int argc, char*argv[])
int listRepos(const char home[])
{
char error[ERRORISZE];
char path[PATHSIZE]; sprintf(path,"%s",repos);
for(int i = 0; i < argc; ++i)
sprintf(path,"%s/%s",path,argv[i]);
if(updateInfo(home)<0){
fprintf(stderr,"fail to update informations ...");
return -1;
}

fprintf(stderr, "Workbook List:\n");
fprintf(stderr, "%-10s | %-30s \n", "ID", "Workbook Title");
char path[PATHSIZE]; sprintf(path,"%s/%s",repos,home);
DIR *dir = opendir(path);
struct dirent *dent;
while((dent = readdir(dir))){
if(dent->d_type != DT_DIR || dent->d_name[0] == '.')
continue;

struct info repoInfo;
if(getInfo(home,dent->d_name,NULL,&repoInfo)<0)
continue;

fprintf(stderr, "%-10s | %-30s \n", repoInfo.id, repoInfo.title);
}

DIR *dir;
if((dir = opendir(path)) == NULL){
fprintf(stderr,"opendir ");
return 0;
}

int listProblems(const char home[],const char repoName[])
{
if(updateInfo(home)<0){
fprintf(stderr,"fail to update informations ...");
return -1;
}

fprintf(stderr,"\n=========================================\n");
for(struct dirent *dent = readdir(dir); dent; dent = readdir(dir)){
char target[PATHSIZE];
sprintf(target,"%s/%s",path,dent->d_name);
if(dent->d_type == DT_DIR && strncmp(dent->d_name,"..",2) &&
strcat(target,"/info.json") && !printInfo(target))
;
fprintf(stderr, "Problem List:\n");
fprintf(stderr, "%-10s | %-30s | %s\n", "ID", "Problem Title","Description");
char path[PATHSIZE]; sprintf(path,"%s/%s/%s",repos,home,repoName);
DIR *dir = opendir(path);
struct dirent *dent;
while((dent = readdir(dir))){
if(dent->d_type != DT_DIR || dent->d_name[0] == '.')
continue;

struct info problemInfo;
if(getInfo(home,repoName,dent->d_name,&problemInfo)<0)
continue;

fprintf(stderr, "%-10s | %-30s | %s\n", problemInfo.id, problemInfo.title, problemInfo.description);
}
fprintf(stderr,"=========================================\n");

return 0;
}
Expand Down
15 changes: 10 additions & 5 deletions src/workbook.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ static int delete(int argc, char *argv[])
char home[VALUESIZE]={0},problemName[VALUESIZE] = {0},repoName[VALUESIZE] = {0},testcaseName[VALUESIZE] = {0};
char *values[] = {home,problemName,repoName,testcaseName};
char *cache[] = {homeCache,NULL,repoCache,NULL};
char flags[4] = {0};

fprintf(stderr,"delete : ");
int opts = parseOpt(argc,argv,"h:p:r:t:",4,values,cache);
int opts = parseOpt(argc,argv,"h:p:r:t:",4,values,cache,flags);
fprintf(stderr,"h %d p %d r %d t %d \n",flags[0],flags[1],flags[2],flags[3]);
if(opts<2){
fprintf(stderr,"Missing opts...\n");
exit(EXIT_FAILURE);
}else if(opts == 4)
}else if(flags[3])
deleteTestcases(home,repoName,problemName);
else if(problemName[0] && repoName[0])
else if(flags[1] && repoName[0])
deleteProblem(home, repoName, problemName);
else if(repoName[0])
deleteRepo(home, repoName);
Expand All @@ -39,9 +41,10 @@ static int append(int argc, char*argv[])
char home[VALUESIZE]={0},location[VALUESIZE]={0},repoName[VALUESIZE]={0},problemName[VALUESIZE]={0};
char *values[] = {home,location,repoName,problemName};
char *cache[] = {homeCache, locationCache, repoCache, NULL};
char flags[4] = {0};

fprintf(stderr,"append : ");
int opts = parseOpt(argc,argv,"h:l:r:p:",4,values,cache);
int opts = parseOpt(argc,argv,"h:l:r:p:",4,values,cache,flags);
if(opts<3){
fprintf(stderr,"Missing opts...\n");
exit(EXIT_FAILURE);
Expand Down Expand Up @@ -69,7 +72,9 @@ static int create(int argc, char*argv[])
char home[VALUESIZE] , location[VALUESIZE], repoName[VALUESIZE];
char *values[] = {home,location,repoName};
char *cache[] = {homeCache, locationCache, repoCache};
if(parseOpt(argc,argv,"h:l:r:",3,values,cache)<3){
char flags[4] = {0};

if(parseOpt(argc,argv,"h:l:r:",3,values,cache,flags)<3){
fprintf(stderr,"Missing opts...\n");
exit(EXIT_FAILURE);
}
Expand Down
Loading

0 comments on commit 26596a6

Please sign in to comment.