void folder_dump(FileSystem *fs, Inode *folder_inode, FILE *outfile) { Folder *folder = NULL; int i = 0; int nfile = 0; int nfolder = 0; char **file_names; char **folder_names; int ifile = 0; int ifolder = 0; folder = folder_open(fs, folder_inode); for (i = 0; i < folder->nitem; ++i) { if (skip_folder_item(folder->items[i].cname)) continue; if (fs_load_inode(fs, folder->items[i].page_num)->type == INODE_FILE) { nfile++; } else { nfolder++; } } file_names = malloc(sizeof(char *) * nfile); folder_names = malloc(sizeof(char *) * nfolder); for (i = 0; i < folder->nitem; ++i) { if (skip_folder_item(folder->items[i].cname)) continue; if (fs_load_inode(fs, folder->items[i].page_num)->type == INODE_FILE) { file_names[ifile] = malloc(strlen(folder->items[i].cname) + 1); strcpy(file_names[ifile], folder->items[i].cname); ifile++; } else { folder_names[ifolder] = malloc(strlen(folder->items[i].cname) + 1); strcpy(folder_names[ifolder], folder->items[i].cname); ifolder++; } } sort_strings(file_names, nfile); sort_strings(folder_names, nfolder); for (i = 0; i < nfile; ++i) { if (i) fprintf(outfile, " "); fprintf(outfile, "%s", file_names[i]); } fprintf(outfile, " & "); for (i = 0; i < nfolder; ++i) { if (i) fprintf(outfile, " "); fprintf(outfile, "%s", folder_names[i]); } fprintf(outfile, "\n"); fflush(outfile); free(file_names); free(folder_names); folder_close(&folder); }
/// Sort "gap" and remove duplicate entries. "gap" is expected to contain a /// list of file names in allocated memory. /// /// @param gap void ga_remove_duplicate_strings(garray_T *gap) { int i; int j; char_u **fnames = (char_u **)gap->ga_data; sort_strings(fnames, gap->ga_len); for (i = gap->ga_len - 1; i > 0; --i) if (fnamecmp(fnames[i - 1], fnames[i]) == 0) { vim_free(fnames[i]); for (j = i + 1; j < gap->ga_len; ++j) fnames[j - 1] = fnames[j]; --gap->ga_len; } }
int main(int argc, char *argv[]) { int i; char *args[argc - 1]; i = 0; while (++i < argc) args[i - 1] = argv[i]; sort_strings(args, argc - 1); i = -1; while (++i < argc - 1) { ft_putstr(args[i]); ft_putchar('\n'); } return (0); }
END_TEST START_TEST(test_sort_strings) { char strs[][4][20] = {{ "oNe", "TWO", "three", "Four" }, { "ALPHA", "beta", "Gamma", "Theta" }}; char ref[][4][20] = {{ "Four", "TWO", "oNe", "three" }, { "ALPHA", "Gamma", "Theta", "beta" }}; const unsigned int num = 4; for (int k = 0; k < 2; k++) { char **arr = init_array(); if(!arr) { fail("[Task 4.3.d] init_array returned NULL"); } for (unsigned int i = 0; i < num; i++) { char **a2 = add_string(arr, strs[k][i]); if (!a2) { free_strings(arr); fail("[Task 4.3.d] add_string returned NULL"); } arr = a2; } char buf[200]; printref(buf, strs[k], num); sort_strings(arr); for (unsigned int i = 0; i < num; i++) { if (strcmp(arr[i], ref[k][i])) { remove_nonascii(arr[i]); char buf2[200]; sprintf(buf2, "[Task 4.3.c] After adding strings {%s}, array member %d is '%s', should be '%s'", buf, i, arr[i], ref[k][i]); free_strings(arr); fail(buf2); } } free_strings(arr); } }
int main (int argc, char* argv[]) { char* in[6]; in[6] = NULL; char* out[6]; out[0] = NULL; for (int i = 1; i < argc; i++) { in[i - 1] = argv[i]; } for (int i = 0; i < 6; i++) { printf("%s\n", in[i]); } sort_strings(in, out); for (int i = 0; i < 5; i++) { printf("%s\n", out[i]); } }
/// Sort "gap" and remove duplicate entries. "gap" is expected to contain a /// list of file names in allocated memory. /// /// @param gap void ga_remove_duplicate_strings(garray_T *gap) { char_u **fnames = gap->ga_data; // sort the growing array, which puts duplicates next to each other sort_strings(fnames, gap->ga_len); // loop over the growing array in reverse for (int i = gap->ga_len - 1; i > 0; i--) { if (fnamecmp(fnames[i - 1], fnames[i]) == 0) { vim_free(fnames[i]); // close the gap (move all strings one slot lower) for (int j = i + 1; j < gap->ga_len; j++) { fnames[j - 1] = fnames[j]; } --gap->ga_len; } } }
int main() { struct occurance_t array_of_occurance[3000]; int hash, lenght = 0, index, j; char *word; while(1) { word = (char*) malloc(sizeof(word)); scanf("%s", word); hash = long_hash(word); index = get_occurance_t(array_of_occurance, lenght, hash); if(index == -1) { array_of_occurance[lenght].count = 1; array_of_occurance[lenght].hash = hash; array_of_occurance[lenght].words[0] = word; array_of_occurance[lenght].len = 1; lenght++; } else { int hasWord = 0; int i; for(i = 0; i < array_of_occurance[index].len; i++) { if(strcmp(array_of_occurance[index].words[i], word) == 0) { hasWord = 1; break; } } if(hasWord == 0) { array_of_occurance[index].words[array_of_occurance[index].len] = word; array_of_occurance[index].len++; array_of_occurance[index].count++; } if(array_of_occurance[index].count == 4) { sort_strings(array_of_occurance, lenght); for(i = 0; i < lenght; i++) { if(array_of_occurance[i].count > 1) { printf("%d ", array_of_occurance[i].hash); for(j = 0; j < array_of_occurance[i].len; j++) { printf("%s ", array_of_occurance[i].words[j]); } printf("\n"); } } return 0; } } } return 0; }