Beispiel #1
0
//-----------------------------------------------------------------------------
// fatfs_compare_names: Compare two filenames (without copying or changing origonals)
// Returns 1 if match, 0 if not
//-----------------------------------------------------------------------------
int fatfs_compare_names(char* strA, char* strB)
{
    char *ext1 = NULL;
    char *ext2 = NULL;
    int ext1Pos, ext2Pos;
    int file1Len, file2Len;

    // Get both files extension
    ext1Pos = FileString_GetExtension(strA);
    ext2Pos = FileString_GetExtension(strB);

    // NOTE: Extension position can be different for matching
    // filename if trailing space are present before it!
    // Check that if one has an extension, so does the other
    if ((ext1Pos==-1) && (ext2Pos!=-1))
        return 0;
    if ((ext2Pos==-1) && (ext1Pos!=-1))
        return 0;

    // If they both have extensions, compare them
    if (ext1Pos!=-1)
    {
        // Set pointer to start of extension
        ext1 = strA+ext1Pos+1;
        ext2 = strB+ext2Pos+1;

        // Verify that the file extension lengths match!
        if (strlen(ext1) != strlen(ext2))
            return 0;

        // If they dont match
        if (FileString_StrCmpNoCase(ext1, ext2, (int)strlen(ext1))!=0)
            return 0;

        // Filelength is upto extensions
        file1Len = ext1Pos;
        file2Len = ext2Pos;
    }
    // No extensions
    else
    {
        // Filelength is actual filelength
        file1Len = (int)strlen(strA);
        file2Len = (int)strlen(strB);
    }

    // Find length without trailing spaces (before ext)
    file1Len = FileString_TrimLength(strA, file1Len);
    file2Len = FileString_TrimLength(strB, file2Len);

    // Check the file lengths match
    if (file1Len!=file2Len)
        return 0;

    // Compare main part of filenames
    if (FileString_StrCmpNoCase(strA, strB, file1Len)!=0)
        return 0;
    else
        return 1;
}
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);
}
Beispiel #3
0
//-----------------------------------------------------------------------------
// fatfs_get_extension: Get extension of filename passed in 'filename'.
// Returned extension is always lower case.
// Returns: 1 if ok, 0 if not.
//-----------------------------------------------------------------------------
int fatfs_get_extension(char* filename, char* out, int maxlen)
{
    int len = 0;

    // Get files extension offset
    int ext_pos = FileString_GetExtension(filename);

    if (ext_pos > 0 && out && maxlen)
    {
        filename += ext_pos + 1;

        while (*filename && len < (maxlen-1))
        {
            char a = *filename++;

            // Make lowercase if uppercase
            if ((a>='A') && (a<='Z'))
                a+= 32;

            *out++ = a;
            len++;
        }

        *out = '\0';
        return 1;
    }

    return 0;
}