forked from WartyMN/F256-FileManager
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfolder.c
More file actions
2229 lines (1941 loc) · 79.8 KB
/
Copy pathfolder.c
File metadata and controls
2229 lines (1941 loc) · 79.8 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
/*
* folder.c
*
* Created on: Nov 21, 2020
* Author: micahbly
*/
/*****************************************************************************/
/* Includes */
/*****************************************************************************/
// project includes
#include "api.h"
#include "app.h"
#include "comm_buffer.h"
#include "file.h"
#include "folder.h"
#include "general.h"
#include "list.h"
#include "list_panel.h"
#include "strings.h"
#include "text.h"
// C includes
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <device.h>
//#include <dirent.h>
#include "dirent.h"
// F256 includes
#include "f256.h"
/*****************************************************************************/
/* Definitions */
/*****************************************************************************/
/*****************************************************************************/
/* File-scoped Variables */
/*****************************************************************************/
static char folder_temp_filename_buffer[FILE_MAX_FILENAME_SIZE];
static char* folder_temp_filename = folder_temp_filename_buffer;
/*****************************************************************************/
/* Global Variables */
/*****************************************************************************/
extern char* global_temp_path_1;
extern char* global_temp_path_2;
extern char* global_string_buff1;
extern char* global_string_buff2;
/*****************************************************************************/
/* Private Function Prototypes */
/*****************************************************************************/
// // looks through all files in the file list, comparing the passed file object, and turning true if found in the list
// // use case: checking if a given file in a selection pool is also the potential target for a drag action
// bool Folder_InFileList(WB2KFolderObject* the_folder, WB2KFileObject* the_file, uint8_t the_scope);
// looks through all files in the file list, comparing the passed string to the filename of each file.
// Returns NULL if nothing matches, or returns pointer to first matching list item
WB2KList* Folder_FindListItemByFileName(WB2KFolderObject* the_folder, char* the_file_name);
// looks through all files in the file list, comparing the passed string to the filepath of each file.
// Returns NULL if nothing matches, or returns pointer to first matching list item
WB2KList* Folder_FindListItemByFilePath(WB2KFolderObject* the_folder, char* the_file_path, short the_compare_len);
// looks through all files in the file list, comparing the passed string to the filepath of each file.
// Returns NULL if nothing matches, or returns pointer to first matching FileObject
WB2KFileObject* Folder_FindFileByFilePath(WB2KFolderObject* the_folder, char* the_file_path, short the_compare_len);
// copy file bytes. Returns number of bytes copied, or -1 in event of any error
int32_t Folder_CopyFileBytes(const char* the_source_file_path, const char* the_target_file_path, int32_t expected_bytes);
/*****************************************************************************/
/* Private Function Definitions */
/*****************************************************************************/
// copy file bytes. Returns number of bytes copied, or -1 in event of any error
int32_t Folder_CopyFileBytes(const char* the_source_file_path, const char* the_target_file_path, int32_t expected_bytes)
{
FILE* the_source_handle;
FILE* the_target_handle;
uint8_t* the_buffer = (uint8_t*)STORAGE_FILE_BUFFER_1;
int16_t bytes_read = 0;
int16_t total_bytes_read = 0;
uint32_t percent_read = 0;
bool keep_going = true;
// prepare to use progress bar
App_ShowProgressBar();
// Open source file for Reading
the_source_handle = fopen(the_source_file_path, "r");
if (the_source_handle == NULL)
{
//sprintf(global_string_buff1, "source file '%s' could not be opened", the_source_file_path);
//Buffer_NewMessage(global_string_buff1);
//LOG_ERR(("%s %d: file '%s' could not be opened for reading", __func__ , __LINE__, the_source_file_path));
goto error;
}
else
{
// Get a target file handle for Writing
the_target_handle = Folder_GetTargetHandleForWriting(the_target_file_path);
if (the_target_handle == NULL)
{
LOG_ERR(("%s %d: file '%s' could not be opened for writing", __func__ , __LINE__, the_target_file_path));
goto error;
}
// loop until source file EOF, writing STORAGE_FILE_BUFFER_1_LEN bytes per loop (sized to available buffer)
do
{
bytes_read = fread(the_buffer, 1, STORAGE_FILE_BUFFER_1_LEN, the_source_handle);
if ( bytes_read < 0)
{
//Buffer_NewMessage("bytes_read < 0");
//LOG_ERR(("%s %d: reading file '%s' resulted in error %i", __func__ , __LINE__, the_source_file_path, bytes_read));
goto error;
}
if ( bytes_read == 0)
{
//Buffer_NewMessage("bytes_read == 0 (end of file)");
//LOG_ERR(("%s %d: reading file '%s' produced 0 bytes", __func__ , __LINE__, the_source_file_path));
keep_going = false;
}
if ( bytes_read < STORAGE_FILE_BUFFER_1_LEN )
{
// we hit end of file
//Buffer_NewMessage("s_bytes_read_from_disk was less than full row");
//LOG_ERR(("%s %d: reading file '%s' expected %u bytes, got %i bytes", __func__ , __LINE__, the_file->file_name_, num_bytes_to_read, s_bytes_read_from_disk));
keep_going = false;
}
fwrite(the_buffer, 1, bytes_read, the_target_handle);
total_bytes_read += bytes_read;
percent_read = (uint32_t)total_bytes_read; // REALLY don't want to do math with signed ints
percent_read = (percent_read * 100) / (uint32_t)expected_bytes;
//sprintf(global_string_buff1, "bytes read=%d, percent_read=%lu, total_bytes_read=%i", bytes_read, percent_read, total_bytes_read);
//Buffer_NewMessage(global_string_buff1);
App_UpdateProgressBar((uint8_t)percent_read);
} while (keep_going == true);
fclose(the_source_handle);
fclose(the_target_handle);
}
// clear the progress bar
App_HideProgressBar();
return total_bytes_read;
error:
if (the_source_handle) fclose(the_source_handle);
if (the_target_handle) fclose(the_target_handle);
App_HideProgressBar();
return -1;
}
// // looks through all files in the file list, comparing the passed file object, and turning true if found in the list
// // use case: checking if a given file in a selection pool is also the potential target for a drag action
// bool Folder_InFileList(WB2KFolderObject* the_folder, WB2KFileObject* the_file, uint8_t the_scope)
// {
// WB2KList* the_item;
//
// if (the_folder == NULL)
// {
// LOG_ERR(("%s %d: passed class object was null", __func__ , __LINE__));
// App_Exit(ERROR_DEFINE_ME); // crash early, crash often
// }
//
// the_item = *(the_folder->list_);
//
// while (the_item != NULL)
// {
// WB2KFileObject* this_file = (WB2KFileObject*)(the_item->payload_);
//
// // is this the item we are looking for?
// if (the_scope == LIST_SCOPE_ALL || (the_scope == LIST_SCOPE_SELECTED && this_file->selected_) || (the_scope == LIST_SCOPE_NOT_SELECTED && this_file->selected_ == false))
// {
// if (this_file == the_file)
// {
// return true;
// }
// }
//
// the_item = the_item->next_item_;
// }
//
// return false;
// }
// looks through all files in the file list, comparing the passed string to the filename of each file.
// Returns NULL if nothing matches, or returns pointer to first matching list item
WB2KList* Folder_FindListItemByFileName(WB2KFolderObject* the_folder, char* the_file_name)
{
// LOGIC:
// iterate through all files in the panel's list
// when comparing, the int compare_len is used. This allows an incoming string with .info to be matched easily against a parent filename without the .info.
WB2KList* the_item;
short the_compare_len = General_Strnlen(the_file_name, FILE_MAX_FILENAME_SIZE);
if (the_folder == NULL)
{
LOG_ERR(("%s %d: passed class object was null", __func__ , __LINE__));
return NULL;
}
the_item = *(the_folder->list_);
while (the_item != NULL)
{
WB2KFileObject* this_file = (WB2KFileObject *)(the_item->payload_);
// is this the item we are looking for?
//DEBUG_OUT(("%s %d: examining file '%s' (len %i) against '%s' (len %i)", __func__ , __LINE__, this_file->file_name_, General_Strnlen(this_file->file_name_, FILE_MAX_PATHNAME_SIZE), the_file_name, the_compare_len));
if ( General_Strnlen(this_file->file_name_, FILE_MAX_FILENAME_SIZE) == the_compare_len )
{
//DEBUG_OUT(("%s %d: lengths reported as match", __func__ , __LINE__));
if ( (General_Strncasecmp(the_file_name, this_file->file_name_, the_compare_len)) == 0)
{
return the_item;
}
}
the_item = the_item->next_item_;
}
//DEBUG_OUT(("%s %d: no match to filename '%s'", __func__ , __LINE__, the_file_name));
return NULL;
}
// // looks through all files in the file list, comparing the passed string to the filepath of each file.
// // if check_device_name is set, it will not return a match unless the drive codes also match up
// // Returns NULL if nothing matches, or returns pointer to first matching list item
// WB2KList* Folder_FindListItemByFilePath(WB2KFolderObject* the_folder, char* the_file_path, short the_compare_len)
// {
// // LOGIC:
// // iterate through all files in the panel's list
// // when comparing, the int compare_len is used. This allows an incoming string with .info to be matched easily against a parent filename without the .info.
// WB2KList* the_item;
//
// if (the_folder == NULL)
// {
// LOG_ERR(("%s %d: passed class object was null", __func__ , __LINE__));
// return NULL;
// }
//
// the_item = *(the_folder->list_);
//
// while (the_item != NULL)
// {
// WB2KFileObject* this_file = (WB2KFileObject *)(the_item->payload_);
//
// // is this the item we are looking for?
// //DEBUG_OUT(("%s %d: examining file '%s' (len %i) against '%s' (len %i)", __func__ , __LINE__, this_file->file_path_, General_Strnlen(this_file->file_path_, FILE_MAX_PATHNAME_SIZE), the_file_path, the_compare_len));
//
// if ( General_Strnlen(this_file->file_path_, FILE_MAX_PATHNAME_SIZE) == the_compare_len )
// {
// //DEBUG_OUT(("%s %d: lengths reported as match", __func__ , __LINE__));
//
// if ( (General_Strncasecmp(the_file_path, this_file->file_path_, the_compare_len)) == 0)
// {
// return the_item;
// }
// }
//
// the_item = the_item->next_item_;
// }
//
// return NULL;
// }
// // looks through all files in the file list, comparing the passed string to the filepath of each file.
// // Returns NULL if nothing matches, or returns pointer to first matching FileObject
// WB2KFileObject* Folder_FindFileByFilePath(WB2KFolderObject* the_folder, char* the_file_path, short the_compare_len)
// {
// // LOGIC:
// // iterate through all files in the panel's list
// // when comparing, the int compare_len is used. This allows an incoming string with .info to be matched easily against a parent filename without the .info.
//
// WB2KList* the_item;
//
// if (the_folder == NULL)
// {
// LOG_ERR(("%s %d: passed class object was null", __func__ , __LINE__));
// return NULL;
// }
//
// the_item = Folder_FindListItemByFilePath(the_folder, the_file_path, the_compare_len);
//
// if (the_item == NULL)
// {
// //DEBUG_OUT(("%s %d: couldn't find path '%s'", __func__ , __LINE__, the_file_path));
// return NULL;
// }
//
// return (WB2KFileObject *)(the_item->payload_);
// }
/*****************************************************************************/
/* Public Function Definitions */
/*****************************************************************************/
// **** CONSTRUCTOR AND DESTRUCTOR *****
// constructor
// allocates space for the object and any string or other properties that need allocating
// if the passed folder pointer is not NULL, it will pass it back without allocating a new one.
// if the passed folder pointer is NULL, it will reset the folder, without destroying it, to a condition where it can be completely repopulated
// destroys all child objects except the folder file, which is emptied out
// recreates the folder file based on the device number and the new_path string (eg, "0:myfolder")
// returns NULL on any non-fatal error
WB2KFolderObject* Folder_NewOrReset(WB2KFolderObject* the_existing_folder,uint8_t the_device_number, char* new_path)
{
WB2KFolderObject* the_folder;
char** this_string_p;
DateTime this_datetime;
if (the_existing_folder == NULL)
{
if ( (the_folder = (WB2KFolderObject*)calloc(1, sizeof(WB2KFolderObject)) ) == NULL)
{
LOG_ERR(("%s %d: could not allocate memory to create new folder object", __func__ , __LINE__));
goto error;
}
LOG_ALLOC(("%s %d: __ALLOC__ the_folder %p size %i", __func__ , __LINE__, the_folder, sizeof(WB2KFolderObject)));
// create a root file folder object
if ( (the_folder->folder_file_ = File_New(new_path, PARAM_FILE_IS_FOLDER, 0, 0, 0, &this_datetime) ) == NULL)
{
App_Exit(ERROR_COULD_NOT_CREATE_ROOT_FOLDER_FILE);
}
}
else
{
the_folder = the_existing_folder;
// free all files in the folder's file list
Folder_DestroyAllFiles(the_folder);
// free the list
LOG_ALLOC(("%s %d: __FREE__ (the_folder)->list_ %p size %i", __func__ , __LINE__, (the_folder)->list_, sizeof(WB2KList*)));
free((the_folder)->list_);
(the_folder)->list_ = NULL;
// free strings
LOG_ALLOC(("%s %d: this_string_p=%p, &the_folder->folder_file_->file_name_=%p", __func__ , __LINE__, this_string_p, &the_folder->folder_file_->file_name_));
this_string_p = &the_folder->folder_file_->file_name_;
LOG_ALLOC(("%s %d: this_string_p=%p, &the_folder->folder_file_->file_name_=%p", __func__ , __LINE__, this_string_p, &the_folder->folder_file_->file_name_));
the_folder->folder_file_->file_name_ = NULL;
LOG_ALLOC(("%s %d: this_string_p=%p, &the_folder->folder_file_->file_name_=%p", __func__ , __LINE__, this_string_p, &the_folder->folder_file_->file_name_));
if (*this_string_p)
{
free(*this_string_p);
}
this_string_p = &the_folder->file_path_;
the_folder->file_path_ = NULL;
if (*this_string_p)
{
free(*this_string_p);
}
}
// initiate the list, but don't add the first node yet (we don't have any items yet)
if ( (the_folder->list_ = (WB2KList**)calloc(1, sizeof(WB2KList*)) ) == NULL)
{
LOG_ERR(("%s %d: could not allocate memory to create new list", __func__ , __LINE__));
goto error;
}
LOG_ALLOC(("%s %d: __ALLOC__ the_folder->list_ %p size %i", __func__ , __LINE__, the_folder->list_, sizeof(WB2KList*)));
// set/reset some other props
the_folder->cur_row_ = 0;
the_folder->file_count_ = 0;
the_folder->device_number_ = the_device_number;
// set the folder filepath to match device+":"
if ( (the_folder->file_path_ = General_StrlcpyWithAlloc(new_path, FILE_MAX_PATHNAME_SIZE)) == NULL)
{
//Buffer_NewMessage("could not allocate memory for the path name");
LOG_ERR(("%s %d: could not allocate memory for the path name", __func__ , __LINE__));
goto error;
}
LOG_ALLOC(("%s %d: __ALLOC__ the_folder->file_path_ %p size %i %s", __func__ , __LINE__, the_folder->file_path_, General_Strnlen(the_folder->file_path_, FILE_MAX_PATHNAME_SIZE) + 1, the_folder->file_path_));
return the_folder;
error:
if (the_folder) free(the_folder);
return NULL;
}
// destructor
// frees all allocated memory associated with the passed object, and the object itself
void Folder_Destroy(WB2KFolderObject** the_folder)
{
if (*the_folder == NULL)
{
LOG_ERR(("%s %d: passed class object was null", __func__ , __LINE__));
App_Exit(ERROR_FOLDER_TO_DESTROY_WAS_NULL); // crash early, crash often
}
if ((*the_folder)->file_path_ != NULL)
{
LOG_ALLOC(("%s %d: __FREE__ (*the_folder)->file_path_ %p size %i %s", __func__ , __LINE__, (*the_folder)->file_path_, General_Strnlen((*the_folder)->file_path_, FILE_MAX_PATHNAME_SIZE) + 1, (*the_folder)->file_path_));
free((*the_folder)->file_path_);
(*the_folder)->file_path_ = NULL;
}
if ((*the_folder)->folder_file_ != NULL)
{
File_Destroy(&(*the_folder)->folder_file_);
}
// free all files in the folder's file list
Folder_DestroyAllFiles(*the_folder);
LOG_ALLOC(("%s %d: __FREE__ (*the_folder)->list_ %p size %i", __func__ , __LINE__, (*the_folder)->list_, sizeof(WB2KList*)));
free((*the_folder)->list_);
(*the_folder)->list_ = NULL;
// free the folder object itself
LOG_ALLOC(("%s %d: __FREE__ *the_folder %p size %i", __func__ , __LINE__, *the_folder, sizeof(WB2KFolderObject)));
free(*the_folder);
*the_folder = NULL;
}
// free every fileobject in the panel's list, and remove the nodes from the list
void Folder_DestroyAllFiles(WB2KFolderObject* the_folder)
{
int num_nodes = 0;
WB2KList* the_item;
if (the_folder == NULL)
{
LOG_ERR(("%s %d: passed class object was null", __func__ , __LINE__));
App_Exit(ERROR_DESTROY_ALL_FOLDER_WAS_NULL); // crash early, crash often
}
the_item = *(the_folder->list_);
while (the_item != NULL)
{
WB2KFileObject* this_file = (WB2KFileObject*)(the_item->payload_);
// sprintf(global_string_buff1, "Folder_DestroyAllFiles: destroying '%s'...", this_file->file_name_);
// Buffer_NewMessage(global_string_buff1);
File_Destroy(&this_file);
++num_nodes;
--the_folder->file_count_;
the_item = the_item->next_item_;
}
// now free up the list items themselves
List_Destroy(the_folder->list_);
//DEBUG_OUT(("%s %d: %i files freed", __func__ , __LINE__, num_nodes));
//Buffer_NewMessage("Done destroying all files in folder");
return;
}
// **** SETTERS *****
// sets the row num (-1, or 0-n) of the currently selected file
void Folder_SetCurrentRow(WB2KFolderObject* the_folder, int16_t the_row_number)
{
if (the_folder == NULL)
{
LOG_ERR(("%s %d: passed class object was null", __func__ , __LINE__));
App_Exit(ERROR_SET_CURR_ROW_FOLDER_WAS_NULL); // crash early, crash often
}
the_folder->cur_row_ = the_row_number;
}
// **** GETTERS *****
// // returns the list of files associated with the folder
// WB2KList** Folder_GetFileList(WB2KFolderObject* the_folder)
// {
// if (the_folder == NULL)
// {
// LOG_ERR(("%s %d: passed class object was null", __func__ , __LINE__));
// return NULL;
// }
//
// return the_folder->list_;
// }
// // returns the file object for the root folder
// WB2KFileObject* Folder_GetFolderFile(WB2KFolderObject* the_folder)
// {
// if (the_folder == NULL)
// {
// LOG_ERR(("%s %d: passed class object was null", __func__ , __LINE__));
// return NULL;
// }
//
// return the_folder->folder_file_;
// }
// // returns true if folder has any files/folders in it. based on curated file_count_ property, not on a live check of disk.
// bool Folder_HasChildren(WB2KFolderObject* the_folder)
// {
// if (the_folder == NULL)
// {
// LOG_ERR(("%s %d: passed class object was null", __func__ , __LINE__));
// App_Exit(ERROR_DEFINE_ME); // crash early, crash often
// }
//
// if (the_folder == NULL)
// {
// return false;
// }
//
// if (the_folder->file_count_ == 0)
// {
// return false;
// }
//
// return true;
// }
// returns total number of files in this folder
uint16_t Folder_GetCountFiles(WB2KFolderObject* the_folder)
{
if (the_folder == NULL)
{
LOG_ERR(("%s %d: passed class object was null", __func__ , __LINE__));
App_Exit(ERROR_DEFINE_ME); // crash early, crash often
}
return the_folder->file_count_;
}
// returns the row num (-1, or 0-n) of the currently selected file
int16_t Folder_GetCurrentRow(WB2KFolderObject* the_folder)
{
if (the_folder == NULL)
{
LOG_ERR(("%s %d: passed class object was null", __func__ , __LINE__));
App_Exit(ERROR_GET_CURR_ROW_FOLDER_WAS_NULL); // crash early, crash often
}
return the_folder->cur_row_;
}
// returns the currently selected file, or NULL if no file is marked as selected
WB2KFileObject* Folder_GetCurrentFile(WB2KFolderObject* the_folder)
{
if (the_folder->cur_row_ < 0)
{
return NULL;
}
return Folder_FindFileByRow(the_folder, the_folder->cur_row_);
}
// returns the file type of the currently selected file, or 0 if no file is marked as selected
uint8_t Folder_GetCurrentFileType(WB2KFolderObject* the_folder)
{
WB2KFileObject* the_file;
the_file = Folder_GetCurrentFile(the_folder);
if (the_file == NULL)
{
return 0;
}
return the_file->file_type_;
}
// // returns true if folder has any files/folders showing as selected
// bool Folder_HasSelections(WB2KFolderObject* the_folder)
// {
// // TODO: OPTIMIZATION - think about having dedicated loop for this check that stops on first hit. will be faster with bigger folders.
// // TODO: OPTIMIZATION - think about tracking this as a class property instead, and update when files are selected/unselected? Not sure which would be faster.
//
// if (the_folder == NULL)
// {
// LOG_ERR(("%s %d: passed class object was null", __func__ , __LINE__));
// App_Exit(ERROR_DEFINE_ME); // crash early, crash often
// }
//
// if (Folder_GetCountSelectedFiles(the_folder) == 0)
// {
// return false;
// }
//
// return true;
// }
// // returns number of currently selected files in this folder
// uint16_t Folder_GetCountSelectedFiles(WB2KFolderObject* the_folder)
// {
// // LOGIC:
// // iterate through all files in the folder's list and count any that are marked as selected
//
// uint16_t the_count = 0;
// WB2KList* the_item;
//
// if (the_folder == NULL)
// {
// LOG_ERR(("%s %d: passed class object was null", __func__ , __LINE__));
// App_Exit(ERROR_DEFINE_ME); // crash early, crash often
// }
//
// the_item = *(the_folder->list_);
//
// while (the_item != NULL)
// {
// WB2KFileObject* this_file = (WB2KFileObject*)(the_item->payload_);
//
// if (this_file->selected_)
// {
// ++the_count;
// }
//
// the_item = the_item->next_item_;
// }
//
// return the_count;
// }
// // returns the first selected file/folder in the folder.
// // use Folder_GetCountSelectedFiles() first if you need to make sure you will be getting the only selected file.
// WB2KFileObject* Folder_GetFirstSelectedFile(WB2KFolderObject* the_folder)
// {
// // LOGIC:
// // iterate through all files in the folder's list and return the first file/folder marked as selected
//
// WB2KList* the_item;
//
// if (the_folder == NULL)
// {
// LOG_ERR(("%s %d: passed class object was null", __func__ , __LINE__));
// return NULL;
// }
//
// the_item = *(the_folder->list_);
//
// while (the_item != NULL)
// {
// WB2KFileObject* this_file = (WB2KFileObject*)(the_item->payload_);
//
// if (this_file->selected_)
// {
// return this_file;
// }
//
// the_item = the_item->next_item_;
// }
//
// return NULL;
// }
// // returns the first file/folder in the folder.
// WB2KFileObject* Folder_GetFirstFile(WB2KFolderObject* the_folder)
// {
// WB2KList* the_item;
//
// if (the_folder == NULL)
// {
// LOG_ERR(("%s %d: passed class object was null", __func__ , __LINE__));
// return NULL;
// }
//
// the_item = *(the_folder->list_);
//
// if (the_item != NULL)
// {
// WB2KFileObject* this_file = (WB2KFileObject*)(the_item->payload_);
// return this_file;
// }
//
// return NULL;
// }
// // returns the lowest or highest row number used by all the selected files in the folder
// // WARNING: will always return a number, even if no files selected, so calling function must have made it's own checks on selection where necessary
// uint16_t Folder_GetMinOrMaxSelectedRow(WB2KFolderObject* the_folder, bool find_max)
// {
// // LOGIC:
// // iterate through all files in the folder's list and keep track of the lowest row # for those that are selected
//
// uint16_t boundary = 0xFFFF;
// WB2KList* the_item;
//
// if (the_folder == NULL)
// {
// LOG_ERR(("%s %d: passed class object was null", __func__ , __LINE__));
// App_Exit(ERROR_DEFINE_ME); // crash early, crash often
// }
//
// the_item = *(the_folder->list_);
//
// if (find_max)
// {
// boundary = 0;
// }
//
// while (the_item != NULL)
// {
// WB2KFileObject* this_file = (WB2KFileObject*)(the_item->payload_);
//
// if (this_file->selected_)
// {
// uint16_t this;
//
// this = this_file->row_;
//
// if (find_max)
// {
// if (this > boundary) boundary = this;
// }
// else
// {
// if (this < boundary) boundary = this;
// }
// }
//
// the_item = the_item->next_item_;
// }
//
// return boundary;
// }
// // looks through all files in the file list, comparing the passed string to the filename_ of each file.
// // Returns NULL if nothing matches, or returns pointer to first FileObject with a filename that starts with the same string as the one passed
// // DOES NOT REQUIRE a match to the full filename. case insensitive search is used.
// WB2KFileObject* Folder_FindFileByFileNameStartsWith(WB2KFolderObject* the_folder, char* string_to_match, int compare_len)
// {
// // LOGIC:
// // iterate through all files in the panel's list
// // when comparing, the int compare_len is used to limit the number of chars of filename that are searched
//
// WB2KList* the_item;
//
// if (the_folder == NULL)
// {
// LOG_ERR(("%s %d: passed class object was null", __func__ , __LINE__));
// return NULL;
// }
//
// the_item = *(the_folder->list_);
//
// while (the_item != NULL)
// {
// WB2KFileObject* this_file = (WB2KFileObject*)(the_item->payload_);
//
// // is this the item we are looking for?
// if ( General_Strncasecmp(string_to_match, this_file->file_name_, compare_len) == 0)
// {
// return this_file;
// }
//
// the_item = the_item->next_item_;
// }
//
// DEBUG_OUT(("%s %d: couldn't find filename match for '%s'. compare_len=%i", __func__ , __LINE__, string_to_match, compare_len));
//
// return NULL;
// }
// looks through all files in the file list, comparing the passed row to that of each file.
// Returns NULL if nothing matches, or returns pointer to first matching FileObject
WB2KFileObject* Folder_FindFileByRow(WB2KFolderObject* the_folder, uint8_t the_row)
{
WB2KList* the_item;
if (the_folder == NULL)
{
LOG_ERR(("%s %d: passed class object was null", __func__ , __LINE__));
return NULL;
}
the_item = *(the_folder->list_);
while (the_item != NULL)
{
WB2KFileObject* this_file = (WB2KFileObject*)(the_item->payload_);
// is this the item we are looking for?
if ( this_file->row_ == the_row)
{
return this_file;
}
the_item = the_item->next_item_;
}
DEBUG_OUT(("%s %d: couldn't find row %i", __func__ , __LINE__, the_row));
return NULL;
}
// **** OTHER FUNCTIONS *****
// populate the files in a folder by doing a directory command
uint8_t Folder_PopulateFiles(WB2KFolderObject* the_folder)
{
bool file_added;
char* this_file_name;
struct DIR* dir;
struct dirent* dirent;
uint8_t the_error_code = ERROR_NO_ERROR;
uint16_t file_cnt = 0;
WB2KFileObject* this_file;
DateTime this_datetime;
uint16_t the_block_size;
// uint8_t* temp_ptr;// = (uint8_t*)&dirent->d_blocks;
if (the_folder == NULL)
{
LOG_ERR(("%s %d: passed class object was null", __func__ , __LINE__));
App_Exit(ERROR_POPULATE_FILES_FOLDER_WAS_NULL); // crash early, crash often
}
if (the_folder->folder_file_ == NULL)
{
//sprintf(global_string_buff1, "folder file was null");
//Buffer_NewMessage(global_string_buff1);
LOG_ERR(("%s %d: passed file was null", __func__ , __LINE__));
App_Exit(ERROR_FOLDER_FILE_WAS_NULL); // crash early, crash often
}
if (the_folder->file_path_ == NULL)
{
//sprintf(global_string_buff1, "filepath for folder '%s' was null", the_folder->file_path_);
//Buffer_NewMessage(global_string_buff1);
LOG_ERR(("%s %d: passed folder's filepath was null", __func__ , __LINE__));
App_Exit(ERROR_FOLDER_WAS_NULL); // crash early, crash often
}
// set up base path for the folder + /. we will use this to build the filepaths for the files individually
General_Strlcpy(global_temp_path_1, the_folder->file_path_, FILE_MAX_PATHNAME_SIZE);
// reset panel's file count, as we will be starting over from zero
the_folder->file_count_ = 0;
// account for FAT32 sectors vs IEC blocks when estimating file szie
if (the_folder->device_number_ == 0)
{
// SD-card = FAT32
the_block_size = FILE_BYTES_PER_BLOCK;
}
else
{
// a floppy = CBMDOS
the_block_size = FILE_BYTES_PER_BLOCK_IEC;
}
/* print directory listing */
dir = Kernel_OpenDir(the_folder->file_path_);
if (! dir) {
//sprintf(global_string_buff1, "Kernel_OpenDir failed. filepath='%s'. errno=%u", the_folder->file_path_, errno);
//sprintf(global_string_buff1, "Kernel_OpenDir failed. filepath='%s'", the_folder->file_path_);
//Buffer_NewMessage(global_string_buff1);
return ERROR_COULD_NOT_OPEN_DIR;
}
while ( (dirent = Kernel_ReadDir(dir)) != NULL )
{
// is this is the disk name, or a file?
//temp_ptr = (uint8_t*)&dirent->d_bytes;
//temp_ptr = (uint8_t*)&dirent->d_blocks;
//sprintf(global_string_buff1, "dirent->d_name='%s', dirent->d_type=%u,", dirent->d_name, dirent->d_type);
//sprintf(global_string_buff1, "dirent->d_name='%s', dirent->d_type=%u, dirent->d_name[0]=%u", dirent->d_name, dirent->d_type, dirent->d_name[0]);
//sprintf(global_string_buff1, "%02X %02X %02X %02X %02X %02X %02X %02X %02X %02X", temp_ptr[0], temp_ptr[1], temp_ptr[2], temp_ptr[3], temp_ptr[4], temp_ptr[5], temp_ptr[6], temp_ptr[7], temp_ptr[8], temp_ptr[9]);
//sprintf(global_string_buff1, "%s %02X %02X %02X %02X %02X %02X %02X %02X", dirent->d_name, temp_ptr[0], temp_ptr[1], temp_ptr[2], temp_ptr[3], temp_ptr[4], temp_ptr[5], temp_ptr[6], temp_ptr[7]);
//Buffer_NewMessage(global_string_buff1);
if (_DE_ISDIR(dirent->d_type))
{
this_file_name = dirent->d_name;
if (this_file_name[0] == '.' && this_file_name[1] != '.')
{
// this is a dir starting with '.' probably macOS junk, OR
// this is a the current dir ".", and we still don't need to see it.
}
else
{
//sprintf(global_string_buff1, "file '%s' detected as dir, setting path to '%s'", this_file_name, global_temp_path_2);
//Buffer_NewMessage(global_string_buff1);
this_file = File_New(this_file_name, PARAM_FILE_IS_FOLDER, (dirent->d_blocks * FILE_BYTES_PER_BLOCK), _CBM_T_DIR, file_cnt, &this_datetime);
if (this_file == NULL)
{
LOG_ERR(("%s %d: Could not allocate memory for file object", __func__ , __LINE__));
//DEBUG_OUT(("%s %d: Could not allocate memory for file object '%s'", __func__ , __LINE__, this_file_name));
the_error_code = ERROR_COULD_NOT_CREATE_NEW_FILE_OBJECT;
sprintf(global_string_buff1, "Could not allocate memory for file object '%s'", this_file_name);
Buffer_NewMessage(global_string_buff1);
goto error;
}
// Add this file to the list of files
file_added = Folder_AddNewFile(the_folder, this_file);
// if this is first file in scan, preselect it
if (file_cnt == 0)
{
this_file->selected_ = true;
}
++file_cnt;
//sprintf(global_string_buff1, "file '%s' identified as folder by _DE_ISDIR, added=%u", dirent->d_name, file_added);
//Buffer_NewMessage(global_string_buff1);
}
}
else if (_DE_ISLBL(dirent->d_type))
{