summaryrefslogtreecommitdiff
path: root/issuemem.c
blob: 2aa7edbb624a128dc3396ec2cb065788251a07ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * This file is part of GetHT
 *
 * See COPYING file for copyright, license and warranty details.
 *
 */

#include <stdio.h>
#include <stdlib.h>

#include "getht.h"

void nogo_mem()
/*	called if memory assignation fails
 *	TODO: handle freeing of memory to avoid leaks */
{
	fprintf(stderr, "Could not assign memory, exitting\n");
	exit(1);
}

iss ** assignnew_iss(iss ** issue, int *no_of_issues)
/* assign memory for new issue */
{
	iss ** tmp = NULL;

	if(*no_of_issues < 0)
	{       /* make issue** a new array of issue pointers */
		if( (tmp = malloc(sizeof(iss *))) == NULL )
			nogo_mem();
	}
	else
	{       /* add a new pointer to issue pointer list */
		if( (tmp = realloc(issue, sizeof(iss *) + (((*no_of_issues)+1) * sizeof(iss *)))) == NULL )
			nogo_mem();
	}

	(*no_of_issues)++;
	
	/* make new array item a pointer to issue */
	if( (tmp[*no_of_issues] = malloc(sizeof(iss))) == NULL )
		nogo_mem();

	return tmp;
}

sec ** assignnew_sec(sec ** section, int *no_of_sections)
/* assign memory for new section */
{
	sec ** tmp = NULL;

	if(*no_of_sections < 0)
	{       /* make **section a new array of section pointers */
		if( (tmp = malloc(sizeof(sec *))) == NULL )
			nogo_mem();
	}
	else
	{       /* add a new pointer to section pointer list */
		if( (tmp = realloc(section, sizeof(sec *) + (((*no_of_sections)+1) * sizeof(sec *)))) == NULL )
			nogo_mem();
	}
	
	(*no_of_sections)++;

	/* make new array item a pointer to issue */
	if( (tmp[*no_of_sections] = malloc(sizeof(sec))) == NULL )
		nogo_mem();

	return tmp;
}

it ** assignnew_it(it ** item, int * no_of_items)
{
	it ** tmp = NULL;

	if(*no_of_items < 0)
	{       /* make **item a new array of item pointers */
		if( (tmp = malloc(sizeof(it *))) == NULL )
			nogo_mem();
										                        }
	else
	{       /* add a new pointer to item pointer list */
		if( (tmp = realloc(item, sizeof(it *) + (((*no_of_items)+1) * sizeof(it *)))) == NULL )
			nogo_mem();
	}

	(*no_of_items)++;

	/* make new array item a pointer to item */
	if( (tmp[*no_of_items] = malloc(sizeof(it))) == 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;
}