forked from WartyMN/F256-FileManager
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgeneral.c
More file actions
1160 lines (908 loc) · 35.6 KB
/
Copy pathgeneral.c
File metadata and controls
1160 lines (908 loc) · 35.6 KB
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* general.c
*
* Created on: Feb 19, 2022
* Author: micahbly
*
* - adapted from my Amiga WB2K project's general.c
*/
// 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
/*****************************************************************************/
/* Includes */
/*****************************************************************************/
// project includes
#include "general.h"
#include "app.h"
#include "folder.h" // only need for the CBM file type definitions
#include "strings.h"
#include "memory.h"
#include "text.h"
#include "sys.h"
// C includes
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
//#include <math.h>
// F256 includes
#include "f256.h"
/*****************************************************************************/
/* Definitions */
/*****************************************************************************/
// ** serial comms related
#define UART_BAUD_DIV_300 5244 // divisor for 300 baud
#define UART_BAUD_DIV_600 2622 // divisor for 600 baud
#define UART_BAUD_DIV_1200 1311 // divisor for 1200 baud
#define UART_BAUD_DIV_1800 874 // divisor for 1800 baud
#define UART_BAUD_DIV_2000 786 // divisor for 2000 baud
#define UART_BAUD_DIV_2400 655 // divisor for 2400 baud
#define UART_BAUD_DIV_3600 437 // divisor for 3600 baud
#define UART_BAUD_DIV_4800 327 // divisor for 4800 baud
#define UART_BAUD_DIV_9600 163 // divisor for 9600 baud
#define UART_BAUD_DIV_19200 81 // divisor for 19200 baud
#define UART_BAUD_DIV_38400 40 // divisor for 38400 baud
#define UART_BAUD_DIV_57600 27 // divisor for 57600 baud
#define UART_BAUD_DIV_115200 13 // divisor for 115200 baud
#define UART_DATA_BITS 0b00000011 // 8 bits
#define UART_STOP_BITS 0 // 1 stop bit
#define UART_PARITY 0 // no parity
#define UART_BRK_SIG 0b01000000
#define UART_NO_BRK_SIG 0b00000000
#define UART_DLAB_MASK 0b10000000
#define UART_THR_IS_EMPTY 0b00100000
#define UART_THR_EMPTY_IDLE 0b01000000
#define UART_DATA_AVAILABLE 0b00000001
#define UART_ERROR_MASK 0b10011110
#define UART_MAX_SEND_ATTEMPTS 1000
/*****************************************************************************/
/* File-scoped Variables */
/*****************************************************************************/
//static uint8_t general_string_merge_buff_192b[192];
/*****************************************************************************/
/* Global Variables */
/*****************************************************************************/
char* global_string[NUM_STRINGS];
extern char* global_string_buff1;
extern char* global_string_buff2;
extern uint8_t zp_bank_num;
#pragma zpsym ("zp_bank_num");
// logging related: only do if debugging is active
#if defined LOG_LEVEL_1 || defined LOG_LEVEL_2 || defined LOG_LEVEL_3 || defined LOG_LEVEL_4 || defined LOG_LEVEL_5
static char debug_buffer[256]; // create once, use for every debug and logging function
static const char* kDebugFlag[5] = {
"[ERROR]",
"[WARNING]",
"[INFO]",
"[DEBUG]",
"[ALLOC]"
};
#ifndef USE_SERIAL_LOGGING
//static FILE* global_log_file;
static int16_t global_log_file_handle;
#endif
#endif
/*****************************************************************************/
/* Private Function Prototypes */
/*****************************************************************************/
/*****************************************************************************/
/* Private Function Definitions */
/*****************************************************************************/
/*****************************************************************************/
/* Public Function Definitions */
/*****************************************************************************/
// **** 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)
// {
// if (the_float < 0.0)
// return (int)(the_float - 0.5);
// else
// return (int)(the_float + 0.5);
// }
// note: disabled. see this cc65 error:
// Fatal: Floating point type is currently unsupported
// **** 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)
// {
// //double final_size; // no float in cc65!
// uint32_t final_size;
//
// // convert to float before doing any operations on it, to prevent integer weirdness
// //final_size = (double)size_in_bytes;
// final_size = size_in_bytes;
//
// if (size_in_bytes < 1024) // 1.0k
// {
// // show size in bytes, precisely
// sprintf((char*)formatted_file_size, "%lu b", size_in_bytes);
// }
// else if (size_in_bytes < 10240) // 10k
// {
// // show size in .1k chunks (eg, 9.4k)
// final_size /= 1024;
// sprintf((char*)formatted_file_size, "%.1f kb", final_size);
// }
// else if (size_in_bytes < 1048576) // 1 MB
// {
// // show size in 1k chunks
// final_size /= 1024;
// //size_in_bytes = General_Round(final_size);
// size_in_bytes = final_size /= 1024;
// sprintf((char*)formatted_file_size, "%lu kb", size_in_bytes);
// }
// else if (size_in_bytes < 10485760) // 10MB
// {
// // show size in .1M chunks (eg, 1.4MB)
// final_size /= 1048576;
// sprintf((char*)formatted_file_size, "%.1f Mb", final_size);
// }
// else
// {
// // show size in 1M chunks (eg, 1536 MB)
// final_size /= 1048576;
// //size_in_bytes = General_Round(final_size);
// size_in_bytes = final_size;
// sprintf((char*)formatted_file_size, "%lu Mb", size_in_bytes);
// }
//
// return;
// }
// **** MISC STRING UTILITIES *****
// retrieves a string from extended memory and stashes in the first page of STORAGE_DOS_BOOT_BUFFER for the calling program to retrieve
// returns a pointer to the string (pointer will always be to STORAGE_DOS_BOOT_BUFFER)
char* General_GetString(uint8_t the_string_id)
{
uint8_t old_bank_under_io;
// Disable the I/O page so we can get to RAM under it
asm("SEI"); // disable interrupts in case some other process has a role here
Sys_DisableIOBank();
asm("SEI"); // disable interrupts in case some other process has a role here
// map the string bank into CPU memory space
zp_bank_num = STRING_STORAGE_VALUE;
old_bank_under_io = Memory_SwapInNewBank(BANK_IO);
// copy the string to buffer in MAIN space (we'll copy a whole page, because cheaper than checking len of string (??)
//DEBUG_OUT(("%s %d: str id=%u, global_string[id]=%p, STORAGE_GETSTRING_BUFFER=%p", __func__, __LINE__, the_string_id, global_string[the_string_id], (char*)STORAGE_GETSTRING_BUFFER));
strcpy((char*)STORAGE_GETSTRING_BUFFER, global_string[the_string_id]);
Memory_RestorePreviousBank(BANK_IO);
asm("CLI"); // restore interrupts
// Re-enable the I/O page, which unmaps the string bank from 6502 RAM space
Sys_RestoreIOPage();
return (char*)STORAGE_GETSTRING_BUFFER;
}
// //! 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)
// {
// int16_t i;
// int16_t len = strlen(the_string);
// bool change_made = false;
//
// for (i = 0; i < len; i++)
// {
// char this_char;
//
// this_char = the_string[i];
// the_string[i] = General_ToLower(the_string[i]);
//
// if (this_char != the_string[i])
// {
// change_made = true;
// }
// }
//
// return change_made;
// }
//! 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)
{
char lowered_value;
lowered_value = (the_char >='A' && the_char<='Z') ? (the_char + 32) : (the_char);
return lowered_value;
}
//! 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)
{
char* dst;
size_t alloc_len;
if (max_len < 1)
{
return NULL;
}
alloc_len = General_Strnlen(src, max_len) + 1;
if ( (dst = (char*)calloc(alloc_len, sizeof(char)) ) == NULL)
{
return NULL;
}
else
{
General_Strlcpy(dst, src, max_len);
}
//LOG_ALLOC(("%s %d: __ALLOC__ dst %p size %i, string='%s'", __func__ , __LINE__, dst, General_Strnlen(src, max_len) + 1, dst));
return dst;
}
//! 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)
{
const signed long src_len = strlen(src);
if (max_len < 1)
{
return -1;
}
if (src_len + 1 < max_len)
{
memcpy(dst, src, src_len + 1);
}
else
{
memcpy(dst, src, max_len - 1);
dst[max_len - 1] = '\0';
}
return src_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)
{
const signed long src_len = strlen(src);
const signed long dst_len = General_Strnlen(dst, max_len);
if (max_len > 0)
{
if (dst_len == max_len)
{
return max_len + src_len;
}
if (src_len < max_len - dst_len)
{
memcpy(dst + dst_len, src, src_len + 1);
}
else
{
memcpy(dst + dst_len, src, max_len - dst_len - 1);
dst[max_len - 1] = '\0';
}
}
return dst_len + src_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 max_len)
// {
// register uint8_t u;
//
// do ; while( (u = (uint8_t)*string_1++) && (u == (uint8_t)*string_2++) && --max_len );
//
// if (u)
// {
// string_2--;
// }
//
// return (u - (uint8_t)*string_2);
// }
//! 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)
{
uint8_t u1;
uint8_t u2;
//DEBUG_OUT(("%s %d: s1='%s'; s2='%s'; max_len=%i", __func__ , __LINE__, string_1, string_2, max_len));
for (; max_len != 0; max_len--, string_1++, string_2++)
{
u1 = (uint8_t)*string_1;
u2 = (uint8_t)*string_2;
if (General_ToLower(u1) != General_ToLower(u2))
{
return General_ToLower(u1) - General_ToLower(u2);
}
if (u1 == '\0')
{
return 0;
}
}
return 0;
}
//! 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)
{
signed long len;
for (len = 0; len < max_len; len++, the_string++)
{
if (!*the_string)
{
break;
}
}
return (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)
// {
// // LOGIC:
// // PathPart includes the : if non-name part is for a volume. but doesn't not include trailing / if not a volume
// // we want in include the trailing : and /, so calling routine can always just append a file name and get a legit path
//
// signed long path_len;
// char* the_directory_path;
//
// // get a string for the directory portion of the filepath
// if ( (the_directory_path = (char*)calloc(FILE_MAX_PATHNAME_SIZE, sizeof(char)) ) == NULL)
// {
// LOG_ERR(("%s %d: could not allocate memory for the directory path", __func__ , __LINE__));
// return NULL;
// }
//
// path_len = (General_PathPart(the_file_path) - the_file_path) - 1;
//
// //DEBUG_OUT(("%s %d: pathlen=%lu; last char='%c'", __func__ , __LINE__, path_len, the_file_path[path_len]));
//
// if (the_file_path[path_len] != ':')
// {
// // path wasn't to root of a volume, move 1 tick to the right to pick up the / that is already in the full path
// path_len++;
// }
//
// path_len++;
//
// General_Strlcpy(the_directory_path, the_file_path, path_len + 1);
// //DEBUG_OUT(("%s %d: pathlen=%lu; parent path='%s'", __func__ , __LINE__, path_len, the_directory_path));
//
// return the_directory_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)
// {
// char* the_file_name;
//
// // get a string for the file name portion of the filepath
// if ( (the_file_name = (char*)calloc(FILE_MAX_PATHNAME_SIZE, sizeof(char)) ) == NULL)
// {
// LOG_ERR(("%s %d: could not allocate memory for the filename", __func__ , __LINE__));
// return NULL;
// }
// else
// {
// char* the_file_name_part = General_NamePart(the_file_path);
// int16_t filename_len = General_Strnlen(the_file_name_part, FILE_MAX_PATHNAME_SIZE);
//
// if (filename_len == 0)
// {
// // FilePart() might return a string with no text: that would indicate the file path is for the root of a file system or virtual device
// // in that case, we just use the file path minus : as the name
//
// // copy the part of the path minus the last char into the file name
// int16_t path_len = General_Strnlen(the_file_path, FILE_MAX_PATHNAME_SIZE);
// General_Strlcpy(the_file_name, the_file_path, path_len);
// }
// else
// {
// General_Strlcpy(the_file_name, the_file_name_part, filename_len + 1);
// }
// LOG_ALLOC(("%s %d: __ALLOC__ the_file_name %p size %i", __func__ , __LINE__, the_file_name, FILE_MAX_PATHNAME_SIZE));
// }
//
// return the_file_name;
// }
// populates the passed string by safely combining the passed file path and name, accounting for cases where path is a disk root
// also accounts for filename '..' which means the parent folder.
void General_CreateFilePathFromFolderAndFile(char* the_combined_path, char* the_folder_path, char* the_file_name)
{
uint16_t path_len;
General_Strlcpy(the_combined_path, the_folder_path, FILE_MAX_PATHNAME_SIZE);
// if the filename passed was empty, just return the original folder path.
// otherwise you end up with "mypath" and file "" = "mypath/", which is bad.
if (the_file_name[0] == '\0')
{
return;
}
if (the_file_name[0] == '.' && the_file_name[1] == '.' && the_file_name[2] == 0)
{
// this is a link to the "parent directory", so instead of adding the name to the parent path, in fact go back and snip off the last part of path
path_len = General_PathPart(the_combined_path) - the_combined_path;
the_combined_path[path_len] = '\0';
if (General_Strnlen(the_combined_path, FILE_MAX_PATHNAME_SIZE) == 1)
{
// parent was the root (0:, 1:, or 2:), so we snipped it down too far, to just "0", "1", etc.
the_combined_path[path_len++] = ':';
the_combined_path[path_len] = '\0';
}
return;
}
path_len = General_Strnlen(the_combined_path, FILE_MAX_PATHNAME_SIZE);
if (path_len == 2)
{
// this is a disk root, so path ends in : ("0:", "1:", etc.)
// no need to insert a separator
}
else
{
// this is at least one level deep, so need to add a path divider
the_combined_path[path_len++] = '/';
the_combined_path[path_len] = 0;
}
General_Strlcat(the_combined_path, the_file_name, FILE_MAX_PATHNAME_SIZE);
//DEBUG_OUT(("%s %d: file '%s' and folder '%s' produces path of '%s'", __func__ , __LINE__, the_file_name, the_folder_path, the_combined_path));
}
// // 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)
// {
// char* last_slash;
//
// last_slash = strchr(the_file_path, '/');
//
// if (last_slash && ++last_slash)
// {
// return last_slash;
// }
//
// return (char*)the_file_path;
// }
// return everything to the left of the filename in a path.
// amigaDOS compatibility function
char* General_PathPart(const char* the_file_path)
{
char* the_directory_path;
char* this_point;
this_point = (char*)the_file_path;
the_directory_path = this_point; // default to returning start of the string
while (*this_point)
{
if (*this_point == '/' || *this_point == ':')
{
the_directory_path = this_point;
}
this_point++;
}
return the_directory_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)
{
// LOGIC:
// if the first char is the first dot from right, we'll count the whole thing as an extension
// if no dot char, then don't set extension, and return false
char* dot = strrchr((char*)the_file_name, '.');
int16_t i;
// (re) set the file extension to "" in case we have to return. It may have a value from whatever previous use was
the_extension[0] = '\0';
if(!dot)
{
return false;
}
for (i = 1; dot[i]; i++)
{
the_extension[i-1] = General_ToLower(dot[i]);
}
the_extension[i-1] = '\0';
return true;
}
// 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)
{
switch (cbm_filetype_id)
{
// case _CBM_T_HEADER:
// return General_GetString(ID_STR_FILETYPE_DIR);
//
// case _CBM_T_SEQ:
// return General_GetString(ID_STR_FILETYPE_SEQ);
//
// case _CBM_T_PRG:
// return General_GetString(ID_STR_FILETYPE_PRG);
//
// case _CBM_T_REL:
// return General_GetString(ID_STR_FILETYPE_REL);
case FNX_FILETYPE_BASIC:
// any file ending in .bas
return General_GetString(ID_STR_FILETYPE_BASIC);
case FNX_FILETYPE_FONT:
// any 2k file ending in .fnt
return General_GetString(ID_STR_FILETYPE_FONT);
case FNX_FILETYPE_EXE:
// any .pgz, etc executable
return General_GetString(ID_STR_FILETYPE_EXE);
case FNX_FILETYPE_IMAGE:
// any .256, .lbm, etc, image file
return General_GetString(ID_STR_FILETYPE_IMAGE);
case FNX_FILETYPE_MUSIC:
// any .mod etc music file that modo can play
return General_GetString(ID_STR_FILETYPE_MUSIC);
//
// case _CBM_T_DEL:
case _CBM_T_DIR:
return General_GetString(ID_STR_FILETYPE_DIR);
default:
return General_GetString(ID_STR_FILETYPE_OTHER);
}
// #define _CBM_T_REG 0x10U /* Bit set for regular files */
// #define _CBM_T_SEQ 0x10U
// #define _CBM_T_PRG 0x11U
// #define _CBM_T_USR 0x12U
// #define _CBM_T_REL 0x13U
// #define _CBM_T_VRP 0x14U /* Vorpal fast-loadable format */
// #define _CBM_T_DEL 0x00U
// #define _CBM_T_CBM 0x01U /* 1581 sub-partition */
// #define _CBM_T_DIR 0x02U /* IDE64 and CMD sub-directory */
// #define _CBM_T_LNK 0x03U /* IDE64 soft-link */
// #define _CBM_T_OTHER 0x04U /* File-type not recognized */
// #define _CBM_T_HEADER 0x05U /* Disk header / title */
}
// **** 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)
{
uint16_t i;
uint16_t j;
for (i = 0; i < ticks; i++)
{
for (j = 0; j < 5; j++);
}
}
// //! 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)
// {
// long start_ticks = sys_time_jiffies();
// long now_ticks = start_ticks;
//
// while ((now_ticks - start_ticks) / SYS_TICKS_PER_SEC < seconds)
// {
// now_ticks = sys_time_jiffies();
// }
// }
// **** USER INPUT UTILITIES *****
// // Wait for one character from the keyboard and return it
// char General_GetChar(void)
// {
// uint8_t the_char;
//
// #ifdef _C256_FMX_
// char (*Kernal_GetCharWithWait)(void);
// //GETCHW $00:104C Get a character from the input channel. Waits until data received. A=0 and Carry=1 if no data is waiting
// Kernal_GetCharWithWait = (void*)(0x00104c);
//
// the_char = Kernal_GetCharWithWait();
// #else
// the_char = Keyboard_GetChar();
// #endif
//
// return the_char;
// }
// **** 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)
// {
// // LOGIC
// // we only have 80x60 to work with, and we need a row for "hit space for more, esc to stop"
// // so 60 rows * 16 bytes = 960 max bytes can be shown
// // we only need one buffer as we read and print to screen line by line (80 bytes)
// // we need to keep the file stream open until it is used up, or user exits loop
//
// uint8_t y;
// uint8_t cut_off_pos;
// uint16_t num_bytes_to_read = MEM_DUMP_BYTES_PER_ROW;
// uint8_t* loc_in_file = 0x000; // will track the location within the file, so we can show to users on left side.
//
// cut_off_pos = MEM_DUMP_BYTES_PER_ROW * 3; // each char represented by 2 hex digits and a space
// y = 0;
// Text_ClearScreen(FILE_CONTENTS_FOREGROUND_COLOR, FILE_CONTENTS_BACKGROUND_COLOR);
// sprintf(global_string_buff1, General_GetString(ID_STR_MSG_HEX_VIEW_INSTRUCTIONS), "Memory Dump");
// Text_DrawStringAtXY(0, y++, global_string_buff1, FILE_CONTENTS_ACCENT_COLOR, FILE_CONTENTS_BACKGROUND_COLOR);
//
// // loop until all screen rows used
// do
// {
//
// sprintf(global_string_buff2, "%p: ", the_buffer);
// Text_DrawStringAtXY(0, y, global_string_buff2, FILE_CONTENTS_ACCENT_COLOR, FILE_CONTENTS_BACKGROUND_COLOR);
//
// sprintf(global_string_buff2, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x ",
// the_buffer[0], the_buffer[1], the_buffer[2], the_buffer[3], the_buffer[4], the_buffer[5], the_buffer[6], the_buffer[7],
// the_buffer[8], the_buffer[9], the_buffer[10], the_buffer[11], the_buffer[12], the_buffer[13], the_buffer[14], the_buffer[15]);
//
// // cut off the string
// global_string_buff2[cut_off_pos] = '\0';
//
// Text_DrawStringAtXY(MEM_DUMP_START_X_FOR_HEX, y, global_string_buff2, FILE_CONTENTS_FOREGROUND_COLOR, FILE_CONTENTS_BACKGROUND_COLOR);
//
// // render chars with char draw function to avoid problem of 0s getting treated as nulls in sprintf
// Text_DrawCharsAtXY(MEM_DUMP_START_X_FOR_CHAR, y, (uint8_t*)the_buffer, MEM_DUMP_BYTES_PER_ROW);
//
// the_buffer += MEM_DUMP_BYTES_PER_ROW;
// ++y;
//
// } while (y < MAX_TEXT_VIEW_ROWS_PER_PAGE);
// }
// **** LOGGING AND DEBUG UTILITIES *****
#if defined LOG_LEVEL_1 || defined LOG_LEVEL_2 || defined LOG_LEVEL_3 || defined LOG_LEVEL_4 || defined LOG_LEVEL_5
#if defined USE_SERIAL_LOGGING
// LOGIC FOR SERIAL LOGGING:
// we will use the baud speed defined in by macro in build script. 81N.
// we will NOT check if the other side is receiving, we just blast away
// no READING of the serial port happens. just writing to it.
// UART setup is based on mgr42's dcopy project, the uart.asm file.
// linux/mac setup for using 'screen' command as terminal: screen /dev/tty.usbserial-FT53JP031 300,cs8,-ixon,-ixoff,-istrip,-parenb
// set UART chip to DLAB mode
void Serial_SetDLAB(void);
// turn off DLAB mode on UART chip
void Serial_ClearDLAB(void);
// set up UART for serial comms
void Serial_InitUART(void);
// send 1-255 bytes to the UART serial connection
// returns # of bytes successfully sent (which may be less than number requested, in event of error, etc.)
uint8_t Serial_SendData(uint8_t* the_buffer, uint16_t buffer_size);
// send a byte over the UART serial connection
// if the UART send buffer does not have space for the byte, it will try for UART_MAX_SEND_ATTEMPTS then return an error
// returns false on any error condition
bool Serial_SendByte(uint8_t the_byte);
// send 1-255 bytes to the UART serial connection
// returns # of bytes successfully sent (which may be less than number requested, in event of error, etc.)
uint8_t Serial_SendData(uint8_t* the_buffer, uint16_t buffer_size);
// set UART chip to DLAB mode
void Serial_SetDLAB(void)
{
Sys_SwapIOPage(VICKY_IO_PAGE_REGISTERS);
R8(UART_LCR) = R8(UART_LCR) | UART_DLAB_MASK;
Sys_RestoreIOPage();
}
// turn off DLAB mode on UART chip
void Serial_ClearDLAB(void)
{
Sys_SwapIOPage(VICKY_IO_PAGE_REGISTERS);
R8(UART_LCR) = R8(UART_LCR) & (~UART_DLAB_MASK);
Sys_RestoreIOPage();
}
// set up UART for serial comms
void Serial_InitUART(void)
{
Sys_SwapIOPage(VICKY_IO_PAGE_REGISTERS);
R8(UART_LCR) = UART_DATA_BITS | UART_STOP_BITS | UART_PARITY | UART_NO_BRK_SIG;
Serial_SetDLAB();
R16(UART_DLL) = UART_BAUD_DIV_57600;
Serial_ClearDLAB();
Sys_RestoreIOPage();
}
// send a byte over the UART serial connection
// if the UART send buffer does not have space for the byte, it will try for UART_MAX_SEND_ATTEMPTS then return an error
// returns false on any error condition
bool Serial_SendByte(uint8_t the_byte)
{
uint8_t error_check;
bool uart_in_buff_is_empty = false;
uint16_t num_tries = 0;
Sys_SwapIOPage(VICKY_IO_PAGE_REGISTERS);
error_check = R8(UART_LSR) & UART_ERROR_MASK;
if (error_check > 0)
{
goto error;
}
while (uart_in_buff_is_empty == false && num_tries < UART_MAX_SEND_ATTEMPTS)
{
uart_in_buff_is_empty = R8(UART_LSR) & UART_THR_IS_EMPTY;
++num_tries;
};
if (uart_in_buff_is_empty == true)
{
goto error;
}
R8(UART_THR) = the_byte;
Sys_RestoreIOPage();
return true;
error:
Sys_RestoreIOPage();
return false;
}
// send 1-255 bytes to the UART serial connection
// returns # of bytes successfully sent (which may be less than number requested, in event of error, etc.)
uint8_t Serial_SendData(uint8_t* the_buffer, uint16_t buffer_size)
{
uint16_t i;
uint8_t the_byte;
if (buffer_size > 256)
{
return 0;
}
for (i=0; i <= buffer_size; i++)
{
the_byte = the_buffer[i];
if (Serial_SendByte(the_byte) == false)
{
return i;
}
}
// add a line return if we got this far
Serial_SendByte(0x0D);
return i;
}
#endif
// DEBUG functionality I want:
// 3 levels of logging (err/warn/info)
// additional debug out function that leaves no footprint in compiled release version of code (calls to it also disappear)
// able to pass format string and multiple variables when needed
#ifdef LOG_LEVEL_1