// fread: no size arg static void fake_fread(volatile u8* dst, u32 size, void* fp) { u32 n = 0; while(n < size) { *dst = fl_fgetc(fp); n++; dst++; } }
//----------------------------------------------------------------- // Main: Test bench file to create 5 files with psuedo random // sequences in of varying lengths - read them back and complete // then remove them. //----------------------------------------------------------------- void main() { int i,j,x; FL_FILE * files[5]; FL_FILE *readFile; char filenames[5][260]; BYTE fileData[5][10000]; BYTE readBuffer[10000]; int fileLengths[5]; BYTE *massiveData; time_t timeStart, timeEnd; #define TESTFILES 6 char *testfile[] = { "X:\\1", "X:\\1.bin", "X:\\12345678.321", "X:\\mylongfilename", "X:\\mylongfilename.bin", "X:\\the Quick Brown Fox jumped over the lazy dog.elf.binfile.jar" }; srand(time(NULL)); // Initialise FAT32_InitDrive(); fl_init(); if (fl_attach_media(FAT_ReadSector, FAT_WriteSector) != FAT_INIT_OK) return; // List directory fl_listdirectory("C:\\"); // return ; test_start: // Generate 5 random files memset(filenames, 0x00, 260*5); for (j=0;j<5;j++) { // Length fileLengths[j] = GetRandom(9999); // Data for (x=0;x<fileLengths[j];x++) fileData[j][x] = (BYTE)GetRandom(255); // Names sprintf(filenames[j], "X:\\Auto Generated Filename Number %d", j+1); } // Create some files for (j=0;j<5;j++) { printf("Creating File: %s [%d bytes]\n", filenames[j], fileLengths[j]); // Create File files[j] = fl_fopen(filenames[j], "w"); if (files[j]!=NULL) { if (fl_fwrite(fileData[j], 1, fileLengths[j], files[j])!=fileLengths[j]) { printf("ERROR: File Write Block Failed File %s Length %d\n", filenames[j], fileLengths[j]); fl_assert(0); } } else { printf("ERROR: Error Creating File %s\n", filenames[j]); fl_assert(0); } fl_fclose(files[j]); // Clear buffer for (i=0;i<sizeof(readBuffer);i++) readBuffer[i] = 0; // Verify File readFile = fl_fopen(filenames[j], "r"); if (readFile!=NULL) { int failed = FALSE; printf("File %s Read Check (fread whole file) [%d bytes]\n", filenames[j], fileLengths[j]); if (fl_fread(readBuffer, 1, fileLengths[j], readFile)!=fileLengths[j]) { printf("ERROR: File %s Read Length Error %d\n", filenames[j], fileLengths[j]); fl_assert(0); } for (i=0;i<fileLengths[j];i++) if ( fileData[j][i] != (BYTE)readBuffer[i] ) failed = TRUE; if (failed) { printf("ERROR: File %s Data Verify Failed\n", filenames[j]); fl_assert(0); } } fl_fclose(readFile); // Clear buffer for (i=0;i<sizeof(readBuffer);i++) readBuffer[i] = 0; // Verify File using fgetc readFile = fl_fopen(filenames[j], "r"); if (readFile!=NULL) { int failed = FALSE; printf("File %s Read Check (fgetc) [%d bytes]\n", filenames[j], fileLengths[j]); i = 0; while (i < fileLengths[j]) { int res = fl_fgetc(readFile); if (res == -1) break; readBuffer[i++] = (BYTE)res; } if (i != fileLengths[j]) { printf("ERROR: File %s Read Length Error %d\n", filenames[j], fileLengths[j]); fl_assert(0); } for (i=0;i<fileLengths[j];i++) if ( fileData[j][i] != (BYTE)readBuffer[i] ) failed = TRUE; if (failed) { printf("ERROR: File %s Data Verify Failed\n", filenames[j]); fl_assert(0); } } fl_fclose(readFile); // Clear buffer for (i=0;i<sizeof(readBuffer);i++) readBuffer[i] = 0; // Verify File chunks readFile = fl_fopen(filenames[j], "r"); if (readFile!=NULL) { int failed = FALSE; printf("File %s Read Check (fread chunks) [%d bytes]\n", filenames[j], fileLengths[j]); i = 0; while (i < fileLengths[j]) { int read_length = GetRandom(1025); if (read_length > (fileLengths[j] - i)) read_length = fileLengths[j] - i; if (fl_fread(readBuffer + i, 1, read_length, readFile) != read_length) { printf("ERROR: File %s fread error\n", filenames[j]); fl_assert(0); break; } i += read_length; } if (i != fileLengths[j]) { printf("ERROR: File %s Read Length Error %d\n", filenames[j], fileLengths[j]); fl_assert(0); } for (i=0;i<fileLengths[j];i++) if ( fileData[j][i] != (BYTE)readBuffer[i] ) { failed = TRUE; break; } if (failed) { printf("ERROR: File %s Data Verify Failed at %d\n", filenames[j], i); fl_assert(0); } } fl_fclose(readFile); // Delete File if (fl_remove(filenames[j])<0) printf("ERROR: Delete File %s Failed\n", filenames[j]); // Verify file is no longer present! readFile = fl_fopen(filenames[j], "r"); if (readFile != NULL) { printf("ERROR: File %s still present after delete!\n", filenames[j]); fl_assert(0); fl_fclose(readFile); } } // Create folder fl_createdirectory("C:\\folder1"); #if 0 // Create massive file #define MASSIVE_FILE_LEN (1024 * 1024) printf("Creating Massive File [%d bytes]\n", MASSIVE_FILE_LEN); massiveData = malloc(MASSIVE_FILE_LEN); if (!massiveData) { printf("ERROR: Could not allocate memory for massive array!\n"); fl_assert(0); fl_shutdown(); return ; } // Create random data for file for (x=0;x<MASSIVE_FILE_LEN;x++) massiveData[x] = (BYTE)GetRandom(255); // Remove if it already exists! fl_remove("X:\\folder1\\massive file.bin"); timeStart = time(NULL); // Create Large File readFile = fl_fopen("X:\\folder1\\massive file.bin", "w"); if (readFile != NULL) { if (fl_fwrite(massiveData, 1, MASSIVE_FILE_LEN, readFile) != MASSIVE_FILE_LEN) { printf("ERROR: File Write Block Failed for massive file (Length %d)\n", MASSIVE_FILE_LEN); fl_assert(0); } } else { printf("ERROR: Error Creating massive file\n"); fl_assert(0); } fl_fclose(readFile); // Verify Massive File readFile = fl_fopen("X:\\folder1\\massive file.bin", "r"); if (readFile!=NULL) { int failed = FALSE; printf("File Massive File Read Check (fread whole file) [%d bytes]\n", MASSIVE_FILE_LEN); i = 0; while (i < MASSIVE_FILE_LEN) { int read_length = GetRandom(2048) + 128; if (read_length > (MASSIVE_FILE_LEN - i)) read_length = MASSIVE_FILE_LEN - i; if (fl_fread(readBuffer, 1, read_length, readFile) != read_length) { printf("ERROR: File massive file fread error\n"); fl_assert(0); break; } for (j=0;j<read_length;j++) if ( massiveData[i+j] != (BYTE)readBuffer[j] ) { printf("ERROR: File Massive File Data Verify Failed at %d\n", i+j); fl_assert(0); break; } i += read_length; } if (i != MASSIVE_FILE_LEN) { printf("ERROR: File massive file Read Length Error %d\n", MASSIVE_FILE_LEN); fl_assert(0); } } fl_fclose(readFile); timeEnd = time(NULL); printf("Time taken %d seconds\n", (int)(timeEnd-timeStart)); free(massiveData); #endif // Filename test for (i=0;i<TESTFILES;i++) { // Create File readFile = fl_fopen(testfile[i], "w"); if (readFile != NULL) ; else { printf("ERROR: Error Creating File %s\n", testfile[i]); fl_assert(0); } fl_fputc(0, readFile); fl_fclose(readFile); readFile = fl_fopen(testfile[i], "r"); assert(readFile); fl_fclose(readFile); } // List directory fl_listdirectory("C:\\"); for (i=0;i<TESTFILES;i++) { // Delete File if (fl_remove(testfile[i])<0) { printf("ERROR: Delete File %s Failed\n", testfile[i]); fl_assert(0); } } fl_shutdown(); printf("\r\nCompleted\r\n"); // List directory fl_listdirectory("C:\\"); }
// search for specified scaler file and load it to specified buffer // return 1 on success, 0 on failure u8 files_load_scaler_name(const char* name, s32* dst, u32 dstSize) { void* fp; u32 size = 0; u32 i; union { u32 u; s32 s; u8 b[4]; } swap; u8 ret = 0; //// test //s32* p = dst; /// app_pause(); fp = list_open_file_name(&scalerList, name, "r", &size); if( fp != NULL) { print_dbg("\r\n scaler file pointer: 0x"); print_dbg_hex((u32)fp); #ifdef SCALER_LE swap.b[3] = fl_fgetc(fp); swap.b[2] = fl_fgetc(fp); swap.b[1] = fl_fgetc(fp); swap.b[0] = fl_fgetc(fp); #else swap.b[0] = fl_fgetc(fp); swap.b[1] = fl_fgetc(fp); swap.b[2] = fl_fgetc(fp); swap.b[3] = fl_fgetc(fp); #endif size = swap.u; print_dbg("\r\n read size (words): 0x"); print_dbg_ulong(size); if(size > dstSize) { print_dbg("\r\n warning: requested scaler data is > target, truncating"); for(i=0; i<dstSize; ++i) { #ifdef SCALER_LE swap.b[3] = fl_fgetc(fp); swap.b[2] = fl_fgetc(fp); swap.b[1] = fl_fgetc(fp); swap.b[0] = fl_fgetc(fp); #else swap.b[0] = fl_fgetc(fp); swap.b[1] = fl_fgetc(fp); swap.b[2] = fl_fgetc(fp); swap.b[3] = fl_fgetc(fp); #endif *dst++ = swap.s; } } else if (size < dstSize) { print_dbg("\r\n warning: requested scaler data is < target, padding"); for(i=0; i<size; ++i) { #ifdef SCALER_LE swap.b[3] = fl_fgetc(fp); swap.b[2] = fl_fgetc(fp); swap.b[1] = fl_fgetc(fp); swap.b[0] = fl_fgetc(fp); #else swap.b[0] = fl_fgetc(fp); swap.b[1] = fl_fgetc(fp); swap.b[2] = fl_fgetc(fp); swap.b[3] = fl_fgetc(fp); #endif *dst++ = swap.s; } // remainder size = dstSize - size; for(i=0; i<size; ++i) { *dst++ = 0; } } else { for(i=0; i<size; ++i) { #ifdef SCALER_LE swap.b[3] = fl_fgetc(fp); swap.b[2] = fl_fgetc(fp); swap.b[1] = fl_fgetc(fp); swap.b[0] = fl_fgetc(fp); #else swap.b[0] = fl_fgetc(fp); swap.b[1] = fl_fgetc(fp); swap.b[2] = fl_fgetc(fp); swap.b[3] = fl_fgetc(fp); #endif *dst++ = swap.s; } } fl_fclose(fp); ret = 1; } else { print_dbg("\r\n error: fp was null in files_load_scaler_name \r\n"); ret = 0; } print_dbg("\r\n finished loading scaler file (?)"); ///// TEST: verify /* for(i=0; i<size; i++) { */ /* print_dbg(" 0x"); print_dbg_hex(p[i]); if((i%4)==0) { print_dbg("\r\n"); } */ /* } */ app_resume(); return ret; }
int32_t file_getc(file_handle_t handle) { return fl_fgetc(handle); }