aboutsummaryrefslogtreecommitdiff
path: root/src/outstream.c
blob: 10b5f981598589f229b938e3314d444354c853c9 (plain) (blame)
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
/* Copyrights 2002 Luis Figueiredo (stdio@netc.pt) All rights reserved. 
 *
 * See the LICENSE file
 *
 * The origin of this software must not be misrepresented, either by
 * explicit claim or by omission.  Since few users ever read sources,
 * credits must appear in the documentation.
 *
 * date: Sat Mar 30 14:25:25 GMT 2002 
 *
 * -- stream list functions
 */


#include "outstream.h"

/*********************************************************************************************************/ 
/*
 *	add_outstream, add a file to output (client) stream
 */
int __ILWS_add_outstream(struct outstream *list,char *fname,FILE* stream,int istmp){
	struct outstream *temp=list;
	FILE *tmp;
	while(temp->next!=NULL)temp=temp->next;
	
	if(!(temp->next=__ILWS_malloc(sizeof(struct outstream)))) {
		LWSERR(LE_MEMORY);
		return 0;
	};
	// file check (0.5.3);
	tmp=fopen(fname,"rb");
	if(tmp==NULL) {
		__ILWS_free(temp->next);
		temp->next=NULL;
		return 0;
	};
	fclose(tmp);
	// -- 
	temp->next->fname=NULL;
	if(fname!=NULL) {
		if(!(temp->next->fname=__ILWS_malloc(strlen(fname)+1))) {
			__ILWS_free(temp->next);
			temp->next=NULL;
			LWSERR(LE_MEMORY);
			return 0;
		};
		memcpy(temp->next->fname,fname,strlen(fname));
		temp->next->fname[strlen(fname)]='\0'; 
	};
	temp->next->todelete=istmp;
	temp->next->fstream=stream;
	temp->next->wsize=1;
	temp->next->rsize=0;
	temp->next->wrotesize=0;
	temp->next->varsize=0;
	temp->next->next=NULL;	
	return 1;	
}

/*********************************************************************************************************/
/*
 * Initializate (allocate) outstream list
 */
struct outstream *__ILWS_init_outstream_list() {
	struct outstream *ret;
	
	
	if(!(ret=__ILWS_malloc(sizeof(struct outstream)))) {
		LWSERR(LE_MEMORY);
		return NULL;
	};
	ret->todelete=0;
	ret->fname=NULL;
	ret->flags=0;
	ret->fstream=NULL;
	ret->next=NULL;
	return ret;
}

/*********************************************************************************************************/
/*
 * Delete a especific node
 */
void __ILWS_delete_outstream(struct outstream *node) { // Changed
	int rt;
	if(node->fstream!=NULL)fclose(node->fstream); // better here;
	if(node->todelete) { // is temporary file
		rt=unlink(node->fname);
		if(rt==-1) {
			LWSERR(LE_FILESYS);
		};
		
	};
	if(node->fname!=NULL)__ILWS_free(node->fname);
	__ILWS_free(node);
}

/*********************************************************************************************************/
/*
 * delete next node 
 */
void __ILWS_delete_next_outstream(struct outstream *node) {
	struct outstream *temp=node->next;
	node->next=node->next->next;
	__ILWS_delete_outstream(temp);
}

/*********************************************************************************************************/
/*
 * delete all nodes on the list (reset list)
 */
void __ILWS_delete_outstream_list(struct outstream *list) {
	struct outstream *temp=list;
	while(temp->next!=NULL) {
		
		__ILWS_delete_next_outstream(temp);
	};
	
	__ILWS_delete_outstream(temp);
	
}