SPREADSHEET *SS_ReadCSV(char *fileName) { char line[MAXINPUTLINELEN]; char *tempRow[MAXITEMSPERROW]; SPREADSHEET *result; struct OneRow *lastRow = NULL; int i; result = malloc(sizeof(SPREADSHEET)); CHECKMALLOC(result); result->fileName = mystrdup(fileName); result->firstRow = NULL; result->numRows = result->numCols = 0; FILE *f = fopen(fileName, "r"); if (f == NULL) { fprintf(stderr, "Unable to read from file %s\n", fileName); perror(fileName); return NULL; } for( i = 0; ; i++) { if (fgets(line, MAXINPUTLINELEN, f) == NULL) break; int k = extractItems(line, tempRow); if (result->numCols == 0) { result->numCols = k; } else if (result->numCols != k) { fprintf(stderr, "Row %d has different number of columns from first row\n", i); continue; // ignore this row } // instantiate the storage for the new row and copy k cells into it char **rc = calloc(k, sizeof(char *)); CHECKMALLOC(rc); struct OneRow *newrow = malloc(sizeof(struct OneRow)); CHECKMALLOC(newrow); newrow->row = rc; newrow->nextRow = NULL; int ix; for( ix = 0; ix < k; ix++ ) { rc[ix] = tempRow[ix]; } // add the new row as the last row in the spreadsheet if (lastRow == NULL) { result->firstRow = newrow; } else { lastRow->nextRow = newrow; } lastRow = newrow; } result->numRows = i; fclose(f); return result; }
static instance_t* add_instance(PP_Instance pp_inst) { const size_t next_count = (g_instances != NULL ? g_instances->count : 0) + 1; const size_t new_array_bytes = sizeof(instance_t) * next_count; instances_t* new_instances = malloc(new_array_bytes + sizeof(instances_t)); CHECKMALLOC("malloc new_instances", new_instances, NULL); new_instances->count = next_count; instance_t* new_array = get_instances_array(new_instances); if(g_instances != NULL) { memmove(new_array, get_instances_array(g_instances), new_array_bytes); } const size_t new_idx = next_count - 1; new_array[new_idx].pp = pp_inst; new_array[new_idx].vlc = NULL; new_array[new_idx].media_player = NULL; new_array[new_idx].media_list_player = NULL; new_array[new_idx].playlist = NULL; instances_t* old_instances = g_instances; g_instances = new_instances; if(old_instances != NULL) { free(old_instances); } return &get_instances_array(g_instances)[new_idx]; }
void add_shp_to_hash(char *filename, SHPHandle sHP) { // This function does NOT check whether there already is something in // the hashtable that matches. // Check that before calling this routine. shpinfo *temp; int filenm_len; filenm_len=strlen(filename); if (!shp_hash) { // no table to add to init_shp_hash(1); // so create one } temp = (shpinfo *)malloc(sizeof(shpinfo)); CHECKMALLOC(temp); // leave room for terminator temp->filename = (char *) malloc(sizeof(char)*(filenm_len+1)); CHECKMALLOC(temp->filename); strncpy(temp->filename,filename,filenm_len+1); temp->filename[filenm_len]='\0'; // just to be safe // xastir_snprintf(temp->filename,sizeof(shpinfo),"%s",filename); temp->root = Xastir_RTreeNewIndex(); temp->creation = sec_now(); temp->last_access = temp->creation; build_rtree(&(temp->root),sHP); //fprintf(stderr, " adding %s...",temp->filename); if (!hashtable_insert(shp_hash,temp->filename,temp)) { fprintf(stderr,"Insert failed on shapefile hash --- fatal\n"); free(temp->filename); free(temp); exit(1); } }
static PP_Bool remove_instance(instance_t* instance) { assert(instance != NULL); assert(g_instances != NULL && g_instances->count > 0); const size_t new_bytes = sizeof(instance_t) * (g_instances->count - 1); instances_t* new_instances = malloc(new_bytes + sizeof(instances_t)); CHECKMALLOC("malloc new_instances", new_instances, PP_FALSE); new_instances->count = g_instances->count - 1; instance_t* cur_array = get_instances_array(g_instances); instance_t* new_array = get_instances_array(new_instances); if(new_instances->count != 0) { const size_t slice_start = (size_t)(instance - cur_array); const size_t slice_end = slice_start + 1; memmove(new_array, cur_array, (size_t)(instance - cur_array)); memmove((uint8_t*)new_array + slice_start, (uint8_t*)cur_array + slice_end, new_bytes - slice_end); } instances_t* old_instances = g_instances; g_instances = new_instances; if(instance->vlc != NULL) { libvlc_release(instance->vlc); } if(instance->media_player != NULL) { libvlc_media_player_release(instance->media_player); } if(instance->media_list_player != NULL) { libvlc_media_list_player_release(instance->media_list_player); } if(instance->playlist != NULL) { libvlc_media_list_release(instance->playlist); } free(old_instances); return PP_OK; }
// The strdup function is provided in many but not all variants of the // C library. Here it is, renamed as mystrdup, just in case. char *mystrdup(char *s) { int len = strlen(s); char *result = malloc(len+1); CHECKMALLOC(result); return strcpy(result, s); }