/*************************************************************************** shuffler.c ------------------- begin : Fri Mar 21 01:05:09 CST 2003 copyright : (C) 2003 by Andy Ruder email : aeruder@yahoo.com ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ #include #include #include const long int MaxStringLength = 3000; struct item_list { struct item_list *previous; struct item_list *next; char *item_name; }; typedef struct item_list item_list; int isspace(char x) { if (x == ' ' || x == '\n') return 1; if (x == '\r' || x == '\t') return 1; return 0; } int file_exists(char *string) { FILE *x; x = fopen(string, "r"); if (x == NULL) { return 0; } fclose(x); return 1; } int load_items(item_list **playlist, char *playlist_file_name) { FILE *playlist_file; item_list *temp; long int buffer_position = 0; char buffer[MaxStringLength]; char x; int uncasted_x; long int abort_flag = 0; long int number_items = 0; playlist_file = fopen(playlist_file_name, "r"); while (!abort_flag) { buffer_position = 0; buffer[0] = 0; do { x = 0; uncasted_x = fgetc(playlist_file); if (uncasted_x == EOF) { abort_flag = 1; break; } x = (char) uncasted_x; } while(isspace(x)); while (!isspace(x)) { buffer[buffer_position] = x; uncasted_x = fgetc(playlist_file); x = 0; if (uncasted_x == EOF) { abort_flag = 1; break; } x = (char) uncasted_x; buffer_position++; if (buffer_position == MaxStringLength - 1) buffer_position --; } buffer[buffer_position] = 0; temp = malloc(sizeof(item_list)); temp->item_name = malloc(strlen(buffer) + 1); strcpy(temp->item_name, buffer); temp->next = *playlist; //temp->previous = NULL; //(*playlist)->previous = temp; *playlist = temp; number_items++; } fclose(playlist_file); return number_items-1; } void swap_item(item_list **list, long int x, long int y, long int items) { long int z; item_list *temp; item_list *x_list; item_list *y_list; char *temp_string; if (x > items) return; if (y > items) return; for (temp = *list, z = 0; temp != NULL && z != x; z++, temp = temp->next); x_list = temp; for (temp = *list, z = 0; temp != NULL && z != y; z++, temp = temp->next); y_list = temp; if (x_list == NULL || y_list == NULL) return; if (x_list == y_list) return; temp_string = x_list->item_name; x_list->item_name = y_list->item_name; y_list->item_name = temp_string; } int main(int argc, char *argv[]) { item_list *playlist = NULL; long int number_of_items; item_list *temp_list = NULL; item_list *temp_list2 = NULL; long int x; if (argc == 1) { printf("Syntax : shuffler \n"); return 1; } if (!file_exists(argv[1])) { printf("File does not exist!\n"); return 2; } number_of_items = load_items(&playlist, argv[1]); srand(time(NULL)); /* for (temp_list = playlist; temp_list != NULL; temp_list = temp_list->next) { printf("%s ", temp_list->item_name); }*/ for (x = 1; x <= number_of_items; x++) { swap_item(&playlist, x, rand() % number_of_items + 1, number_of_items); } for (temp_list = playlist; temp_list != NULL; temp_list = temp_list->next) { printf("%s\n", temp_list->item_name); } for (temp_list = playlist; temp_list != NULL; ) { temp_list2 = temp_list->next; free(temp_list->item_name); free(temp_list); temp_list = temp_list2; } playlist = NULL; return 0; }