]> git.vomp.tv Git - vompclient.git/blob - list.cc
Corruption fixes
[vompclient.git] / list.cc
1 /*
2     Copyright 2004-2005 Chris Tallon
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "list.h"
22
23 // ------ Constructor add delete get methods -------------------------------------
24
25 List::List(void)
26 {
27   start = NULL;
28   current = NULL;
29   prev = NULL;
30   numElements = 0;
31 }
32
33 List::~List()
34 {
35   while (!isEmpty())
36   {
37     reset();
38     remove();
39   }
40   numElements = 0;
41 }
42
43 void List::add(void *newData)
44 {
45   Node *temp = new Node(newData, current);
46   current = temp;
47   if (prev == 0)
48     start = current;
49   else
50     prev->setPtr(current);
51   ++numElements;
52 }
53
54 void *List::getCurrent(void) const
55 {
56   if (current == 0) return 0;
57   else return current->getData();
58 }
59
60 void *List::remove(void)
61 {
62   if (current == 0) return 0;
63   Node *dN;
64
65   if (prev == 0)
66     start = current->getPtr();
67   else
68     prev->setPtr(current->getPtr());
69   dN = current;
70   current = current->getPtr();
71
72   // saved a reference to the Node in dN and current points to where it should
73   // that's dN out of the list but it still needs to be deleted, and what it
74   // points to
75
76   void *returnObject;
77   returnObject = dN->getData();
78   delete dN;
79   --numElements;
80   return returnObject;
81 }
82
83 void List::remove(void *removeThis)
84 {
85   reset();
86   while((!eol()) && (current->getData() != removeThis)) next();
87   // Now use method above to actually remove the node and get the object
88   remove();
89 }
90
91 unsigned long int List::getNumElements()
92 {
93   return numElements;
94 }
95
96 // ------ Pointer shift methods -----------------------------------------------
97
98 void List::reset(void)
99 {
100   current = start;
101   prev = 0;
102 }
103
104 void List::next(void)
105 {
106   if (current == 0) return;
107   prev = current;
108   current = current->getPtr();
109 }
110
111 // ------ Boolean methods -----------------------------------------------------
112
113 short List::isEmpty(void) const
114 {
115   return (start == 0);
116 }
117
118 short List::eol(void) const
119 {
120   return (current == 0);
121 }
122
123 // ------ Save / load methods method --------------------------------------------
124
125 void List::save(char *fileName, long int objectLength)
126 {
127   FILE *f = fopen(fileName, "w");
128   fwrite(&objectLength, sizeof(long int), 1, f);
129
130   void *v;
131   reset();
132   while(!eol())
133   {
134     v = getCurrent();
135     fwrite(v, objectLength, 1, f);
136     next();
137   }
138   fclose(f);
139 }
140
141 int List::load(char *fileName)
142 {
143 int temp1 = 0;
144
145   int readIn = 0;
146   long int objectLength = 0;
147   FILE *f = fopen(fileName, "r");
148   if (!f) return readIn;
149
150   if (fread(&objectLength, sizeof(long int), 1, f) != 1)
151   {
152     fclose(f);
153     return 0;
154   }
155
156   void *temp = 0;
157
158   while(1)
159   {
160     temp = malloc(objectLength);
161     if ((temp1=fread(temp, objectLength, 1, f)) < 1) break;
162     readIn++;
163     add(temp);
164   }
165
166   // We allocated temp but then fread failed
167   free(temp);
168   fclose(f);
169   return readIn;
170 }