void main(void)
{
	char output[255];
	char output2[255];

	assert(fatfs_total_path_levels("C:\\folder\\file.zip") == 1);
	assert(fatfs_total_path_levels("C:\\file.zip") == 0);
	assert(fatfs_total_path_levels("C:\\folder\\folder2\\file.zip") == 2);
	assert(fatfs_total_path_levels("C:\\") == -1);
	assert(fatfs_total_path_levels("") == -1);
	assert(fatfs_total_path_levels("/dev/etc/file.zip") == 2);
	assert(fatfs_total_path_levels("/dev/file.zip") == 1);

	assert(fatfs_get_substring("C:\\folder\\file.zip", 0, output, sizeof(output)) == 0);
	assert(strcmp(output, "folder") == 0);

	assert(fatfs_get_substring("C:\\folder\\file.zip", 1, output, sizeof(output)) == 0);
	assert(strcmp(output, "file.zip") == 0);

	assert(fatfs_get_substring("/dev/etc/file.zip", 0, output, sizeof(output)) == 0);
	assert(strcmp(output, "dev") == 0);

	assert(fatfs_get_substring("/dev/etc/file.zip", 1, output, sizeof(output)) == 0);
	assert(strcmp(output, "etc") == 0);

	assert(fatfs_get_substring("/dev/etc/file.zip", 2, output, sizeof(output)) == 0);
	assert(strcmp(output, "file.zip") == 0);

	assert(fatfs_split_path("C:\\folder\\file.zip", output, sizeof(output), output2, sizeof(output2)) == 0);
	assert(strcmp(output, "C:\\folder") == 0);
	assert(strcmp(output2, "file.zip") == 0);

	assert(fatfs_split_path("C:\\file.zip", output, sizeof(output), output2, sizeof(output2)) == 0);
	assert(output[0] == 0);
	assert(strcmp(output2, "file.zip") == 0);

	assert(fatfs_split_path("/dev/etc/file.zip", output, sizeof(output), output2, sizeof(output2)) == 0);
	assert(strcmp(output, "/dev/etc") == 0);
	assert(strcmp(output2, "file.zip") == 0);

	assert(FileString_GetExtension("C:\\file.zip") == strlen("C:\\file"));
	assert(FileString_GetExtension("C:\\file.zip.ext") == strlen("C:\\file.zip"));
	assert(FileString_GetExtension("C:\\file.zip.") == strlen("C:\\file.zip"));

	assert(FileString_TrimLength("C:\\file.zip", strlen("C:\\file.zip")) == strlen("C:\\file.zip"));
	assert(FileString_TrimLength("C:\\file.zip   ", strlen("C:\\file.zip   ")) == strlen("C:\\file.zip"));
	assert(FileString_TrimLength("   ", strlen("   ")) == 0);

	assert(fatfs_compare_names("C:\\file.ext", "C:\\file.ext") == 1);
	assert(fatfs_compare_names("C:\\file2.ext", "C:\\file.ext") == 0);
	assert(fatfs_compare_names("C:\\file  .ext", "C:\\file.ext") == 1);
	assert(fatfs_compare_names("C:\\file  .ext", "C:\\file2.ext") == 0);

	assert(fatfs_string_ends_with_slash("C:\\folder") == 0);
	assert(fatfs_string_ends_with_slash("C:\\folder\\") == 1);
	assert(fatfs_string_ends_with_slash("/path") == 0);
	assert(fatfs_string_ends_with_slash("/path/a") == 0);
	assert(fatfs_string_ends_with_slash("/path/") == 1);
}
//-----------------------------------------------------------------------------
// _check_file_open: Returns true if the file is already open
//-----------------------------------------------------------------------------
static int _check_file_open(FL_FILE* file)
{
	FL_FILE* openFile = _open_file_list;
	
	// Compare open files
	while (openFile)
	{
		// If not the current file 
		if (openFile != file)
		{
			// Compare path and name
			if ( (fatfs_compare_names(openFile->path,file->path)) && (fatfs_compare_names(openFile->filename,file->filename)) )
				return 1;
		}

		openFile = openFile->next;
	}

	return 0;
}
示例#3
0
//-------------------------------------------------------------
// fatfs_get_file_entry: Find the file entry for a filename
//-------------------------------------------------------------
uint32 fatfs_get_file_entry(struct fatfs *fs, uint32 Cluster, char *name_to_find, struct fat_dir_entry *sfEntry)
{
    uint8 item=0;
    uint16 recordoffset = 0;
    uint8 i=0;
    int x=0;
    char *long_filename = NULL;
    char short_filename[13];
    struct lfn_cache lfn;
    int dotRequired = 0;
    struct fat_dir_entry *directoryEntry;

    fatfs_lfn_cache_init(&lfn, 1);

    // Main cluster following loop
    while (1)
    {
        // Read sector
        if (fatfs_sector_reader(fs, Cluster, x++, 0)) // If sector read was successfull
        {
            // Analyse Sector
            for (item = 0; item < FAT_DIR_ENTRIES_PER_SECTOR; item++)
            {
                // Create the multiplier for sector access
                recordoffset = FAT_DIR_ENTRY_SIZE * item;

                // Overlay directory entry over buffer
                directoryEntry = (struct fat_dir_entry*)(fs->currentsector.sector+recordoffset);

#if FATFS_INC_LFN_SUPPORT
                // Long File Name Text Found
                if (fatfs_entry_lfn_text(directoryEntry) )
                    fatfs_lfn_cache_entry(&lfn, fs->currentsector.sector+recordoffset);

                // If Invalid record found delete any long file name information collated
                else if (fatfs_entry_lfn_invalid(directoryEntry) )
                    fatfs_lfn_cache_init(&lfn, 0);

                // Normal SFN Entry and Long text exists
                else if (fatfs_entry_lfn_exists(&lfn, directoryEntry) )
                {
                    long_filename = fatfs_lfn_cache_get(&lfn);

                    // Compare names to see if they match
                    if (fatfs_compare_names(long_filename, name_to_find))
                    {
                        memcpy(sfEntry,directoryEntry,sizeof(struct fat_dir_entry));
                        return 1;
                    }

                    fatfs_lfn_cache_init(&lfn, 0);
                }
                else
#endif
                // Normal Entry, only 8.3 Text
                if (fatfs_entry_sfn_only(directoryEntry) )
                {
                    memset(short_filename, 0, sizeof(short_filename));

                    // Copy name to string
                    for (i=0; i<8; i++)
                        short_filename[i] = directoryEntry->Name[i];

                    // Extension
                    dotRequired = 0;
                    for (i=8; i<11; i++)
                    {
                        short_filename[i+1] = directoryEntry->Name[i];
                        if (directoryEntry->Name[i] != ' ')
                            dotRequired = 1;
                    }

                    // Dot only required if extension present
                    if (dotRequired)
                    {
                        // If not . or .. entry
                        if (short_filename[0]!='.')
                            short_filename[8] = '.';
                        else
                            short_filename[8] = ' ';
                    }
                    else
                        short_filename[8] = ' ';

                    // Compare names to see if they match
                    if (fatfs_compare_names(short_filename, name_to_find))
                    {
                        memcpy(sfEntry,directoryEntry,sizeof(struct fat_dir_entry));
                        return 1;
                    }

                    fatfs_lfn_cache_init(&lfn, 0);
                }
            } // End of if
        }
        else
            break;
    } // End of while loop

    return 0;
}