string_array *mk_copy_string_array(const string_array *sa) { string_array *nsa = mk_string_array(string_array_size(sa)); int i; for ( i = 0 ; i < string_array_size(sa) ; i++ ) string_array_set(nsa,i,string_array_ref(sa,i)); return(nsa); }
void draw_hist_axis( frame *fr, axis *ax , string_array *bar_labels ) { int i; onpoint p; int color = ag_pen_color(); int num_bars = string_array_size(bar_labels); ag_set_pen_color(AG_RED); ongr_line(fr,&ax->start,&ax->end); for ( i = 0 ; i < num_bars ; i++ ) { char* mark = string_array_ref(bar_labels,i); p.x = ax->start.x + ((ax->end.x - ax->start.x) * (i+0.5)) / num_bars; p.y = ax->start.y + ((ax->end.y - ax->start.y) * (i+0.5)) / num_bars; ag_set_pen_color(AG_RED); ongr_cross(fr,&p); p.x += ax->rel_mark_number.x; p.y += ax->rel_mark_number.y; ag_set_pen_color(color); if ( mark != NULL ) ongr_print(fr,&p,mark); } ag_set_pen_color(color); /* Finally, we do the label for the axis */ p.x = (frame_width(fr)) / 40.0; p.y = CH/2.0; ongr_print(fr,&p,ax->label); }
void fprintf_string_array(FILE *s,const char *m1,const string_array *sar, const char *m2) { int i; if (sar == NULL) fprintf(s,"%s = <NULL string array>%s",m1,m2); else { if ( string_array_size(sar) == 0 ) fprintf(s,"%s = <empty string array>%s",m1,m2); else { for ( i = 0 ; i < string_array_size(sar) ; i++ ) fprintf(s,"%s[%3d] = %s%s", m1,i, (string_array_ref(sar,i)==NULL) ? "NULL" : string_array_ref(sar,i), m2 ); } } }
void string_array_remove(string_array* sa, int idx) { int i; if ( string_array_size(sa) <= 0 ) my_error("string_array_remove: empty string_array"); if ( idx < 0 || idx >= string_array_size(sa) ) my_error("string_array_remove: bad index"); /* WKW - New version shuffles the pointers over by one instead of using string_array_set, which copies the (i+1)th element into the ith spot and then frees the (i+1)th element. This is faster and it avoids some memory bugs in the old version */ if( sa->sarr[idx] != NULL ) { am_free_string(sa->sarr[idx]); sa->sarr[idx]=NULL; } for( i = idx ; i < (sa->size - 1); i++ ) sa->sarr[i] = sa->sarr[i+1]; sa->sarr[sa->size-1] = NULL; /* Might help catch some weird errors */ sa -> size -= 1; }
/** * @brief read ::cmd_start_data from @p m * @param m the message to parse * @param skip_argv0 start building from argv[1] * @returns a ::cmd_start_data pointer on success, NULL on error. */ struct cmd_start_data *extract_start_data(message *m, char skip_argv0) { char *arg, *end; int i, n; size_t size; struct cmd_start_data *data; struct cmd_start_info *start_info; start_info = (struct cmd_start_info *) m->data; n = string_array_size(m, start_info->data); if(!n && !skip_argv0) { print( ERROR, "empty array"); return NULL; } data = malloc(sizeof(struct cmd_start_data)); if(!data) { print( ERROR, "malloc: %s", strerror(errno)); return NULL; } memset(data, 0, sizeof(struct cmd_start_data)); if(start_info->argc > n) { print( ERROR, "argc out of bounds ( argc=%d, n=%d )", start_info->argc, n); goto error; } data->argc = start_info->argc; if(skip_argv0) { i=1; data->argc++; } else { i=0; data->free_argv0 = 1; } size = (sizeof(char *) * (data->argc+1)); data->argv = malloc(size); if(!data->argv) { print( ERROR, "malloc: %s", strerror(errno) ); goto error; } memset(data->argv, 0, size); end = m->data + m->head.size; arg=NULL; for(;i < data->argc && (arg=string_array_next(m, start_info->data, arg)); i++) { data->argv[i] = strndup(arg, (end-arg)); if(!data->argv[i]) { print( ERROR, "strndup: %s", strerror(errno)); goto error; } } if(arg) { data->env_start = string_array_next(m, start_info->data, arg); } return data; error: free_start_data(data); return NULL; }