File: stack.c

package info (click to toggle)
wcd 3.1.4-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 776 kB
  • ctags: 633
  • sloc: ansic: 8,105; makefile: 44; sh: 6; csh: 1
file content (229 lines) | stat: -rw-r--r-- 5,118 bytes parent folder | download
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
Copyright (C) 1997-2000 Erwin Waterlander

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "std_macro.h"
#include "structures.h"
#include "nameset.h"
#include "display.h"
#include "dosdir.h"
#include "WcdStack.h"
#include "Text.h"
#include "wcd.h"
#include "stack.h"


/********************************************************************
 *
 *                 stack_add
 *
 ********************************************************************/

int stack_add(WcdStack ws, char *dir)
{
	ws->lastadded++;

	if(ws->lastadded == ws->maxsize)
		ws->lastadded = 0;

	ws->current = ws->lastadded;

	/* printWcdStack("add ", ws, stdout); */

	/* free old dir string if present */
	if (ws->dir != NULL)
	if((ws->dir[ws->lastadded] != NULL) && (ws->size == ws->maxsize))
	free (ws->dir[ws->lastadded]);

	putElementAtWcdStackDir(textNew(dir), ws->lastadded, ws);

	return(0);
}
/********************************************************************
 *
 *                 stack_read
 *
 ********************************************************************/

int stack_read(WcdStack ws,char *stackfilename)
{

	FILE *infile;
	char tmp[DD_MAXDIR];


	/* open stack-file */
	if  ((ws->maxsize <= 0)||((infile = fopen(stackfilename,"r")) == NULL))
	{
		/* fprintf(stderr,"Wcd: Error opening stack-file %s\n",stackfilename); */
		ws->lastadded = -1;
		ws->current = -1;
	}
	else
	{
		if(fscanf(infile,"%d %d",&ws->lastadded,&ws->current)==2)
		{

			while( !feof(infile)&&(ws->size < ws->maxsize) )
			{
			int len ;
			/* read a line */
			len = getline(tmp,DD_MAXDIR,infile);

			if (len > 0 )
				addToWcdStackDir(textNew(tmp), ws);
			}
		}
		else
		  {
			fprintf(stderr,"Wcd: Error parsing stack\n");
			ws->lastadded = -1;
			ws->current = -1;
		  }

		fclose(infile);

		if (ws->lastadded >= ws->size)
		ws->lastadded = 0;
		if (ws->current >= ws->size)
		ws->current = 0;
	}

 /*	printWcdStack("READ ", ws, stdout); */
	return(0);
}

/*******************************************************************/

int stack_print(WcdStack ws, int use_numbers, int use_stdout)
{
#ifdef WCD_USECONIO
	if (use_stdout == 0)
		return display_list_conio(NULL,ws,0,use_numbers);
	else
		return display_list_stdout(NULL,ws,0);
#else
# ifdef WCD_USECURSES
	int i;
	if ((use_stdout == 0) && ((i = display_list_curses(NULL,ws,0,use_numbers)) != WCD_ERR_CURSES))
		return i;
	else
		return display_list_stdout(NULL,ws,0);
# else
	return display_list_stdout(NULL,ws,0);
# endif
#endif
}
/********************************************************************
 *
 *                 stack_push
 *
 ********************************************************************/

char* stack_push(WcdStack ws, int push_ntimes)
{

	int  new_stack_nr;

	if(ws == NULL)
	return (NULL);
	else
		if( ((ws->size) <= 0) || ((ws->size) > ws->maxsize) )
		return (NULL);
		else
		{

			  push_ntimes = push_ntimes % ws->size;

			  new_stack_nr = ws->current - push_ntimes;

			  if(new_stack_nr < 0)
			  new_stack_nr = ws->size + new_stack_nr;

			  ws->current = new_stack_nr;

			  return(ws->dir[ws->current]);
		}
}
/********************************************************************
 *
 *                 stack_pop
 *
 *
 *
 ********************************************************************/

char* stack_pop(WcdStack ws, int pop_ntimes)
{

	int  new_stack_nr;


	if(ws == NULL)
	return (NULL);
	else
		if( ((ws->size) <= 0) || ((ws->size) > ws->maxsize) )
		return (NULL);
		else
		{
	         pop_ntimes = pop_ntimes % ws->size;

	         new_stack_nr = ws->current + pop_ntimes;

	         if(new_stack_nr > (ws->size -1))
	         new_stack_nr =  new_stack_nr - ws->size;

	         ws->current = new_stack_nr;
	         return(ws->dir[ws->current]);
		}
}
/********************************************************************
 *
 *                 stack_write
 *
 ********************************************************************/

int stack_write(WcdStack ws,char *stackfilename)
{
	FILE *outfile;
	int  i;

	if (ws->maxsize <= 0)
	   return(0);
	else
	if ( (outfile = fopen(stackfilename,"w")) == NULL)
	{
		fprintf(stderr,"Wcd: Error opening stack-file %s for write.\n",stackfilename);
		return(0);
	}
	else
	{
		fprintf(outfile,"%d %d\n",ws->lastadded,ws->current);
		for(i=0;((i<ws->size)&&(i<ws->maxsize));i++)
		{
	  /*		printf("writing line %d\n",i);  */
			fprintf(outfile,"%s\n",ws->dir[i]);
		}
		fclose(outfile);
	}
	return(0);
}