added segments array
This commit is contained in:
parent
1aa1ed514a
commit
c24f516380
@ -74,6 +74,8 @@ int main(int argc, char *argv[]) {
|
|||||||
commandcode = 12;
|
commandcode = 12;
|
||||||
} else if (!strcmp(argv[1], "save")) {
|
} else if (!strcmp(argv[1], "save")) {
|
||||||
commandcode = 13;
|
commandcode = 13;
|
||||||
|
} else if (!strcmp(argv[1], "count")) {
|
||||||
|
commandcode = 14;
|
||||||
} else {
|
} else {
|
||||||
perror("No valid command given");
|
perror("No valid command given");
|
||||||
exit(1);
|
exit(1);
|
||||||
@ -91,7 +93,7 @@ int main(int argc, char *argv[]) {
|
|||||||
//bzero(buffer,256);
|
//bzero(buffer,256);
|
||||||
|
|
||||||
//read an int response
|
//read an int response
|
||||||
if (commandcode < 11) {
|
if (commandcode < 11 || commandcode == 14) {
|
||||||
int x = -1;
|
int x = -1;
|
||||||
n = read(sockfd, &x, sizeof(int));
|
n = read(sockfd, &x, sizeof(int));
|
||||||
|
|
||||||
|
@ -30,6 +30,11 @@ struct run_event {
|
|||||||
enum event_type type;
|
enum event_type type;
|
||||||
struct timespec time;
|
struct timespec time;
|
||||||
};
|
};
|
||||||
|
struct segment {
|
||||||
|
char *shortname;
|
||||||
|
char *longname;
|
||||||
|
char *description;
|
||||||
|
};
|
||||||
|
|
||||||
char* current_category = NULL;
|
char* current_category = NULL;
|
||||||
char* current_route = NULL;
|
char* current_route = NULL;
|
||||||
@ -46,6 +51,8 @@ int files = 0;
|
|||||||
char **filePaths = NULL;
|
char **filePaths = NULL;
|
||||||
char **names, **values;
|
char **names, **values;
|
||||||
int valuecount;
|
int valuecount;
|
||||||
|
struct segment *segments;
|
||||||
|
int segment_count = 0;
|
||||||
|
|
||||||
//functions
|
//functions
|
||||||
void sub_timespec(struct timespec t1, struct timespec t2, struct timespec* td);
|
void sub_timespec(struct timespec t1, struct timespec t2, struct timespec* td);
|
||||||
@ -66,9 +73,11 @@ void resume();
|
|||||||
void appendRunToFile();
|
void appendRunToFile();
|
||||||
void timespecToRFC3339(struct timespec t, char buf[]);
|
void timespecToRFC3339(struct timespec t, char buf[]);
|
||||||
void loadFiles();
|
void loadFiles();
|
||||||
|
void add_segment(char *sname, char *lname, char *desc);
|
||||||
void addFile(char *path);
|
void addFile(char *path);
|
||||||
void sendTime(int sock);
|
void sendTime(int sock);
|
||||||
void sendValue(int sock, char* name);
|
void sendValue(int sock, char* name);
|
||||||
|
void sendInt(int sock, int value);
|
||||||
void doprocessing (int sock);
|
void doprocessing (int sock);
|
||||||
|
|
||||||
|
|
||||||
@ -323,7 +332,38 @@ void loadFiles()
|
|||||||
break;
|
break;
|
||||||
if (buff[0] == '/' && buff[1] == '/' || buff[0] == '\n' || buff[0] == '\t')
|
if (buff[0] == '/' && buff[1] == '/' || buff[0] == '\n' || buff[0] == '\t')
|
||||||
continue;
|
continue;
|
||||||
if (!strcmp(buff, "Segment\n") || !strcmp(buff, "Route\n"))
|
if (!strcmp(buff, "Segment\n")) {
|
||||||
|
char *s = NULL;
|
||||||
|
char *l = NULL;
|
||||||
|
char *d = NULL;
|
||||||
|
//"\t\tStory 12\n\0"
|
||||||
|
for (int x = 0; x < 3; x++) {
|
||||||
|
if (!fgets(buff2, 255, fp))
|
||||||
|
break;
|
||||||
|
if (!strcmp(buff2, "\tShortname\n")) {
|
||||||
|
if (!fgets(buff2, 255, fp))
|
||||||
|
break;
|
||||||
|
s = malloc(strlen(buff2) - 2);
|
||||||
|
s = strncpy(s, buff2 + 2, strlen(buff2) - 2);
|
||||||
|
s[strlen(s) - 1] = '\0';
|
||||||
|
} else if (!strcmp(buff2, "\tLongname\n")) {
|
||||||
|
if (!fgets(buff2, 255, fp))
|
||||||
|
break;
|
||||||
|
l = malloc(strlen(buff2) - 2);
|
||||||
|
l = strncpy(l, buff2 + 2, strlen(buff2) - 2);
|
||||||
|
l[strlen(l) - 1] = '\0';
|
||||||
|
} else if (!strcmp(buff2, "\tDescription\n")) {
|
||||||
|
if (!fgets(buff2, 255, fp))
|
||||||
|
break;
|
||||||
|
d = malloc(strlen(buff2) - 2);
|
||||||
|
d = strncpy(d, buff2 + 2, strlen(buff2) - 2);
|
||||||
|
d[strlen(d) - 1] = '\0';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
add_segment(s, l, d);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!strcmp(buff, "Route\n"))
|
||||||
continue;
|
continue;
|
||||||
if (!strcmp(buff, "Run\n")) {
|
if (!strcmp(buff, "Run\n")) {
|
||||||
run_count++;
|
run_count++;
|
||||||
@ -353,9 +393,23 @@ void loadFiles()
|
|||||||
for (int i = 0; i < valuecount; i++) {
|
for (int i = 0; i < valuecount; i++) {
|
||||||
printf("%s | %s", names[i], values[i]);
|
printf("%s | %s", names[i], values[i]);
|
||||||
}
|
}
|
||||||
|
//Print segments
|
||||||
|
for (int i = 0; i < segment_count; i++) {
|
||||||
|
printf("Segment %d: %s\n", i, segments[i].shortname);
|
||||||
|
}
|
||||||
|
//Print run count
|
||||||
printf("%d\n", run_count);
|
printf("%d\n", run_count);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void add_segment(char *sname, char *lname, char *desc)
|
||||||
|
{
|
||||||
|
segment_count++;
|
||||||
|
segments = realloc(segments, sizeof(struct segment) * segment_count);
|
||||||
|
segments[segment_count - 1].shortname = sname;
|
||||||
|
segments[segment_count - 1].longname = lname;
|
||||||
|
segments[segment_count - 1].description = desc;
|
||||||
|
}
|
||||||
|
|
||||||
//TODO: eventually file loading should support loading multiple files
|
//TODO: eventually file loading should support loading multiple files
|
||||||
void addFile(char *path)
|
void addFile(char *path)
|
||||||
{
|
{
|
||||||
@ -405,6 +459,15 @@ void sendValue(int sock, char* name)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void sendInt(int sock, int value)
|
||||||
|
{
|
||||||
|
int n = write(sock, &value, sizeof(int));
|
||||||
|
if (n < 0) {
|
||||||
|
perror("ERROR writing to socket");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void doprocessing (int sock)
|
void doprocessing (int sock)
|
||||||
{
|
{
|
||||||
int n;
|
int n;
|
||||||
@ -454,6 +517,9 @@ void doprocessing (int sock)
|
|||||||
} else if (commandcode == 13) {
|
} else if (commandcode == 13) {
|
||||||
printf("Recieved save command\n");
|
printf("Recieved save command\n");
|
||||||
appendRunToFile();
|
appendRunToFile();
|
||||||
|
} else if (commandcode == 14) {
|
||||||
|
printf("Recieved request for run count\n");
|
||||||
|
sendInt(sock, run_count);
|
||||||
} else {
|
} else {
|
||||||
printf("Recieved invalid command code, ignoring...\n");
|
printf("Recieved invalid command code, ignoring...\n");
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user