Ejemplo n.º 1
0
//-----------------------------------------------------------------------------
// fatfs_split_path: Full path contains the passed in string.
// Returned is the path string and file Name string
// E.g. C:\folder\file.zip -> path = C:\folder  filename = file.zip
// E.g. C:\file.zip -> path = [blank]  filename = file.zip
//-----------------------------------------------------------------------------
int fatfs_split_path(char *full_path, char *path, int max_path, char *filename, int max_filename)
{
    int strindex;

    // Count the levels to the filepath
    int levels = fatfs_total_path_levels(full_path);
    if (levels == -1)
        return -1;

    // Get filename part of string
    if (fatfs_get_substring(full_path, levels, filename, max_filename) != 0)
        return -1;

    // If root file
    if (levels == 0)
        path[0] = '\0';
    else
    {
        strindex = (int)strlen(full_path) - (int)strlen(filename);
        if (strindex > max_path)
            strindex = max_path;

        memcpy(path, full_path, strindex);
        path[strindex-1] = '\0';
    }

    return 0;
}
//-----------------------------------------------------------------------------
// fl_list_opendir: Opens a directory for listing
//-----------------------------------------------------------------------------
int fl_list_opendir(const char *path, struct fs_dir_list_status *dirls)
{
	int levels;
	UINT32 cluster = FAT32_INVALID_CLUSTER;

	// If first call to library, initialise
	CHECK_FL_INIT();

	FL_LOCK(&_fs);

	levels = fatfs_total_path_levels((char*)path) + 1;

	// If path is in the root dir
	if (levels == 0)
		cluster = fatfs_get_root_cluster(&_fs);
	// Find parent directory start cluster
	else
		_open_directory((char*)path, &cluster);

	fatfs_list_directory_start(&_fs, dirls, cluster);

	FL_UNLOCK(&_fs);

	return cluster != FAT32_INVALID_CLUSTER ? 1 : 0;
}
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);
}
//-----------------------------------------------------------------------------
// _open_directory: Cycle through path string to find the start cluster
// address of the highest subdir.
//-----------------------------------------------------------------------------
static int _open_directory(char *path, unsigned long *pathCluster)
{
	int levels;
	int sublevel;
	char currentfolder[FATFS_MAX_LONG_FILENAME];
	struct fat_dir_entry sfEntry;
	unsigned long startcluster;

	// Set starting cluster to root cluster
	startcluster = fatfs_get_root_cluster(&_fs);

	// Find number of levels
	levels = fatfs_total_path_levels(path);

	// Cycle through each level and get the start sector
	for (sublevel=0;sublevel<(levels+1);sublevel++) 
	{
		if (fatfs_get_substring(path, sublevel, currentfolder, sizeof(currentfolder)) == -1)
			return 0;

		// Find clusteraddress for folder (currentfolder) 
		if (fatfs_get_file_entry(&_fs, startcluster, currentfolder,&sfEntry))
		{
			// Check entry is folder
			if (fatfs_entry_is_dir(&sfEntry))
				startcluster = (((unsigned long)sfEntry.FstClusHI)<<16) + sfEntry.FstClusLO;
			else
				return 0;
		}
		else
			return 0;
	}

	*pathCluster = startcluster;
	return 1;
}