int main(int argc, char * argv[]) { char * pathname; if (argc >= 2) { pathname = argv[1]; } else pathname = getenv("PWD"); mkfifo(FIFONAME, 0664); int fd = open(FIFONAME, O_RDONLY | O_NONBLOCK); int i = recursive_search(pathname, FIFONAME,0,0); if (i < 0) { perror("Error on recursive search"); return -1; } FILE * stream = fdopen(fd, "r"); char * buf = malloc(5); size_t buf_size = 5; size_t read; int count = 0; int average = 0; int temp = 0; while ( (read = getline(&buf, &buf_size, stream)) != -1) { temp = atoi(buf); average+=temp; count++; } average /= count; printf("\n\n"); printf("Average length of file names: %D\n", average); printf("Starting printing of file names above average...\n"); i = recursive_search(pathname, FIFONAME, 1, average); if (i < 0) { perror("Error on printing recursion"); return -1; } free(buf); }
/* The main function. This is where the function result is computed */ long long go_isa(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) { long dnaLength= args->lengths[0]; const char *child=args->args[0]; const char *parent=args->args[0]; char name1[MAX_TERM_LENGTH]; char name2[MAX_TERM_LENGTH]; TermDBPtr termdb=(TermDBPtr)initid->ptr; int index; *is_null=0; if (args->args[0]==NULL || args->args[1]==NULL || args->lengths[0]>=MAX_TERM_LENGTH || args->lengths[1]>=MAX_TERM_LENGTH ) /* Null argument */ { *is_null=1; return -1; } strncpy(name1,args->args[0],args->lengths[0]); name1[args->lengths[0]]=0; strncpy(name2,args->args[1],args->lengths[1]); name2[args->lengths[1]]=0; index=termdb_findIndexByName(termdb,name1); if(index==-1) { return 0; } return recursive_search(termdb,index,name2,0); }
bool recursive_search(int value, int *array, int start, int end) { // Found needle if (value == array[(start+end)] / 2) return true; while (start <= end) { // Declare middle variable int middle = (start + end) / 2; // Recursively search left/right portion of the array if (value > array[middle]) return recursive_search(value, array, middle + 1, end); else return recursive_search(value, array, start, middle - 1); } // Unable to locate value return false; }
static int recursive_search(const TermDBPtr db,int index, const char* parent,int depth) { int rez=0; int start=index; int parent_idx=0; if(start<0 || start>=db->n_terms) return 0; if(strcmp(db->terms[index].child,parent)==0) return 1; while(index < db->n_terms) { if(strcmp(db->terms[index].child,db->terms[start].child)!=0) break; if(strcmp(db->terms[index].parent,parent)==0) return 1; parent_idx= termdb_findIndexByName(db,db->terms[index].parent); rez= recursive_search(db,parent_idx,parent,depth+1); if(rez==1 ) return 1; ++index; } return 0; }
int task_execute(oph_operator_struct * handle) { if (!handle || !handle->operator_handle) { pmesg(LOG_ERROR, __FILE__, __LINE__, "Null Handle\n"); logging(LOG_ERROR, __FILE__, __LINE__, OPH_GENERIC_CONTAINER_ID, OPH_LOG_OPH_SEARCH_NULL_OPERATOR_HANDLE); return OPH_ANALYTICS_OPERATOR_NULL_OPERATOR_HANDLE; } //Only master process has to continue if (handle->proc_rank != 0) return OPH_ANALYTICS_OPERATOR_SUCCESS; int folderid; char *abs_path = NULL; char *filters = NULL; int *max_lengths = NULL; int max_lengths_size; int permission = 0; ophidiadb *oDB = &((OPH_SEARCH_operator_handle *) handle->operator_handle)->oDB; if (!strcasecmp(((OPH_SEARCH_operator_handle *) handle->operator_handle)->path, OPH_FRAMEWORK_FS_DEFAULT_PATH)) { if (oph_odb_fs_path_parsing(".", ((OPH_SEARCH_operator_handle *) handle->operator_handle)->cwd, &folderid, &abs_path, oDB)) { pmesg(LOG_ERROR, __FILE__, __LINE__, "Unable to parse path\n"); logging(LOG_ERROR, __FILE__, __LINE__, OPH_GENERIC_CONTAINER_ID, OPH_LOG_OPH_SEARCH_PATH_PARSING_ERROR); return OPH_ANALYTICS_OPERATOR_UTILITY_ERROR; } } else { if (oph_odb_fs_path_parsing(((OPH_SEARCH_operator_handle *) handle->operator_handle)->path, ((OPH_SEARCH_operator_handle *) handle->operator_handle)->cwd, &folderid, &abs_path, oDB)) { pmesg(LOG_ERROR, __FILE__, __LINE__, "Unable to parse path '%s/%s'\n", ((OPH_SEARCH_operator_handle *) handle->operator_handle)->cwd, ((OPH_SEARCH_operator_handle *) handle->operator_handle)->path); logging(LOG_ERROR, __FILE__, __LINE__, OPH_GENERIC_CONTAINER_ID, OPH_LOG_OPH_SEARCH_PATH_PARSING_ERROR); return OPH_ANALYTICS_OPERATOR_UTILITY_ERROR; } } if ((oph_odb_fs_check_folder_session(folderid, ((OPH_SEARCH_operator_handle *) handle->operator_handle)->sessionid, oDB, &permission)) || !permission) { pmesg(LOG_ERROR, __FILE__, __LINE__, "Path %s not allowed\n", abs_path); logging(LOG_ERROR, __FILE__, __LINE__, OPH_GENERIC_CONTAINER_ID, OPH_LOG_OPH_SEARCH_PATH_NOT_ALLOWED_ERROR, abs_path); if (abs_path) { free(abs_path); abs_path = NULL; } return OPH_ANALYTICS_OPERATOR_BAD_PARAMETER; } // Change folder_to_be_printed in user format if (abs_path) { char old_abs_path[1 + strlen(abs_path)]; strcpy(old_abs_path, abs_path); free(abs_path); abs_path = NULL; if (oph_pid_drop_session_prefix(old_abs_path, ((OPH_SEARCH_operator_handle *) handle->operator_handle)->sessionid, &abs_path)) { pmesg(LOG_ERROR, __FILE__, __LINE__, "Folder conversion error\n"); logging(LOG_ERROR, __FILE__, __LINE__, OPH_GENERIC_CONTAINER_ID, "Folder conversion error\n"); if (abs_path) { free(abs_path); abs_path = NULL; } return OPH_ANALYTICS_OPERATOR_BAD_PARAMETER; } } if (get_filters_string (((OPH_SEARCH_operator_handle *) handle->operator_handle)->container_filter, ((OPH_SEARCH_operator_handle *) handle->operator_handle)->container_filter_num, ((OPH_SEARCH_operator_handle *) handle->operator_handle)->metadata_key_filter, ((OPH_SEARCH_operator_handle *) handle->operator_handle)->metadata_key_filter_num, ((OPH_SEARCH_operator_handle *) handle->operator_handle)->metadata_value_filter, ((OPH_SEARCH_operator_handle *) handle->operator_handle)->metadata_value_filter_num, &filters)) { pmesg(LOG_ERROR, __FILE__, __LINE__, "Unable to parse filters\n"); logging(LOG_ERROR, __FILE__, __LINE__, OPH_GENERIC_CONTAINER_ID, OPH_LOG_OPH_SEARCH_FILTERS_PARSING_ERROR); if (abs_path) { free(abs_path); abs_path = NULL; } return OPH_ANALYTICS_OPERATOR_UTILITY_ERROR; } if (recursive_get_max_lengths(strlen(abs_path), folderid, filters, oDB, &max_lengths, &max_lengths_size, NULL, 1)) { pmesg(LOG_ERROR, __FILE__, __LINE__, "Search error\n"); logging(LOG_ERROR, __FILE__, __LINE__, OPH_GENERIC_CONTAINER_ID, OPH_LOG_OPH_SEARCH_SEARCH_ERROR); if (abs_path) { free(abs_path); abs_path = NULL; } if (filters) { free(filters); filters = NULL; } return OPH_ANALYTICS_OPERATOR_UTILITY_ERROR; } if (recursive_search (abs_path, folderid, filters, oDB, max_lengths, max_lengths_size, NULL, NULL, 1, handle->operator_json, oph_json_is_objkey_printable(((OPH_SEARCH_operator_handle *) handle->operator_handle)->objkeys, ((OPH_SEARCH_operator_handle *) handle->operator_handle)->objkeys_num, OPH_JSON_OBJKEY_SEARCH))) { pmesg(LOG_ERROR, __FILE__, __LINE__, "Search error\n"); logging(LOG_ERROR, __FILE__, __LINE__, OPH_GENERIC_CONTAINER_ID, OPH_LOG_OPH_SEARCH_SEARCH_ERROR); if (abs_path) { free(abs_path); abs_path = NULL; } if (filters) { free(filters); filters = NULL; } if (max_lengths) { free(max_lengths); max_lengths = NULL; } return OPH_ANALYTICS_OPERATOR_UTILITY_ERROR; } if (abs_path) { free(abs_path); abs_path = NULL; } if (filters) { free(filters); filters = NULL; } if (max_lengths) { free(max_lengths); max_lengths = NULL; } return OPH_ANALYTICS_OPERATOR_SUCCESS; }
int recursive_search(const char *folder_abs_path, int folderid, const char *filters, ophidiadb * oDB, int *max_lengths, int max_lengths_size, char *query, char *path, int is_start, oph_json * oper_json, int is_objkey_printable) { if (!oDB) { pmesg(LOG_ERROR, __FILE__, __LINE__, "Null input parameter\n"); return OPH_ODB_NULL_PARAM; } if (oph_odb_check_connection_to_ophidiadb(oDB)) { pmesg(LOG_ERROR, __FILE__, __LINE__, "Unable to reconnect to OphidiaDB.\n"); return OPH_ODB_MYSQL_ERROR; } MYSQL_RES *res; MYSQL_FIELD *fields; MYSQL_ROW row; int i, j, num_fields; char *buffer; char *buffer2; if (is_start) { buffer = malloc(MYSQL_BUFLEN); if (!buffer) { pmesg(LOG_ERROR, __FILE__, __LINE__, "Error allocating memory for query\n"); return OPH_ANALYTICS_OPERATOR_MEMORY_ERR; } buffer2 = malloc(MYSQL_BUFLEN); if (!buffer2) { pmesg(LOG_ERROR, __FILE__, __LINE__, "Error allocating memory for path\n"); free(buffer); return OPH_ANALYTICS_OPERATOR_MEMORY_ERR; } } else { buffer = query; buffer2 = path; } snprintf(buffer, MYSQL_BUFLEN, MYSQL_QUERY_OPH_SEARCH_READ_INSTANCES, folderid, filters); if (mysql_query(oDB->conn, buffer)) { pmesg(LOG_ERROR, __FILE__, __LINE__, "MySQL query error: %s\n", mysql_error(oDB->conn)); free(buffer); free(buffer2); return OPH_ANALYTICS_OPERATOR_MYSQL_ERROR; } res = mysql_store_result(oDB->conn); fields = mysql_fetch_fields(res); num_fields = mysql_num_fields(res) - 1; if (is_start) { //print header printf("+"); for (i = 0; i < max_lengths_size; i++) { printf("-"); for (j = 0; j < max_lengths[i]; j++) { printf("-"); } printf("-+"); } printf("\n"); printf("|"); for (i = 0; i < max_lengths_size; i++) { printf(" "); printf("%-*s", max_lengths[i], fields[i].name); printf(" |"); } printf("\n"); printf("+"); for (i = 0; i < max_lengths_size; i++) { printf("-"); for (j = 0; j < max_lengths[i]; j++) { printf("-"); } printf("-+"); } printf("\n"); if (is_objkey_printable) { char **jsonkeys = NULL; char **fieldtypes = NULL; int iii, jjj; jsonkeys = (char **) malloc(sizeof(char *) * num_fields); if (!jsonkeys) { pmesg(LOG_ERROR, __FILE__, __LINE__, "Error allocating memory\n"); logging(LOG_ERROR, __FILE__, __LINE__, OPH_GENERIC_CONTAINER_ID, OPH_LOG_OPH_SEARCH_MEMORY_ERROR_INPUT, "keys"); mysql_free_result(res); return OPH_ANALYTICS_OPERATOR_MEMORY_ERR; } for (jjj = 0; jjj < num_fields; ++jjj) { jsonkeys[jjj] = strdup(fields[jjj + 1].name ? fields[jjj + 1].name : ""); if (!jsonkeys[jjj]) { pmesg(LOG_ERROR, __FILE__, __LINE__, "Error allocating memory\n"); logging(LOG_ERROR, __FILE__, __LINE__, OPH_GENERIC_CONTAINER_ID, OPH_LOG_OPH_SEARCH_MEMORY_ERROR_INPUT, "key"); for (iii = 0; iii < jjj; iii++) if (jsonkeys[iii]) free(jsonkeys[iii]); if (jsonkeys) free(jsonkeys); mysql_free_result(res); return OPH_ANALYTICS_OPERATOR_MEMORY_ERR; } } fieldtypes = (char **) malloc(sizeof(char *) * num_fields); if (!fieldtypes) { pmesg(LOG_ERROR, __FILE__, __LINE__, "Error allocating memory\n"); logging(LOG_ERROR, __FILE__, __LINE__, OPH_GENERIC_CONTAINER_ID, OPH_LOG_OPH_SEARCH_MEMORY_ERROR_INPUT, "fieldtypes"); for (iii = 0; iii < num_fields; iii++) if (jsonkeys[iii]) free(jsonkeys[iii]); if (jsonkeys) free(jsonkeys); mysql_free_result(res); return OPH_ANALYTICS_OPERATOR_MEMORY_ERR; } for (jjj = 0; jjj < num_fields; ++jjj) { fieldtypes[jjj] = strdup(OPH_JSON_STRING); if (!fieldtypes[jjj]) { pmesg(LOG_ERROR, __FILE__, __LINE__, "Error allocating memory\n"); logging(LOG_ERROR, __FILE__, __LINE__, OPH_GENERIC_CONTAINER_ID, OPH_LOG_OPH_SEARCH_MEMORY_ERROR_INPUT, "fieldtype"); for (iii = 0; iii < num_fields; iii++) if (jsonkeys[iii]) free(jsonkeys[iii]); if (jsonkeys) free(jsonkeys); for (iii = 0; iii < jjj; iii++) if (fieldtypes[iii]) free(fieldtypes[iii]); if (fieldtypes) free(fieldtypes); mysql_free_result(res); return OPH_ANALYTICS_OPERATOR_MEMORY_ERR; } } if (oph_json_add_grid(oper_json, OPH_JSON_OBJKEY_SEARCH, "Searching results", NULL, jsonkeys, num_fields, fieldtypes, num_fields)) { pmesg(LOG_ERROR, __FILE__, __LINE__, "ADD GRID error\n"); logging(LOG_WARNING, __FILE__, __LINE__, OPH_GENERIC_CONTAINER_ID, "ADD GRID error\n"); for (iii = 0; iii < num_fields; iii++) if (jsonkeys[iii]) free(jsonkeys[iii]); if (jsonkeys) free(jsonkeys); for (iii = 0; iii < num_fields; iii++) if (fieldtypes[iii]) free(fieldtypes[iii]); if (fieldtypes) free(fieldtypes); mysql_free_result(res); return OPH_ANALYTICS_OPERATOR_UTILITY_ERROR; } for (iii = 0; iii < num_fields; iii++) if (jsonkeys[iii]) free(jsonkeys[iii]); if (jsonkeys) free(jsonkeys); for (iii = 0; iii < num_fields; iii++) if (fieldtypes[iii]) free(fieldtypes[iii]); if (fieldtypes) free(fieldtypes); } } //print each ROW while ((row = mysql_fetch_row(res))) { printf("|"); for (i = 0; i < max_lengths_size; i++) { printf(" "); if (!strcasecmp(fields[i].name, "Container")) { if (folder_abs_path[strlen(folder_abs_path) - 1] == '/') { snprintf(buffer2, MYSQL_BUFLEN, "%s%s", folder_abs_path, row[i]); } else { snprintf(buffer2, MYSQL_BUFLEN, "%s/%s", folder_abs_path, row[i]); } printf("%-*s", max_lengths[i], buffer2); } else { printf("%-*s", max_lengths[i], row[i]); } printf(" |"); } printf("\n"); if (is_objkey_printable) { char **jsonvalues = NULL; int iii, jjj; jsonvalues = (char **) calloc(num_fields, sizeof(char *)); if (!jsonvalues) { pmesg(LOG_ERROR, __FILE__, __LINE__, "Error allocating memory\n"); logging(LOG_ERROR, __FILE__, __LINE__, OPH_GENERIC_CONTAINER_ID, OPH_LOG_OPH_SEARCH_MEMORY_ERROR_INPUT, "values"); mysql_free_result(res); return OPH_ANALYTICS_OPERATOR_MEMORY_ERR; } for (jjj = 0; jjj < num_fields; ++jjj) { if (jjj) jsonvalues[jjj] = strdup(row[1 + jjj] ? row[1 + jjj] : ""); else { char *tmp_uri = NULL, *pid = NULL; if (oph_pid_get_uri(&tmp_uri)) { pmesg(LOG_ERROR, __FILE__, __LINE__, "Error allocating memory\n"); logging(LOG_ERROR, __FILE__, __LINE__, OPH_GENERIC_CONTAINER_ID, OPH_LOG_OPH_SEARCH_MEMORY_ERROR_INPUT, "PID"); mysql_free_result(res); return OPH_ANALYTICS_OPERATOR_MEMORY_ERR; } if (oph_pid_create_pid(tmp_uri, (int) strtol(row[0], NULL, 10), (int) strtol(row[1], NULL, 10), &pid)) { pmesg(LOG_ERROR, __FILE__, __LINE__, "Unable to create PID string\n"); logging(LOG_ERROR, __FILE__, __LINE__, OPH_GENERIC_CONTAINER_ID, OPH_LOG_OPH_SEARCH_MEMORY_ERROR_INPUT, "PID"); mysql_free_result(res); return OPH_ANALYTICS_OPERATOR_MEMORY_ERR; } if (tmp_uri) free(tmp_uri); jsonvalues[jjj] = strdup(pid ? pid : ""); } if (!jsonvalues[jjj]) { pmesg(LOG_ERROR, __FILE__, __LINE__, "Error allocating memory\n"); logging(LOG_ERROR, __FILE__, __LINE__, OPH_GENERIC_CONTAINER_ID, OPH_LOG_OPH_SEARCH_MEMORY_ERROR_INPUT, "value"); for (iii = 0; iii < jjj; iii++) if (jsonvalues[iii]) free(jsonvalues[iii]); if (jsonvalues) free(jsonvalues); mysql_free_result(res); return OPH_ANALYTICS_OPERATOR_MEMORY_ERR; } } if (oph_json_add_grid_row(oper_json, OPH_JSON_OBJKEY_SEARCH, jsonvalues)) { pmesg(LOG_ERROR, __FILE__, __LINE__, "ADD GRID ROW error\n"); logging(LOG_WARNING, __FILE__, __LINE__, OPH_GENERIC_CONTAINER_ID, "ADD GRID ROW error\n"); for (iii = 0; iii < num_fields; iii++) if (jsonvalues[iii]) free(jsonvalues[iii]); if (jsonvalues) free(jsonvalues); mysql_free_result(res); return OPH_ANALYTICS_OPERATOR_UTILITY_ERROR; } for (iii = 0; iii < num_fields; iii++) if (jsonvalues[iii]) free(jsonvalues[iii]); if (jsonvalues) free(jsonvalues); } } mysql_free_result(res); //recursive step snprintf(buffer, MYSQL_BUFLEN, MYSQL_QUERY_OPH_SEARCH_READ_SUBFOLDERS, folderid); if (mysql_query(oDB->conn, buffer)) { pmesg(LOG_ERROR, __FILE__, __LINE__, "MySQL query error: %s\n", mysql_error(oDB->conn)); free(buffer); free(buffer2); return OPH_ANALYTICS_OPERATOR_MYSQL_ERROR; } res = mysql_store_result(oDB->conn); while ((row = mysql_fetch_row(res))) { if (folder_abs_path[strlen(folder_abs_path) - 1] == '/') { snprintf(buffer2, MYSQL_BUFLEN, "%s%s/", folder_abs_path, row[1]); } else { snprintf(buffer2, MYSQL_BUFLEN, "%s/%s/", folder_abs_path, row[1]); } char *subfolder_path = strdup(buffer2); if (!subfolder_path) { pmesg(LOG_ERROR, __FILE__, __LINE__, "Error allocating memory for subfolder path\n"); free(buffer); free(buffer2); mysql_free_result(res); return OPH_ANALYTICS_OPERATOR_MEMORY_ERR; } if (recursive_search(subfolder_path, (int) strtol(row[0], NULL, 10), filters, oDB, max_lengths, max_lengths_size, buffer, buffer2, 0, oper_json, is_objkey_printable)) { pmesg(LOG_ERROR, __FILE__, __LINE__, "Recursive step error\n"); free(buffer); free(buffer2); free(subfolder_path); mysql_free_result(res); return OPH_ANALYTICS_OPERATOR_UTILITY_ERROR; } free(subfolder_path); } mysql_free_result(res); if (is_start) { //print footer printf("+"); for (i = 0; i < max_lengths_size; i++) { printf("-"); for (j = 0; j < max_lengths[i]; j++) { printf("-"); } printf("-+"); } printf("\n"); if (buffer) { free(buffer); buffer = NULL; } if (buffer2) { free(buffer2); buffer2 = NULL; } } return OPH_ANALYTICS_OPERATOR_SUCCESS; }
int main(void) { int i; node *t1, *t2, *t3; /* recursive_insert() */ printf("=== Tesing Insert Recursive ===\n"); t1 = NULL; int tree_values[10] = {5, 8, 3, 6, 1, 4, 9, 0, 7, 2}; for (i=0; i<10; i++) { recursive_insert(&t1,tree_values[i]); } printf("=== Inorder Traversal ===\n"); print_inorder_traverse(t1); printf("\n"); printf("=== PreOrder Traversal ===\n"); print_preorder_traverse(t1); printf("\n"); printf("=== PostOrder Traversal ===\n"); print_postorder_traverse(t1); printf("\n"); node *elem; printf("=== Recursive Search ===\n"); elem = recursive_search(t1, 5); printf("%d\n", elem->value); printf("%d\n", elem->left->value); printf("%d\n", elem->right->value); printf("\n"); printf("=== Copy Tree ===\n"); t2=copy(t1); print_inorder_traverse(t1); printf("\n"); print_inorder_traverse(t2); printf("\n"); printf("=== Depth ===\n"); int _d = depth(t1); printf("%d\n", _d); printf("\n"); printf("=== Num Nodes ===\n"); int _n = num_nodes(t1); printf("%d\n", _n); printf("\n"); printf("=== Destroy ===\n"); destroy(&t1); print_inorder_traverse(t1); printf("\n"); printf("=== Is Identical ===\n"); t1=NULL; t2=NULL; for (i=0; i<10; i++) { recursive_insert(&t1,tree_values[i]); recursive_insert(&t2,tree_values[i]); } int identical = is_identical(t1, t2); if (identical) printf("%s\n", "Trees are identical"); else printf("%s\n", "Trees are not identical"); printf("\n"); printf("=== Num of Leaves ===\n"); int leaves = num_leaves(t1); printf("%d\n", leaves); printf("\n"); }