-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.c
executable file
·226 lines (189 loc) · 4.83 KB
/
file.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
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "file.h"
#include "finder.h"
#include "main.h"
#include "editor.h"
#include "ui.h"
#include <stdint.h>
#include <fileioc.h>
#include <string.h>
#include <stdbool.h>
#include <keypadc.h>
#include <debug.h>
uint8_t loadFiles(struct file files[MAX_FILES_LOADABLE])
{
uint8_t numFiles = 0;
char *osName = NULL;
void *search_pos = NULL;
uint8_t fileSlot;
while ((osName = ti_Detect(&search_pos, FILE_DETECT_STRING)) != NULL)
{
if (numFiles > MAX_FILES_LOADABLE)
{
return numFiles;
}
// store the file's os name (the name the calculator uses to identify files)
strcpy(files[numFiles].osName, osName);
// store the aesthetic name (the name the user sees)
fileSlot = ti_Open(files[numFiles].osName, "r");
if(!fileSlot)
{
alert("Warning!", "Error loading file. Not going to load any more files.");
break;
}
ti_Seek(AESTHETIC_FILE_NAME_POS, SEEK_SET, fileSlot);
ti_Read(files[numFiles].aestheticName, (AESTHETIC_FILE_NAME_LEN - 1), 1, fileSlot);
ti_Close(fileSlot);
// don't forget the null byte after the aesthetic name string
files[numFiles].aestheticName[AESTHETIC_FILE_NAME_LEN - 1] = '\0';
numFiles++;
}
return numFiles;
}
void loadFileData(struct file *file)
{
// check if it exists
uint8_t slot = ti_Open(file->osName, "r");
if(!slot)
{
programState = QUIT;
return;
}
// get file size and make sure it doesn't exceed the maximum
file->size = ti_GetSize(slot);
file->dataSize = file->size - FILE_METADATA_SIZE;
if(file->size > MAX_FILE_SIZE || file->size < FILE_METADATA_SIZE)
{
dbg_printf("File can't be opened because of its size\n");
programState = QUIT;
return;
}
editor.cursorInsert = editor.buffer;
editor.afterCursor = editor.buffer + MAX_DATA_SIZE - file->dataSize;
editor.startOfPage = editor.afterCursor;
// copy file's data into the end of the editor text buffer
ti_Seek(FILE_DATA_POS, SEEK_SET, slot);
ti_Read(editor.afterCursor, file->dataSize, 1, slot);
ti_Close(slot);
}
uint8_t getNumFiles(void)
{
uint8_t numFiles = 0;
void *pos = NULL;
while (ti_Detect(&pos, FILE_DETECT_STRING) != NULL)
{
numFiles++;
}
return numFiles;
}
int toggleHiddenStatus(char* name)
{
ti_var_t fileSlot = ti_Open(name, "r");
if(!fileSlot || strlen(name) > 8)
{
ti_Close(fileSlot);
return -1;
}
char temp[10] = {0};
ti_GetName(temp, fileSlot);
temp[0] ^= 64;
ti_Close(fileSlot);
ti_Rename(name, temp);
return isHidden(temp);
}
bool isHidden(char* name)
{
return (name[0])<(65);
}
void archiveAllFiles(void)
{
void *searchPtr = NULL;
char *namePtr = NULL;
ti_var_t fileSlot;
while ((namePtr = ti_Detect(&searchPtr, FILE_DETECT_STRING)) != NULL)
{
fileSlot = ti_Open(namePtr, "r");
if(!fileSlot)
{
return;
}
if (ti_SetArchiveStatus(true, fileSlot))
{
ti_Close(fileSlot);
}
else
{
return;
}
}
}
bool createNotesFile(char *aestheticName)
{
uint8_t slot, hiddenSlot;
char osName[9] = {'\0'};
char hiddenOsName[9] = {'\0'};
uint8_t nullByte = 0;
uint8_t creationAttempts = 0, maxCreationAttempts = 100;
if(strlen(aestheticName) == 0)
{
return false;
}
for(int i = 1; i < 8; i++)
{
osName[i] = aestheticName[i];
hiddenOsName[i] = aestheticName[i];
}
osName[0] = 65;
hiddenOsName[0] = osName[0] ^ 64;
while(creationAttempts < maxCreationAttempts)
{
// check if a file already exists with the same os name (hidden or not)
slot = ti_Open(osName, "r");
hiddenSlot = ti_Open(hiddenOsName, "r");
ti_Close(slot);
ti_Close(hiddenSlot);
// if no file exists with the same os name...
if(!slot && !hiddenSlot)
{
slot = ti_Open(osName, "w+");
ti_Seek(FILE_DETECT_STRING_POS, SEEK_SET, slot);
ti_Write(FILE_DETECT_STRING, sizeof(FILE_DETECT_STRING) - 1, 1, slot);
ti_Seek(AESTHETIC_FILE_NAME_POS, SEEK_SET, slot);
ti_Write(&nullByte, 1, AESTHETIC_FILE_NAME_LEN, slot);
ti_Seek(AESTHETIC_FILE_NAME_POS, SEEK_SET, slot);
ti_Write(aestheticName, strlen(aestheticName), 1, slot);
ti_Write(&nullByte, 1, 1, slot);
ti_Seek(AESTHETIC_FILE_NAME_POS + AESTHETIC_FILE_NAME_LEN, SEEK_SET, slot);
ti_Write(&nullByte, 1, 1, slot);
ti_Close(slot);
return true;
}
// if a file with the same os name already exists, change the os name a tad until it's unique
else
{
osName[0]++;
hiddenOsName[0] = osName[0] ^ 64;
creationAttempts++;
}
}
while(kb_AnyKey()) kb_Scan();
while(!kb_AnyKey()) kb_Scan();
return false;
}
bool askIfDeleteFile(void)
{
char bodyString[95] = {"Do you really want to delete \""};
strcat(bodyString, finder.files[finder.selectedFile].aestheticName);
strcat(bodyString, "\"? Press enter to delete, and clear to cancel.");
bool delete = alert("Warning!", bodyString);
if(delete)
{
ti_Delete(finder.files[finder.selectedFile].osName);
finder.reloadFiles = true;
while(kb_IsDown(kb_KeyEnter)) kb_Scan();
return true;
}
else
{
return false;
}
}