orion_filepath* orion_filepath_dir( orion_filepath* fp ) { /* dir_fp is the new filepath for the base component */ orion_filepath* dir_fp = _orion_filepath_init(); dir_fp->has_root_component = fp->has_root_component; if( fp->has_root_component ) { safe_malloc_and_strcpy( &( dir_fp->root_component ), fp->root_component ); } /* TODO what if there are no more components? should it start * using "."? See how Path::Tiny behaves. */ /* copy all but the last component into dir_fp */ size_t number_of_components = array_length_str(fp->components); for( int comp_idx = 0; comp_idx < number_of_components - 1; comp_idx++ ) { char* cur_component; safe_malloc_and_strcpy(&cur_component, array_get_str( fp->components, comp_idx)); /* add that to the dir_fp */ array_add_str( dir_fp->components, cur_component ); } /* dir component should not be empty */ assert( array_length_str(dir_fp->components) > 0 ); return dir_fp; }
orion_filepath* orion_filepath_base( orion_filepath* fp ) { /* base_fp is the new filepath for the base component */ orion_filepath* base_fp = _orion_filepath_init(); base_fp->has_root_component = false; /* copy the last component into a buffer */ size_t number_of_components = array_length_str(fp->components); char* last_component; safe_malloc_and_strcpy(&last_component, array_get_str( fp->components, number_of_components - 1)); /* add that buffer to the base_fp */ array_add_str( base_fp->components, last_component ); /* there should only be one component in the base component */ assert( array_length_str(base_fp->components) == 1 ); return base_fp; }
/** * Convert a serialized array back to a C datastructure. */ int array_deserialize( struct array_t *arr, char *input ) { char *p; char *skey; char *svalue; skey = svalue = p = input; while( *p ) { if( *p == '\1' ) { *p = 0; svalue = p+1; } else if( *p == '\2' ) { *p = 0; array_add_str( arr, skey, svalue ); skey = p+1; } p++; } return 0; }
/** * Load, parse, evaulate, display, with a form */ void template_run_form( char *path, struct form_t *form, struct array_t *them ) { struct array_t *arr = array_new(); node *n; /* If not empty we validated */ if( ! array_empty( form->err) ) { array_add_obj( arr, "errors", form->err ); } for( n=array_first( form->clean ); n != NULL; n = array_next( form->clean ) ) { // printf("F**K %s %s\n", n->key, n->value ); array_add_str( arr, n->key, nully(n->value) ); } template_run( path, arr ); array_free(arr); }
void main() { struct array_t *arr = array_new(); int k=0; char *out=NULL; struct array_t *arr2 = array_new(); node *n; for(k=0; k<10; k++ ) { array_add_str( arr, "k", "v" ); } k=array_serialize( arr, &out ); printf("%d %s \n", k, out ); array_deserialize( arr2, out ); for( n=array_first(arr2); n!=NULL; n = array_next(arr2)) { printf("%s %s\n", n->key, n->value ); } free(out); array_free( arr); array_free( arr2); }
orion_filepath* orion_filepath_cat( orion_filepath* fp1, orion_filepath* fp2 ) { orion_filepath* cat_fp = _orion_filepath_init(); TODO( check if the second one is absolute or not ); if( fp1->has_root_component ) { cat_fp->has_root_component = true; safe_malloc_and_strcpy( &(cat_fp->root_component), fp1->root_component ); } orion_filepath* fp[] = { fp1, fp2 }; for( size_t fp_idx = 0; fp_idx < 2; fp_idx++ ) { size_t number_of_components = array_length_str(fp[fp_idx]->components); for( int comp_idx = 0; comp_idx < number_of_components; comp_idx++ ) { char* cur_component; safe_malloc_and_strcpy(&cur_component, array_get_str( fp[fp_idx]->components, comp_idx)); array_add_str( cat_fp->components, cur_component ); } } return cat_fp; }
orion_filepath* orion_filepath_new_from_string(const char* fp_string) { assert( NULL != fp_string ); /* normalise the backslashes to forward slashes on Windows */ char* fp_string_norm; size_t fp_string_norm_len; safe_malloc_and_strcpy(&fp_string_norm, fp_string); size_t fp_string_len = strlen( fp_string ); orion_filepath* fp = _orion_filepath_init(); fp_string_norm_len = strlen( fp_string_norm ); size_t norm_current_idx = 0; while( norm_current_idx < fp_string_norm_len ) { if( '\\' == fp_string_norm[norm_current_idx] ) { fp_string_norm[norm_current_idx] = '/'; } norm_current_idx++; } /*[>DEBUG<]printf("==> %s\n", fp_string_norm);*/ char* fp_string_norm_cur_ptr = fp_string_norm; char* fp_string_norm_end_ptr = strchr(fp_string_norm, '\0'); /* check if the string is an absolute path */ if( '/' == fp_string_norm[0] ) { /* Unix specific: starts with '/', * then it is an absolute path. */ fp->has_root_component = true; safe_malloc_and_strcpy( &(fp->root_component), "/" ); /* we add to the cur_ptr because we want to start * splitting right after the root component */ fp_string_norm_cur_ptr++; } /* split into components */ while( fp_string_norm_cur_ptr < fp_string_norm_end_ptr ) { /* the variable into which the current segment will be copied */ char* cur_comp_seg; if( '/' == *fp_string_norm_cur_ptr ) { /* skip past any forward slashes since they are empty * components */ fp_string_norm_cur_ptr++; continue; } char* comp_up_to_slash = strchr(fp_string_norm_cur_ptr , '/' ); if( NULL == comp_up_to_slash ) { /* no more forward slashes, so we go up to the end */ comp_up_to_slash = fp_string_norm_end_ptr; } /* copy up to (but not including) the next slash */ safe_malloc_and_strncpy(&cur_comp_seg, fp_string_norm_cur_ptr, comp_up_to_slash - fp_string_norm_cur_ptr ); array_add_str( fp->components, cur_comp_seg); /* start right after the next slash */ fp_string_norm_cur_ptr = comp_up_to_slash + 1; } free(fp_string_norm); return fp; }