File: textlist.c

package info (click to toggle)
safecopy 1.7-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 2,388 kB
  • sloc: ansic: 2,751; sh: 1,925; makefile: 19
file content (180 lines) | stat: -rw-r--r-- 5,165 bytes parent folder | download | duplicates (5)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
 * This file is copyright ©2009 Corvus Corax
 * Distributed under the terms of the GPL version 2 or higher
 */
/*This file contains the textlist class, to save the list of
  simple char texts, instead of using arrays*/

/*Maybe i should be proud of this, this is a classical!
  I always said "Who needs C++?"
  >;>>> */

#include <stdlib.h>
#include "voidlist.h"
/*We include the List object motherclass*/

/* Private structures --------------------------------------------------*/


struct lineindex {
   int line;
};
/* Our Identification (List Index) Structure */

/* Our List Data Structure is char* directly <g>*/


/* Public structures --------------------------------------------------*/

struct textlist{
   struct voidlist *liste;
};

/*The structure textlist, public itself, but members are private*/
/*Since my new class system the members are taken from class voidlist,
  so even unknown to this very file ;) */

/* Private functions --------------------------------------------------*/

int textlist_greater (struct voidident *vfrst, struct voidident *vscnd) {
   struct lineindex *frst,*scnd;
   frst=(struct lineindex*)vfrst;
   scnd=(struct lineindex*)vscnd;
   return ( (frst->line>scnd->line) );
}

int textlist_equality (struct voidident *vfrst, struct voidident *vscnd) {
   struct lineindex *frst,*scnd;
   frst=(struct lineindex*)vfrst;
   scnd=(struct lineindex*)vscnd;
   return ( (frst->line==scnd->line) );
}

int textlist_freecontent (struct voidident *videntity, struct voiddata *vdata){
   struct lineindex *identity;
   identity=(struct lineindex*)videntity;
   free (identity);
   return 0;
}

/* Function for comparisation of Index values, and to free the memory of
   the data of our list members. To tell the upper voidlist class.
   I think this is some kind of workaround since ident and data are no
   object classes but ordinary var structs */


/* Public functions ---------------------------------------------------*/

struct textlist* textlist_new() {
/*A constructor*/
   struct textlist* newtext;

   newtext=(struct textlist*)malloc(sizeof(struct textlist));
   if (!newtext) return NULL;
   newtext->liste=voidlist_new();
   if (!newtext->liste) {
      free(newtext);
      return NULL;
   }


   return newtext;
}

/* ------------------------------------------------------------------- */

int textlist_remline (struct textlist* list,int line) {
/*Removes a textline from the list 0 on success, 1 on not found, -1 on error*/
   struct lineindex identity;

   if (!list) return -1;
   identity.line=line;
   return voidlist_remitem(	list->liste,
   				(struct voidident*)&identity,
   				textlist_greater,textlist_equality,
				textlist_freecontent);

}

/* ------------------------------------------------------------------- */

int textlist_kill(struct textlist* list) {
/*A destructor, 0 on success, -1 on error*/
   if (!list) return -1;
   voidlist_kill(list->liste,textlist_freecontent);
   free(list);
   return 0;
}

/* ------------------------------------------------------------------- */

int textlist_setline (struct textlist* list,int line, char* text) {
/*Sets a line on the list to specified value
  0 added, 1, changed, -1 error
*/
   struct lineindex *identity;

   if (!list) return -1;

   identity=(struct lineindex*)malloc(sizeof(struct lineindex));
   if (!identity) return -1;
   /*We have to create a special structure in memory to add to our list */

   identity->line=line;
   /*And fill them with data*/

   return voidlist_additem (	list->liste,
   				(struct voidident*)identity,
   				(struct voiddata*)text,
   				textlist_greater,textlist_equality,
				textlist_freecontent);
   /*And include this data in the list structure*/

}
/* ------------------------------------------------------------------- */

int textlist_addline (struct textlist* list,char* text){
/*Adds a line of text to the end of the list
  0 success, -1 error
*/
   struct lineindex *identity;
   int line;

   if (!list) return -1;

   identity=(struct lineindex*)voidlist_last(list->liste);
   if (!identity) {
      line=0;
   } else {
      line=identity->line+1;
   }
   /* We calculate the position which should be the last, but maybe is also the 1st */
   identity=(struct lineindex*)malloc(sizeof(struct lineindex));
   if (!identity) return -1;
   /*We have to create a special structure in memory to add to our list */

   identity->line=line;
   /*And fill them with data*/

   return voidlist_additem (	list->liste,
   				(struct voidident*)identity,
   				(struct voiddata*)text,
   				textlist_greater,textlist_equality,
				textlist_freecontent);
   /*And include this data in the list structure*/

}

/* ------------------------------------------------------------------- */

char* textlist_line (struct textlist* list, int line){
/*Gives back a single line from position line, NULL if N/A or error*/
   struct lineindex identity;
   if (!list) return NULL;
   identity.line=line;
   return ((char*)voidlist_item (	list->liste,
   					(struct voidident*)&identity,
   					textlist_greater,textlist_equality));
}

/* -End--------------------------------------------------------------- */