forked from swissmicros/SDKdemo
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathfile.h
182 lines (152 loc) · 5.26 KB
/
file.h
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
#ifndef FILE_H
# define FILE_H
// ****************************************************************************
// file.h DB48X project
// ****************************************************************************
//
// File Description:
//
// Abstract the DMCP zany filesystem interface
//
//
//
//
//
//
//
//
// ****************************************************************************
// (C) 2023 Christophe de Dinechin <[email protected]>
// This software is licensed under the terms outlined in LICENSE.txt
// ****************************************************************************
// This file is part of DB48X.
//
// DB48X is free software: you can redistribute it and/or modify
// it under the terms outlined in the LICENSE.txt file
//
// DB48X 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.
// ****************************************************************************
#include "dmcp.h"
#include "types.h"
#include <stdio.h>
// For the text pointer variant of the constructor
typedef const struct text *text_p;
struct file
// ----------------------------------------------------------------------------
// Direct access to daa files
// ----------------------------------------------------------------------------
// This class deals with a linked list of files, because DMCP has a really
// annoying limit where only one file can be open at a time.
{
enum mode { READING, WRITING, APPEND };
file();
file(cstring path, mode wrmode);
file(text_p path, mode wrmode);
~file();
void open(cstring path, mode wrmode);
void close(bool reopen = true);
void reopen();
bool valid();
bool eof();
bool put(unicode out);
bool put(char c);
bool write(const char *buf, size_t len);
bool read(char *buf, size_t len);
unicode get();
unicode get(uint offset);
char getchar();
void seek(uint offset);
unicode peek();
uint position();
uint find(unicode cp);
uint find(unicode cp1, unicode cp2);
uint rfind(unicode cp);
uint rfind(unicode cp1, unicode cp2);
cstring error(int err) const;
cstring error() const;
cstring filename() const { return name; }
static bool unlink(text_p path);
static bool unlink(cstring path);
static cstring extension(cstring path);
static cstring basename(cstring path);
protected:
static file *current; // Only one open file at a time
#if SIMULATOR
typedef FILE *FIL;
#endif // SIMULATOR
FIL data;
cstring name; // File name to use when reopening
uint closed; // Position in file when closing
file * previous; // Previous file to reopen when closing
bool writing; // Should we reopen for writing
};
#define MAGIC_SAVE_STATE 0x05121968
// ============================================================================
//
// DMCP wrappers
//
// ============================================================================
#ifndef SIMULATOR
#define ftell(f) f_tell(&f)
#define fseek(f,o,w) f_lseek(&f,o)
#define fclose(f) f_close(&f)
#define feof(f) f_eof(&f)
#endif // SIMULATOR
// ============================================================================
//
// Inline functions for simple stuff
//
// ============================================================================
inline bool file::valid()
// ----------------------------------------------------------------------------
// Return true if the input file is OK
// ----------------------------------------------------------------------------
{
#if SIMULATOR
return data != 0;
#else
return data.flag && !data.err;
#endif
}
inline void file::seek(uint off)
// ----------------------------------------------------------------------------
// Move the read position in the data file
// ----------------------------------------------------------------------------
{
fseek(data, off, SEEK_SET);
}
inline unicode file::peek()
// ----------------------------------------------------------------------------
// Look at what is as current position without moving it
// ----------------------------------------------------------------------------
{
uint off = ftell(data);
unicode result = get();
seek(off);
return result;
}
inline unicode file::get(uint off)
// ----------------------------------------------------------------------------
// Get code point at given offset
// ----------------------------------------------------------------------------
{
seek(off);
return get();
}
inline uint file::position()
// ----------------------------------------------------------------------------
// Return current position in help file
// ----------------------------------------------------------------------------
{
return ftell(data);
}
inline bool file::eof()
// ----------------------------------------------------------------------------
// Indicate if end of file
// ----------------------------------------------------------------------------
{
return feof(data);
}
#endif // FILE_H