From 1edf37e3b0ad7b0556ba0902b5880044933ced66 Mon Sep 17 00:00:00 2001 From: Nick White Date: Mon, 30 Apr 2007 08:34:43 +0000 Subject: Removed last of static issue array code, added sorting Removed defined constants from issue.h previously used to determine size of static arrays Removed unused separate show media structure function Remove clean media & clean issue functions Added issue sorting code git-archimport-id: getht@sv.gnu.org/getht--mainline--0.1--patch-22 --- issuemem.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 70 insertions(+), 3 deletions(-) (limited to 'issuemem.c') diff --git a/issuemem.c b/issuemem.c index f915de7..3a40d7e 100644 --- a/issuemem.c +++ b/issuemem.c @@ -21,6 +21,7 @@ #include #include +#include #include "issue.h" @@ -32,7 +33,7 @@ void nogo_mem() exit(1); } -iss ** assignnew_iss(int *no_of_issues, iss ** issue) +iss ** assignnew_iss(iss ** issue, int *no_of_issues) /* assign memory for new issue */ { iss ** tmp = NULL; @@ -57,7 +58,7 @@ iss ** assignnew_iss(int *no_of_issues, iss ** issue) return tmp; } -sec ** assignnew_sec(int *no_of_sections, sec ** section) +sec ** assignnew_sec(sec ** section, int *no_of_sections) /* assign memory for new section */ { sec ** tmp = NULL; @@ -82,7 +83,7 @@ sec ** assignnew_sec(int *no_of_sections, sec ** section) return tmp; } -it ** assignnew_it(int * no_of_items, it ** item) +it ** assignnew_it(it ** item, int * no_of_items) { it ** tmp = NULL; @@ -105,3 +106,69 @@ it ** assignnew_it(int * no_of_items, it ** item) return tmp; } + +med ** assignnew_med(med ** media, int * no_of_media) +{ + med ** tmp = NULL; + + if(*no_of_media < 0) + { /* make **section a new array of section pointers */ + if( (tmp = malloc(sizeof(med *))) == NULL ) + nogo_mem(); + } + else + { /* add a new pointer to media pointer list */ + if( (tmp = realloc(media, sizeof(med *) + (((*no_of_media)+1) * sizeof(med *)))) == NULL ) + nogo_mem(); + } + + (*no_of_media)++; + + /* make new array item a pointer to issue */ + if( (tmp[*no_of_media] = malloc(sizeof(med))) == NULL ) + nogo_mem(); + + return tmp; +} + +int issuesort(iss ** issue, int no_of_issues) +/* does a basic bubble sort, by date, returning sorted issue */ +{ + int sortindex[no_of_issues]; + + int count1, count2, temp; + + for(count1 = 0; count1 <= no_of_issues; count1++) + sortindex[count1] = count1; + + /* find correct order of issues using a bubble sort */ + for(count1 = 0; count1 <=no_of_issues; count1++) + { + for(count2 = 0; count2 < no_of_issues; count2++) + { + if(issue[sortindex[count2]]->date.year < issue[sortindex[count2+1]]->date.year) + { + temp = sortindex[count2]; + sortindex[count2] = sortindex[count2+1]; + sortindex[count2+1] = temp; + } + else if((issue[sortindex[count2]]->date.year == issue[sortindex[count2+1]]->date.year) && + (issue[sortindex[count2]]->date.firstmonth < issue[sortindex[count2+1]]->date.firstmonth)) + { + temp = sortindex[count2]; + sortindex[count2] = sortindex[count2+1]; + sortindex[count2+1] = temp; + } + } + } + + iss * sortedissue[no_of_issues]; + + for(count1 = 0; count1 <= no_of_issues; count1++) + sortedissue[count1] = issue[sortindex[count1]]; + + for(count1 = 0; count1 <= no_of_issues; count1++) + issue[count1] = sortedissue[count1]; + + return 0; +} -- cgit v1.2.3