-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.c
213 lines (165 loc) · 4.67 KB
/
files.c
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
/* Helper functions to load program data (basically texts) from files.
Assume all of this to be horribly thread-unsafe! */
#include <stdio.h>
#include <strings.h>
#include "lib.h"
#include "conio.h"
char *language = "nl";
#define SINGLE_LINE_LENGTH 100
#define ROOM_TEXT_LENGTH 200
/* File numbers */
#define ITEM_NAMES 0
#define ITEM_DESCRIPTIONS 1
static char single_line_text[SINGLE_LINE_LENGTH];
static char room_text[ROOM_TEXT_LENGTH];
char *replace_char(str, find, replace)
char *str, find, replace;
{
char *current_pos = strchr(str, find);
while (current_pos)
{
*current_pos = replace;
current_pos = strchr(current_pos,find);
}
return str;
}
#define put_newlines(str) replace_char((str), '_', '\n')
FILE *OpenDataFile(letter, number)
char letter, number;
{
char composed_path[12];
FILE *fp;
sprintf(composed_path, "data/%s/%c%d", language, letter, number);
fp = fopen(composed_path, "r");
if (fp == NULL)
wprintw(mainscr, "Error opening file %s!\n", composed_path);
return fp;
}
void PrintLivingStatus(id, status)
char id, status;
{
FILE *fp;
char line[100];
char *result;
bool printed = FALSE;
fp = OpenDataFile('l', id);
if (fp == NULL)
return;
/* Skip lines until we find the file section for our status */
do
result = fgets(line, 100, fp);
while (result != NULL && (strlen(line) < 3 || (line[0] != 'K' || line[1] != 'J' || line[2] != 'n' - status)));
/* This (EOF) can happen if the text file contains no text for our status */
if (result == NULL)
{
fclose(fp);
return;
}
/* Read and print the status text for our status, until we hit the next status marker */
while (fgets(line, 100, fp) && (strlen(line) < 2 || (line[0] != 'K' || line[1] != 'J')))
{
fuzzle(line);
cputs(line);
printed = TRUE;
}
/* Only add another newline if we actually printed any status text */
if (printed)
putch('\n');
fclose(fp);
}
char *GetSingleLineText(letter, number, line, add_newlines)
char letter, number, line;
bool add_newlines;
{
int i;
FILE *fp;
fp = OpenDataFile(letter, number);
if (fp == NULL)
return NULL;
for (i = 0; i <= line && fgets(single_line_text, SINGLE_LINE_LENGTH, fp); i++);
fclose(fp);
single_line_text[strlen(single_line_text) - 1] = 0;
fuzzle(single_line_text);
return add_newlines ? put_newlines(single_line_text) : single_line_text;
}
void GetRoomText(number, name, description)
char number, **name, **description;
{
int i;
FILE *fp;
char filenumber, *semicolon;
filenumber = number / 20; /* We keep info for 20 rooms in each file */
number %= 20;
fp = OpenDataFile('r', filenumber);
for (i = 0; i <= number && fgets(room_text, ROOM_TEXT_LENGTH, fp); i++);
fclose(fp);
fuzzle(room_text);
*name = room_text;
semicolon = strchr(room_text, ';');
if (semicolon == NULL)
{
*description = NULL;
return;
}
*description = semicolon + 1;
*semicolon = 0;
put_newlines(*description);
}
int LoadStrings(string_array, count, letter, number, add_newlines)
char **string_array;
int count;
char letter, number;
bool add_newlines;
{
int i, string_length, lines_read;
FILE *fp;
char line[100];
fp = OpenDataFile(letter, number);
if (fp == NULL)
return;
for (i = 0; i < count && fgets(line, 100, fp); i++)
{
string_length = strlen(line);
string_array[i] = (char *)malloc(string_length);
/* We don't want the newline at the end of the string */
memcpy(string_array[i], line, string_length - 1);
string_array[i][string_length - 1] = 0;
fuzzle(string_array[i]);
}
lines_read = i;
if (add_newlines)
for (i = 0; i < count; i++)
put_newlines(string_array[i]);
fclose(fp);
return lines_read;
}
void PrintFile(letter, number, centered)
char letter, number;
bool centered;
{
FILE *fp;
char line[100];
char *result;
fp = OpenDataFile(letter, number);
if (fp == NULL)
return;
while (fgets(line, 100, fp))
{
fuzzle(line);
if (line[0] == 0); /* Ignore the last line without the newline*/
else if (line[0] == '\n')
putch('\n');
/* If the first character isn't a newline then we have at least one character,
a newline and \0. */
else if (line[0] == 'B' && line[1] == 'R' && line[2] == '\n')
{
getch();
clrscr();
}
else if (centered)
PrintCentered(line);
else
cputs(line);
}
fclose(fp);
}