forked from WartyMN/F256-FileManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneral.h
317 lines (215 loc) · 12.6 KB
/
general.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//! @file general.h
/*
* general.h
*
* Created on: Feb 19, 2022
* Author: micahbly
*/
// This is a cut-down, semi-API-compatible version of the OS/f general.c file from Lich King (Foenix)
// adapted for Foenix F256 Jr starting November 29, 2022
#ifndef GENERAL_H_
#define GENERAL_H_
/* about this class
*
*
*
*** things this class needs to be able to do
* various utility functions that any app could find useful.
* intended to be re-usable across apps, with at most minimal differences.
* this file should contain only cross-platform code. platform-specific code should go into general_[platformname].h/.c
*
*** things objects of this class have
*
*
*/
/*****************************************************************************/
/* Includes */
/*****************************************************************************/
// project includes
//#include "text.h"
// C includes
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
// cc65 includes
/*****************************************************************************/
/* Macro Definitions */
/*****************************************************************************/
#define CROSS_BANK_COPY_BUFFER_LEN 256 // 1 page, 256b is available for cross-screen copying
// replacement for sys jiffies
#define sys_time_jiffies() *(uint8_t*)0x8d * 65536 + *(uint8_t*)0x8e * 256 + *(uint8_t*)0x8f
// general
#define MAX_STRING_COMP_LEN 192 //!< 255 + terminator is max string size for compares
// word-wrap and string measurement related
#define GEN_NO_STRLEN_CAP -1 //!< for the xxx_DrawString function's max_chars parameter, the value that corresponds to 'draw the entire string if it fits, do not cap it at n characters'
#define WORD_WRAP_MAX_LEN 192 //!< For the xxx_DrawStringInBox function, the strnlen char limit. 40*25.
#ifdef LOG_LEVEL_1
#define LOG_ERR(x) General_LogError x
#else
#define LOG_ERR(x)
#endif
#ifdef LOG_LEVEL_2
#define LOG_WARN(x) General_LogWarning x
#else
#define LOG_WARN(x)
#endif
#ifdef LOG_LEVEL_3
#define LOG_INFO(x) General_LogInfo x
#else
#define LOG_INFO(x)
#endif
#ifdef LOG_LEVEL_4
#define DEBUG_OUT(x) General_DebugOut x
#else
#define DEBUG_OUT(x)
#endif
#ifdef LOG_LEVEL_5
#define LOG_ALLOC(x) General_LogAlloc x
#else
#define LOG_ALLOC(x)
#endif
/*****************************************************************************/
/* Enumerations */
/*****************************************************************************/
#define LogError 0
#define LogWarning 1
#define LogInfo 2
#define LogDebug 3
#define LogAlloc 4
/*****************************************************************************/
/* Structs */
/*****************************************************************************/
typedef struct Coordinate
{
uint8_t x;
uint8_t y;
} Coordinate;
typedef struct Rectangle
{
uint8_t MinX, MinY;
uint8_t MaxX, MaxY;
} Rectangle;
/*****************************************************************************/
/* Global Variables */
/*****************************************************************************/
/*****************************************************************************/
/* Public Function Prototypes */
/*****************************************************************************/
// **** WORD-WRAP UTILITIES *****
// **** MATH UTILITIES *****
//! Round a float to the nearest integer value
//! THINK C's and SAS/C's math.h don't include round()
//! from: https://stackoverflow.com/questions/4572556/concise-way-to-implement-round-in-c
//! @param the_float: a double value to round up/down
//! @return Returns an int with the rounded value
int32_t General_Round(double the_float);
// **** NUMBER<>STRING UTILITIES *****
// // convert a file size in bytes to a human readable format using "10 bytes", "1.4 kb", "1 MB", etc.
// // NOTE: formatted_file_size string must have been allocated before passing here
// void General_MakeFileSizeReadable(unsigned long size_in_bytes, char* formatted_file_size);
// **** MISC STRING UTILITIES *****
// return the global string for the passed ID
// this is just a wrapper around the string, to make it easier to re-use and diff code in different overlays
char* General_GetString(uint8_t the_string_id);
// //! Convert a string, in place, to lower case
// //! This overwrites the string with a lower case version of itself.
// //! Warning: no length check is in place. Calling function must verify string is well-formed (terminated).
// //! @param the_string: the string to convert to lower case.
// //! @return Returns true if the string was modified by the process.
// bool General_StrToLower(char* the_string);
//! Change the case of the passed character from upper to lower (if necessary)
//! Scope is limited to characters A-Z, ascii.
//! replacement for tolower() in c library, which doesn't seem to work [in Amiga WB2K] for some reason.
//! @return a character containing the lowercase version of the passed character.
char General_ToLower(char the_char);
//! Allocates memory for a new string and copies up to max_len - 1 characters from the NUL-terminated string src to the new string, NUL-terminating the result
//! This is meant to be a one stop shop for getting a copy of a string
//! @param src: The string to copy
//! @param max_len: The maximum number of bytes to use in the destination string, including the terminator. If this is shorter than the length of the source string + 1, the resulting copy string will be capped at max_len - 1.
//! @return a copy of the source string to max_len, or NULL on any error condition
char* General_StrlcpyWithAlloc(const char* src, signed long max_len);
//! Copies up to max_len - 1 characters from the NUL-terminated string src to dst, NUL-terminating the result
//! @param src: The string to copy
//! @param dst: The string to copy into. Calling function is responsible for ensuring this string is allocated, and has at least as much storage as max_len.
//! @param max_len: The maximum number of bytes to use in the destination string, including the terminator. If this is shorter than the length of the source string + 1, the resulting copy string will be capped at max_len - 1.
//! @return Returns the length of the source string, or -1 on any error condition
signed long General_Strlcpy(char* dst, const char* src, signed long max_len);
//! Copies up to max_len - 1 characters from the NUL-terminated string src and appends to the end of dst, NUL-terminating the result
//! @param src: The string to copy
//! @param dst: The string to append to. Calling function is responsible for ensuring this string is allocated, and has at least as much storage as max_len.
//! @param max_len: The maximum number of bytes to use in the destination string, including the terminator. If this is shorter than the length of src + length of dst + 1, the resulting copy string will be capped at max_len - 1.
//! @return Returns the length of the attempted concatenated string: initial length of dst plus the length of src.
signed long General_Strlcat(char* dst, const char* src, signed long max_len);
//! Makes a case sensitive comparison of the specified number of characters of the two passed strings
//! Stops processing once max_len has been reached, or when one of the two strings has run out of characters.
//! http://home.snafu.de/kdschem/c.dir/strings.dir/strncmp.c
//! TODO: compare this to other implementations, see which is faster. eg, https://opensource.apple.com/source/Libc/Libc-167/gen.subproj/i386.subproj/strncmp.c.auto.html
//! @param string_1: the first string to compare.
//! @param string_2: the second string to compare.
//! @param max_len: the maximum number of characters to compare. Even if both strings are larger than this number, only this many characters will be compared.
//! @return Returns 0 if the strings are equivalent (at least up to max_len). Returns a negative or positive if the strings are different.
int16_t General_Strncmp(const char* string_1, const char* string_2, size_t length);
//! Makes a case insensitive comparison of the specified number of characters of the two passed strings
//! Stops processing once max_len has been reached, or when one of the two strings has run out of characters.
//! Inspired by code from slashdot and apple open source
//! https://stackoverflow.com/questions/5820810/case-insensitive-string-comparison-in-c
//! https://opensource.apple.com/source/tcl/tcl-10/tcl/compat/strncasecmp.c.auto.html
//! @param string_1: the first string to compare.
//! @param string_2: the second string to compare.
//! @param max_len: the maximum number of characters to compare. Even if both strings are larger than this number, only this many characters will be compared.
//! @return Returns 0 if the strings are equivalent (at least up to max_len). Returns a negative or positive if the strings are different.
int16_t General_Strncasecmp(const char* string_1, const char* string_2, size_t max_len);
//! Measure the length of a fixed-size string
//! Safe(r) strlen function: will stop processing if no terminator found before max_len reached
// Inspired by apple/bsd strnlen.
//! @return Returns strlen(the_string), if that is less than max_len, or max_len if there is no null terminating ('\0') among the first max_len characters pointed to by the_string.
signed long General_Strnlen(const char *the_string, size_t max_len);
// **** RECTANGLE UTILITIES *****
// **** FILENAME AND FILEPATH UTILITIES *****
// // allocate and return the portion of the path passed, minus the filename. In other words: return a path to the parent file.
// // calling method must free the string returned
// char* General_ExtractPathToParentFolderWithAlloc(const char* the_file_path);
// // allocate and return the filename portion of the path passed.
// // calling method must free the string returned
// char* General_ExtractFilenameFromPathWithAlloc(const char* the_file_path);
// populates the passed string by safely combining the passed file path and name, accounting for cases where path is a disk root
void General_CreateFilePathFromFolderAndFile(char* the_combined_path, char* the_folder_path, char* the_file_name);
// // return the first char of the last part of a file path
// // if no path part detected, returns the original string
// // not guaranteed that this is a FILENAME, as if you passed a path to a dir, it would return the DIR name
// // amigaDOS compatibility function (see FilePart)
// char* General_NamePart(const char* the_file_path);
// return everything to the left of the filename in a path.
char* General_PathPart(const char* the_file_path);
//! Extract file extension into the passed char pointer, as new lowercased string pointer, if any found.
//! @param the_file_name: the file name to extract an extension from
//! @param the_extension: a pre-allocated buffer that will contain the extension, if any is detected. Must be large enough to hold the extension! No bounds checking is done.
//! @return Returns false if no file extension found.
bool General_ExtractFileExtensionFromFilename(const char* the_file_name, char* the_extension);
// return a human-readable(ish) string for the filetype of the filetype ID passed - no allocation
// see cbm_filetype.h
char* General_GetFileTypeString(uint8_t cbm_filetype_id);
// **** TIME UTILITIES *****
//! Wait for the specified number of ticks before returning
//! In PET/B128 implementation, we don't bother with real ticks.
void General_DelayTicks(uint16_t ticks);
// //! Wait for the specified number of seconds before returning
// //! In multi-tasking ever becomes a thing, this is not a multi-tasking-friendly operation.
// void General_DelaySeconds(uint16_t seconds);
// **** USER INPUT UTILITIES *****
// // Wait for one character from the keyboard and return it
// char General_GetChar(void);
// **** MISC UTILITIES *****
// // Print out a section of memory in a hex viewer style
// // display length is hard-coded to one screen at 80x59 (MEM_DUMP_BYTES_PER_ROW * MAX_TEXT_VIEW_ROWS_PER_PAGE)
// void General_ViewHexDump(uint8_t* the_buffer);
// **** LOGGING AND DEBUG UTILITIES *****
// ********* logging functionality. requires global_log_file to have been opened.
void General_LogError(const char* format, ...);
void General_LogWarning(const char* format, ...);
void General_LogInfo(const char* format, ...);
void General_DebugOut(const char* format, ...);
void General_LogAlloc(const char* format, ...);
bool General_LogInitialize(void);
void General_LogCleanUp(void);
#endif /* GENERAL_H_ */