File: LineList.c

package info (click to toggle)
ncftp2 1%3A2.4.3-2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 872 kB
  • ctags: 1,231
  • sloc: ansic: 13,766; makefile: 176; sh: 9
file content (106 lines) | stat: -rw-r--r-- 1,849 bytes parent folder | download | duplicates (6)
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
/* LineList.c */

#include "Sys.h"
#include "LineList.h"


/* Dynamically make a copy of a string. */
char *StrDup(char *buf)
{
	char *cp;
	
	cp = (char *) malloc(strlen(buf) + 1);
	if (cp != NULL)
		strcpy(cp, buf);
	return (cp);
}	/* StrDup */




/* Disposes each node of a LineList.  Does a few extra things
 * so the disposed memory won't be very useful after it is freed.
 */
void DisposeLineListContents(LineListPtr list)
{
	LinePtr lp, lp2;
	
	for (lp = list->first; lp != NULL; ) {
		lp2 = lp;
		lp = lp->next;
		lp2->line[0] = '\0';
		free(lp2->line);
		free(lp2);
	}
	/* Same as InitLineList. */
	PTRZERO(list, sizeof(LineList));
}	/* DisposeLineListContents */




void InitLineList(LineListPtr list)
{
	PTRZERO(list, sizeof(LineList));
}	/* InitLineList */




LinePtr RemoveLine(LineListPtr list, LinePtr killMe)
{
	LinePtr nextLine, prevLine;
	
	nextLine = killMe->next;	
	prevLine = killMe->prev;	
	killMe->line[0] = '\0';		/* Make it useless just in case. */
	
	if (list->first == killMe)
		list->first = nextLine;
	if (list->last == killMe)
		list->last = prevLine;

	if (nextLine != NULL)
		nextLine->prev = prevLine;
	if (prevLine != NULL)
		prevLine->next = nextLine;

	free(killMe->line);
	free(killMe);	
	list->nLines--;
	return (nextLine);
}	/* RemoveLine */




/* Adds a string to the LineList specified. */
LinePtr AddLine(LineListPtr list, char *buf)
{
	LinePtr lp;
	
	lp = (LinePtr) malloc(sizeof(Line));
	if (lp != NULL) {
		buf = StrDup(buf);
		if (buf == NULL) {
			free(lp);
			lp = NULL;
		} else {
			lp->line = buf;
			lp->next = NULL;
			if (list->first == NULL) {
				list->first = list->last = lp;
				lp->prev = NULL;
				list->nLines = 1;
			} else {
				lp->prev = list->last;
				list->last->next = lp;
				list->last = lp;
				list->nLines++;
			}
		}
	}
	return lp;
}	/* AddLine */

/* eof LineList.c */