Foenix A2650 OS/f Library
 
Loading...
Searching...
No Matches
general.h
Go to the documentation of this file.
1
2
3/*
4 * general.h
5 *
6 * Created on: Feb 19, 2022
7 * Author: micahbly
8 */
9
10// This is a cut-down, semi-API-compatible version of the OS/f general.c file from Lich King (Foenix)
11// adapted for Foenix F256 Jr starting November 29, 2022
12// Adapted for F256jr/k E fpga builds from F256jr/k version of FileManager starting June 2, 2024
13// Debug-related functionality extracted to debug.h/.c July 2024
14// adapted (back-ported) for A2560K starting Dec 26, 2024
15
16
17#ifndef GENERAL_H_
18#define GENERAL_H_
19
20
21/* about this class
22 *
23 *
24 *
25 *** things this class needs to be able to do
26 * various utility functions that any app could find useful.
27 * intended to be re-usable across apps, with at most minimal differences.
28 * this file should contain only cross-platform code. platform-specific code should go into general_[platformname].h/.c
29 *
30 *** things objects of this class have
31 *
32 *
33 */
34
35
36/*****************************************************************************/
37/* Includes */
38/*****************************************************************************/
39
40// project includes
41//#include "text.h"
42
43// C includes
44#include <stdint.h>
45#include <stdbool.h>
46#include <stdlib.h>
47
48// A2560 includes
49#include "a2560k.h"
50
51
52/*****************************************************************************/
53/* Macro Definitions */
54/*****************************************************************************/
55
56
57
58/*****************************************************************************/
59/* Enumerations */
60/*****************************************************************************/
61
62
63
64/*****************************************************************************/
65/* Structs */
66/*****************************************************************************/
67
68typedef struct Coordinate
69{
70 uint8_t x;
71 uint8_t y;
73
74/*****************************************************************************/
75/* Global Variables */
76/*****************************************************************************/
77
78
79/*****************************************************************************/
80/* Public Function Prototypes */
81/*****************************************************************************/
82
83
84
85// **** WORD-WRAP UTILITIES *****
86
87
88
89
90
91// **** MATH UTILITIES *****
92
98int32_t General_Round(double the_float);
99
100
101
102
103// **** NUMBER<>STRING UTILITIES *****
104
105// convert a file size in bytes to a human readable format using "10 bytes", "1.4 kb", "1 MB", etc.
106// NOTE: formatted_file_size string must have been allocated before passing here
107void General_MakeFileSizeReadable(unsigned long size_in_bytes, char* formatted_file_size);
108
109
110
111
112
113
114// **** MISC STRING UTILITIES *****
115
121bool General_StrToLower(char* the_string);
122
127uint8_t General_ToLower(uint8_t the_char);
128
134char* General_StrlcpyWithAlloc(const char* src, size_t max_len);
135
141int16_t General_Strlcpy(char* dst, const char* src, size_t max_len);
142
148int16_t General_Strlcat(char* dst, const char* src, size_t max_len);
149
158int16_t General_Strncmp(const char* string_1, const char* string_2, size_t max_len);
159
169int16_t General_Strncasecmp(const char* string_1, const char* string_2, size_t max_len);
170
173// Inspired by apple/bsd strnlen.
175int16_t General_Strnlen(const char *the_string, size_t max_len);
176
177
178
179
180// **** RECTANGLE UTILITIES *****
181
182
188
189// test if 2 rectangles intersect
190bool General_RectIntersect(struct Rectangle r1, struct Rectangle r2);
191
192// test if a point is within a rectangle
193bool General_PointInRect(int16_t x, int16_t y, Rectangle r);
194
195// Position one rect within the bounds of another. Horizontally: centers the hero rect within the left/right of the frame rect; Vertically: centers or or puts at 25% line
196// put the frame coords into the frame_rect, and the object to be centered into the hero_rect. ON return, the frame rect will hold the coords to be used.
197void General_CenterRectWithinRect(Rectangle* the_frame_rect, Rectangle* the_hero_rect, bool at_25_percent_v);
198
203
217int16_t General_CalculateRectDifference(Rectangle* r1, Rectangle* r2, Rectangle* diff_r1, Rectangle* diff_r2, Rectangle* diff_r3, Rectangle* diff_r4);
218
225
226
227
228
229
230// **** FILENAME AND FILEPATH UTILITIES *****
231
232
233
234// // allocate and return the portion of the path passed, minus the filename. In other words: return a path to the parent file.
235// // calling method must free the string returned
236// char* General_ExtractPathToParentFolderWithAlloc(const char* the_file_path);
237
238// // allocate and return the filename portion of the path passed.
239// // calling method must free the string returned
240// char* General_ExtractFilenameFromPathWithAlloc(const char* the_file_path);
241
242// populates the passed string by safely combining the passed file path and name, accounting for cases where path is a disk root
243void General_CreateFilePathFromFolderAndFile(char* the_combined_path, char* the_folder_path, char* the_file_name);
244
251char* General_NamePart(const char* the_file_path);
252
257char* General_PathPart(const char* the_file_path);
258
263bool General_ExtractFileExtensionFromFilename(const char* the_file_name, char* the_extension);
264
269bool General_ExtractCoreFilename(const char* the_file_name, char* the_core_part);
270
271
272
273
274// **** TIME UTILITIES *****
275
276
279void General_DelayTicks(int32_t ticks);
280
283void General_DelaySeconds(uint16_t seconds);
284
285
286
287// **** USER INPUT UTILITIES *****
288
289// Wait for one character from the keyboard and return it
290char General_GetChar(void);
291
292
293
294
295
296// **** MISC UTILITIES *****
297
298
301uint32_t General_ByteSwapLong(uint32_t long_needing_swap);
302
303// get random number between 1 and the_range + 1
304// must have seeded the number generator first
305// if passed 0, returns 0.
306uint16_t General_GetRandom(uint16_t the_range);
307
308
309
310#endif /* GENERAL_H_ */
int16_t General_Strnlen(const char *the_string, size_t max_len)
Measure the length of a fixed-size string Safe(r) strlen function: will stop processing if no termina...
Definition: general.c:460
bool General_ExtractCoreFilename(const char *the_file_name, char *the_core_part)
Extract core part of the file name, not including the extension.
Definition: general.c:939
bool General_CalculateRectIntersection(Rectangle *r1, Rectangle *r2, Rectangle *intersect_r)
Calculate the intersection between 2 rectangles, storing result in the 3rd rect passed.
Definition: general.c:728
uint8_t General_ToLower(uint8_t the_char)
Change the case of the passed character from upper to lower (if necessary) Scope is limited to charac...
Definition: general.c:296
bool General_RectWithinRect(Rectangle r1, Rectangle r2)
Test if one rectangle is entirely within the bounds of another Rectangle.
Definition: general.c:513
int16_t General_Strlcpy(char *dst, const char *src, size_t max_len)
Copies up to max_len - 1 characters from the NUL-terminated string src to dst, NUL-terminating the re...
Definition: general.c:343
int16_t General_Strncasecmp(const char *string_1, const char *string_2, size_t max_len)
Makes a case insensitive comparison of the specified number of characters of the two passed strings S...
Definition: general.c:430
int32_t General_Round(double the_float)
Round a float to the nearest integer value THINK C's and SAS/C's math.h don't include round() from: h...
Definition: general.c:140
char * General_PathPart(const char *the_file_path)
Returns a pointer to the end of the next-to-last component of a path.
Definition: general.c:881
int16_t General_CalculateRectDifference(Rectangle *r1, Rectangle *r2, Rectangle *diff_r1, Rectangle *diff_r2, Rectangle *diff_r3, Rectangle *diff_r4)
Calculate the difference between 2 rectangles and populate 0, 1, 2, 3, or 4 new rectangles with the d...
Definition: general.c:635
void General_DelayTicks(int32_t ticks)
Wait for the specified number of ticks before returning In multi-tasking ever becomes a thing,...
Definition: general.c:971
char * General_StrlcpyWithAlloc(const char *src, size_t max_len)
Allocates memory for a new string and copies up to max_len - 1 characters from the NUL-terminated str...
Definition: general.c:311
int16_t General_Strncmp(const char *string_1, const char *string_2, size_t max_len)
Makes a case sensitive comparison of the specified number of characters of the two passed strings Sto...
Definition: general.c:406
bool General_ExtractFileExtensionFromFilename(const char *the_file_name, char *the_extension)
Extract file extension into the passed char pointer, as new lowercased string pointer,...
Definition: general.c:907
bool General_StrToLower(char *the_string)
Convert a string, in place, to lower case This overwrites the string with a lower case version of its...
Definition: general.c:269
uint32_t General_ByteSwapLong(uint32_t long_needing_swap)
Swap bytes big endian <> little endian, for a long NOTE: for a word, see BSWAP() macro.
Definition: general.c:1024
int16_t General_Strlcat(char *dst, const char *src, size_t max_len)
Copies up to max_len - 1 characters from the NUL-terminated string src and appends to the end of dst,...
Definition: general.c:371
void General_DelaySeconds(uint16_t seconds)
Wait for the specified number of seconds before returning In multi-tasking ever becomes a thing,...
Definition: general.c:985
void General_CopyRect(Rectangle *r1, Rectangle *r2)
Copy values of one rect to another.
Definition: general.c:616
char * General_NamePart(const char *the_file_path)
return the first char of the last part of a file path if no path part detected, returns the original ...
Definition: general.c:862
Definition: general.h:69
Definition: a2560k.h:1369