📄 processor.c
字号:
/*
* ARTIFICIAL INTELLIGENCE SYSTEM
*
* Copyright (C) 2007-Present Intelligence Realm Inc. All rights reserved.
*
* See LICENSE.TXT document for licensing information.
*/#include "processor.h"//#define ARCHIVE_WU 0int main(int argc, char **argv)
{ char *file_path = NULL; char *file_name = NULL; char *file_extension = NULL; char folder_work[] = "/home/ovi/aisystem/work0"; char folder_short[3]; char folder_source[255]; char folder_source_new[255]; char file_name_new[50]; char folder_destination[255]; char file_xml_path[500]; char buffer[500]; char command[500]; int return_code = 0; int is_archived = 0; int path_length = 0; char *uuid = NULL; if (strlen(argv[1]) == 0) return 1; strcpy(folder_source, argv[1]); printf("folder_source=%s\n", folder_source); strcpy(buffer, argv[1]); /* get file extension */ file_extension = (char *)malloc((2 + 1) * sizeof(char)); if (strstr(file_extension, "gz") == 0) { is_archived = 0; strcpy(file_extension, right(buffer, 2)); /* db */ } else { is_archived = 1; strcpy(file_extension, right(buffer, 5)); /* db.gz */ } printf("file_extension=%s\n", file_extension); /* get file_name */ file_name = (char *)malloc((UUID_DATA_LENGTH + 3 + 3 + 1) * sizeof(char)); if (is_archived) strcpy(file_name, right(argv[1], 38)); /* UUID + .db + .gz */ else strcpy(file_name, right(argv[1], 35)); /* UUID + .db */ printf("file_name=%s\n", file_name); /* get file_path and also remove the last / (i.e. -1) that is in the front of the file name */ if (is_archived) path_length = strlen(folder_source) - 38 - 1; else path_length = strlen(folder_source) - 35 - 1; printf("path_length=%d\n", path_length); file_path = (char *)malloc(255 * sizeof(char)); substr(file_path, folder_source, 0, path_length); printf("file_path=%s\n", file_path); /* rename file to a new uuid in the same location */ uuid = (char *)malloc((UUID_DATA_LENGTH+1) * sizeof(char)); uuid_create(uuid); strcpy(command, "mv "); /* use mv instead of rename */ strcat(command, folder_source); strcat(command, " "); strcat(command, file_path); strcat(command, "/"); strcat(command, uuid); strcat(command, "."); strcat(command, file_extension); return_code = system(command); if (return_code != 0) { printf("Error: the rename operation failed\n"); return 1; } /* get the first 2 characters of the new name to match it with the destination folder */ strcpy(buffer, uuid); strcpy(folder_short, left(buffer, 2)); /* set new file name */ strcpy(file_name_new, uuid); strcat(file_name_new, "."); strcat(file_name_new, file_extension); /* db or db.gz */ /* set source folder for the renamed file */ substr(folder_source_new, folder_source, 0, path_length); strcat(folder_source_new, "/"); strcat(folder_source_new, file_name_new); /* set folder_destination */ strcpy(folder_destination, folder_work); strcat(folder_destination, "/process/"); strcat(folder_destination, folder_short); strcat(folder_destination, "/"); strcat(folder_destination, file_name_new); printf("folder_destination=%s\n", folder_destination); /* move file to the new location */ strcpy(command, "mv "); strcat(command, folder_source_new); strcat(command, " "); strcat(command, folder_destination); /* file including path */ return_code = system(command); if (return_code != 0) { printf("Error: the move operation failed\n"); return 1; } /* archive file if it is not already archived */#ifdef ARCHIVE_WU if (!is_archived) { strcpy(command, "gzip "); strcat(command, folder_destination); /* file including path */ return_code = system(command); if (return_code != 0) { printf("Error: the archive operation failed\n"); return 1; } }#endif /* create empty xml file with same name as database file */ strcpy(file_xml_path, folder_work); strcat(file_xml_path, "/process/"); strcat(file_xml_path, folder_short); strcat(file_xml_path, "/"); strcat(file_xml_path, uuid); strcat(file_xml_path, ".xml"); printf("file_xml_path=%s\n", file_xml_path); xml_write_data(file_xml_path); /* free memory */ free(uuid); free(file_extension); free(file_name); free(file_path); return 0;}int len(char *src){ int l = 0; while(src[l++] != '\0'); return l;}int substrpos(const char *str, const char *substr)
{
/* finds the position of a substring in another string */
char *dest;
int pos;
dest = strstr(str, substr);
if (dest == NULL) /* substring not found */
pos = 0;
else
{
pos = str - dest; /* calculate the position */
if (pos < 0)
pos = -pos; /* take the positive value of position */
}
return pos;
}void substr(char *dest, const char *src, int offset, int length)
{
/*
* returns a substring of a string, starting from offset for length characters
* the pointer must be allocated and freed outside of this function
*/
strncpy(dest, src + offset, length);
}char *left(char *src, int n){ static char *res = NULL; res = src; res[n] = '\0'; return res;}char *right(char *src, int n){ static char *res = NULL; int i = 0; int l = len(src) - n - 1; res = src; for(;i<l;i++) { res[i] = '\0'; } i=0; while((res[i++] == '\0') && (i < l)); return &res[i];}void xml_write_data(const char *file_name)
{
FILE *file_input;
int flush_status = 0;
file_input = fopen(file_name, "w");
if (file_input == NULL)
printf("Could not create the xml file.\n");
fputs("", file_input);
flush_status = fflush(file_input); /* saves data to disk */
if (flush_status != 0)
printf("Could not flush the buffered data to xml file.\n");
fclose(file_input);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -