File: files.c

package info (click to toggle)
freesweep 0.88-4.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, sarge
  • size: 404 kB
  • ctags: 267
  • sloc: ansic: 3,813; sh: 2,460; makefile: 112
file content (209 lines) | stat: -rw-r--r-- 5,543 bytes parent folder | download | duplicates (2)
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
/**********************************************************************
*  This source code is copyright 1999 by Gus Hartmann & Peter Keller  *
*  It may be distributed under the terms of the GNU General Purpose   *
*  License, version 2 or above; see the file COPYING for more         *
*  information.                                                       *
*                                                                     *
*  $Id: files.c,v 1.8 1999/08/09 05:25:35 hartmann Exp $
*                                                                     *
**********************************************************************/

#include "sweep.h"

int SourceFile(GameStats* Game,FILE* PrefsFile)
{
	char RawBuffer[MAX_LINE];
	char *NameBuffer, *ValueBuffer;
	int Value=0;

	while (feof(PrefsFile)==0)
	{
		if (fgets(RawBuffer,MAX_LINE,PrefsFile)==0)
		{
			return 0;
		}
		if ((NameBuffer=strtok(RawBuffer,DELIMITERS))!=0)
		{
			/* This is essentially a great big switch statement. */
			if ((ValueBuffer=strtok(NULL,DELIMITERS))!=0)
			{
				if (strncasecmp(NameBuffer,"color",5)==0)
				{
					Value=atoi(ValueBuffer);
					((CheckColor(Value)>0)?Game->Color=Value:fprintf(stderr,"Invalid value for color in preference file.\n"));
				}
				else if (strncasecmp(NameBuffer,"faststart",9)==0)
				{
					Value=atoi(ValueBuffer);
					((CheckFast(Value)>0)?Game->Fast=Value:fprintf(stderr,"Invalid value for faststart in preference file.\n"));
				}
				else if (strncasecmp(NameBuffer,"height",6)==0)
				{
					Value=atoi(ValueBuffer);
					((CheckHeight(Value)>0)?Game->Height=Value:fprintf(stderr,"Invalid value for height in preference file.\n"));
				}
				else if (strncasecmp(NameBuffer,"linedraw",8)==0)
				{
					Value=atoi(ValueBuffer);
					((CheckLineDraw(Value)>0)?InitCharSet(Game,Value):fprintf(stderr,"Invalid value for linedraw in preference file.\n"));
				}
				else if (strncasecmp(NameBuffer,"percent",7)==0)
				{
					Value=atoi(ValueBuffer);
					((CheckPercent(Value)>0)?Game->Percent=Value,Game->NumMines=0:fprintf(stderr,"Invaid value for percent in preference file.\n"));
				}
				else if (strncasecmp(NameBuffer,"mines",5)==0)
				{
					Value=atoi(ValueBuffer);
					((CheckNumMines(Value,Game->Height,Game->Width)>0)?Game->NumMines=Value,Game->Percent=0:fprintf(stderr,"Invalid value for mines in preference file.\n"));
				}
				else if (strncasecmp(NameBuffer,"width",5)==0)
				{
					Value=atoi(ValueBuffer);
					((CheckWidth(Value)>0)?Game->Width=Value:fprintf(stderr,"Invalid value for width in preference file.\n"));
				}
				else if (strncasecmp(NameBuffer,"alert",5)==0)
				{
					/* This option takes different strings as arguments. */
					if (strncasecmp(ValueBuffer,"beep",4)==0)
					{
						Game->Alert=BEEP;
					}
					else if (strncasecmp(ValueBuffer,"flash",5)==0)
					{
						Game->Alert=FLASH;
					}
					else if (strncasecmp(ValueBuffer,"none",4)==0)
					{
						Game->Alert=NO_ALERT;
					}
					else
					{
						fprintf(stderr,"Bad value for Alert in preference file.\n");
					}
				}
				else if (strncasecmp(NameBuffer,"#",1)!=0)
				{
					fprintf(stderr,"Unknown tag %s in file.\n",NameBuffer);
				}
			}
		}
	}
	return 0;
}

int SourceHomeFile(GameStats* Game)
{
	FILE* PrefsFile;
	char *Buffer, *Pathname;

	Pathname=(char*)xmalloc(MAX_LINE);

	if ((Buffer=getenv("HOME"))==NULL)
	{
		perror("SourceHomeFile::getenv");
		return 1;
	}
	snprintf(Pathname,MAX_LINE-1,"%s/%s", Buffer, DFL_PREFS_FILE);

	if ((PrefsFile=fopen(Pathname,"r"))==NULL)
	{
		/* The user has no personal preferences. */
		free(Pathname);
		return 0;
	}
	else if (SourceFile(Game,PrefsFile)==1)
	{
/*		An error occurred while reading the file. Try closing it. */
		fclose(PrefsFile);
		free(Pathname);
		return 1;
	}
	else
	{
/*		Done, so close the file. */
		fclose(PrefsFile);
		free(Pathname);
		return 0;
	}

}

int SourceGlobalFile(GameStats* Game)
{
	FILE* PrefsFile;

	if ((PrefsFile=fopen(GLOBAL_PREFS_FILE,"r"))==NULL)
	{
/*		The global file is invalid or non-existant. */
		fprintf(stderr,"Unable to open the global prefernces file %s\n", GLOBAL_PREFS_FILE);
/*		perror("SourceGlobalFile::fopen"); */
		return 1;
	}
	else if (SourceFile(Game,PrefsFile)==1)
	{
/*		An error occurred while reading the file. Try closing it. */
		fclose(PrefsFile);
		return 1;
	}
	else
	{
/*		Done, so close the file. */
		fclose(PrefsFile);
		return 0;
	}
}

int WritePrefsFile(GameStats* Game)
{
	FILE* PrefsFile;
	char *Buffer, Pathname[MAX_LINE+1];

	if ((Buffer=getenv("HOME"))==NULL)
	{
		perror("WriteHomeFile::getenv");
		return 1;
	}
	
	snprintf(Pathname,MAX_LINE-1, "%s/%s", Buffer, DFL_PREFS_FILE);

	if ((PrefsFile=fopen(Pathname,"w"))==NULL)
	{
		perror("WritePrefsFile::fopen");
		return 1;
	}
	else
	{
		fprintf(PrefsFile,"# Freesweep version %s\n",VERSION);
		fprintf(PrefsFile,"# This is a generated file, but you can edit it if you like.\n");
		fprintf(PrefsFile,"Height=%d\n",Game->Height);
		fprintf(PrefsFile,"Width=%d\n",Game->Width);
		if (Game->NumMines==0)
		{
			fprintf(PrefsFile,"Percent=%d\n",Game->Percent);
		}
		else
		{
			fprintf(PrefsFile,"Mines=%d\n",Game->NumMines);
		}
		fprintf(PrefsFile,"FastStart=%d\n",Game->Fast);
		fprintf(PrefsFile,"LineDraw=%d\n",Game->LineDraw);
		switch (Game->Alert)
		{
			case BEEP:
				fprintf(PrefsFile,"Alert=Beep\n");
				break;
			case FLASH:
				fprintf(PrefsFile,"Alert=Flash\n");
				break;
			case NO_ALERT:
				fprintf(PrefsFile,"Alert=None\n");
				break;
			default:
				break;
		}

		fclose(PrefsFile);
	}
	return 0;
}