📄 apfs_fat32_string.c
字号:
//-----------------------------------------------------------------------------
// This file is part of AP.FS
// AP.FS is a FAT32 file system face to the embedded system,It's target is to
// support the portable storage device such as SD Card,TF Card or USB Disc 's
// file system.
// please login www.another-prj.com to get more details.
// caiyuqing
// 2008-1-8
//-----------------------------------------------------------------------------
#include "apfs_fat32_string.h"
//-----------------------------------------------------------------------------
// apfs_path_to_levels: Take a filename and path and count the sub levels
// of folders. E.g. C:\folder\file.zip = 1 level
// Returns: -1 = Error, 0 or more = Ok
//-----------------------------------------------------------------------------
int apfs_fat32_path_to_levels(const char *path)
{
int levels=0;
int length = (int)strlen(path);
// If too short
if(length < 3)
{
return -1;
}
// Check for C:\...
if(path[1] != ':' || path[2] != '\\')
{
return -1;
}
// Count levels in path string
while(*path)
{
// Fast forward through actual subdir text to next slash
for(*path; *path;)
{
// If slash detected escape from for loop
if(*path == '\\')
{
path++;
break;
}
path++;
}
// Increase number of subdirs founds
levels++;
}
// Subtract the drive letter level and file itself
return levels - 2;
}
//-----------------------------------------------------------------------------
// apfs_get_sub_string: Get a substring from 'path' which contains the folder
// (or file) at the specified level.
// E.g. C:\folder\file.zip : Level 0 = C:\folder, Level 1 = file.zip
// Returns: -1 = Error, 0 = Ok
//-----------------------------------------------------------------------------
int apfs_fat32_get_sub_string(const char *path, int levelreq, char *output)
{
int i;
int pathlen=0;
int levels=0;
int copypnt=0;
// Get string length of Path
pathlen = (int)strlen(path);
// If too short
if(pathlen < 3)
{
return -1;
}
// Check for C:\...
if(path[1] != ':' || path[2] != '\\')
{
return -1;
}
// Loop through the number of times as characters in 'Path'
for(i = 0; i < pathlen; i++)
{
// If a '\' is found then increase level
if(*path == '\\')
{
levels++;
}
// If correct level and the character is not a '\' then copy text to 'output'
if ((levels == (levelreq + 1)) && (*path != '\\'))
{
output[copypnt++] = *path;
}
path ++;
}
// Null Terminate
output[copypnt] = '\0';
// If a string was copied return 0 else return 1
if (output[0] != '\0')
{
return 0; // OK
}
else
{
return -1; // Error
}
}
//-----------------------------------------------------------------------------
// apfs_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 apfs_fat32_split_path(const char *full_path, char *path, char *file_name)
{
int strindex;
// Count the levels to the filepath
int levels = apfs_fat32_path_to_levels(full_path);
if(levels == -1)
{
return -1;
}
// Get filename part of string
apfs_fat32_get_sub_string(full_path, levels, file_name);
// If root file
if(levels == 0)
{
path[0] = '\0';
return 0;
}
else
{
strindex = (int)strlen(full_path) - (int)strlen(file_name);
memcpy(path, full_path, strindex);
path[strindex - 1] = '\0';
return 0;
}
}
//-----------------------------------------------------------------------------
// apfs_trim_length: Get length of string excluding trailing spaces
// Returns -1 if not found or index otherwise
//-----------------------------------------------------------------------------
int apfs_fat32_trim_length(const char *str, int str_len)
{
int length = str_len;
char *str_src = str+str_len-1;
// Find last non white space
while(str_len != 0)
{
if(*str_src == ' ')
{
length = (int)(str_src - str);
}
else
{
break;
}
str_src--;
str_len--;
}
return length;
}
//-----------------------------------------------------------------------------
// apfs_get_extension: Get index to extension within filename,
// Returns -1 if not found or index otherwise
//-----------------------------------------------------------------------------
int apfs_fat32_get_extension(const char *str)
{
int dot_pos = -1;
char *str_src = str;
// Find last '.' in string (if at all)
while(*str_src)
{
if(*str_src == '.')
{
dot_pos = (int)(str_src - str);
}
str_src++;
}
return dot_pos;
}
//-----------------------------------------------------------------------------
// apfs_check_extension: check the extentions name
// Returns 1 if extentions exist, 0 if not
//-----------------------------------------------------------------------------
int apfs_fat32_check_extension(const char* str)
{
int i;
char *ext = str+8;
for(i = 0; i < 3; i++)
{
if(ext[i] != ' ')
{
return 1;
}
}
return 0;
}
//-----------------------------------------------------------------------------
// apfs_string_cmpare_no_case: Compare two strings case with case sensitivity
//-----------------------------------------------------------------------------
int apfs_fat32_string_cmpare_no_case(const char *s1,const char *s2, int n)
{
int diff;
char a,b;
while(n--)
{
a = *s1;
b = *s2;
// Make lower case if uppercase
if((a >= 'A') && (a <= 'Z'))
{
a += 32;
}
if((b >= 'A') && (b <= 'Z'))
{
b += 32;
}
diff = a - b;
// If different
if(diff)
{
return diff;
}
s1++;
s2++;
// If run out of strings
if((*s1 == '\0') || (*s2 == '\0'))
{
break;
}
}
return 0;
}
//-----------------------------------------------------------------------------
// apfs_compare_file_name: Compare two filenames (without copying or changing origonals)
// Returns 1 if match, 0 if not
//-----------------------------------------------------------------------------
int apfs_fat32_file_string_compare(const char* str1, const char* str2)
{
char *ext1 = NULL;
char *ext2 = NULL;
int ext1_pos;
int ext2_pos;
int ext1_len;
int ext2_len;
int file1_len;
int file2_len;
// Get both files extension
ext1_pos = apfs_fat32_get_extension(str1);
ext2_pos = apfs_fat32_get_extension(str2);
// 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((ext1_pos == -1) && (ext2_pos != -1))
{
return 0;
}
if((ext2_pos == -1) && (ext1_pos != -1))
{
return 0;
}
// If they both have extensions, compare them
if((ext1_pos != -1) && (ext2_pos != -1))
{
// Set pointer to start of extension
ext1 = str1 + ext1_pos + 1;
ext2 = str2 + ext2_pos + 1;
ext1_len = strlen(ext1);
ext2_len = strlen(ext2);
// Find length without trailing spaces
ext1_len = apfs_fat32_trim_length(ext1, ext1_len);
ext2_len = apfs_fat32_trim_length(ext2, ext2_len);
// If their length don't match
if(ext1_len != ext2_len)
{
return 0;
}
// If they don't match
else if(apfs_fat32_string_cmpare_no_case(ext1, ext2, ext1_len) != 0)
{
return 0;
}
// Filelength is upto extensions
file1_len = ext1_pos;
file2_len = ext2_pos;
if(file1_len != file2_len)
{
return 0;
}
// Compare main part of filenames
if(apfs_fat32_string_cmpare_no_case(str1, str2, file1_len) != 0)
{
return 0;
}
else
{
return 1;
}
}
// No extensions
else
{
// Filelength is actual filelength
file1_len = (int)strlen(str1);
file2_len = (int)strlen(str2);
// Find length without trailing spaces (before ext)
file1_len = apfs_fat32_trim_length(str1, file1_len);
file2_len = apfs_fat32_trim_length(str2, file2_len);
// Check the file lengths match
if (file1_len != file2_len)
{
return 0;
}
// Compare main part of filenames
else if(apfs_fat32_string_cmpare_no_case(str1, str2, file1_len) != 0)
{
return 0;
}
else
{
return 1;
}
}
}
//-----------------------------------------------------------------------------
// apfs_fat32_copy_short_name: copy short name.
// Return the bytes copied.
//-----------------------------------------------------------------------------
int apfs_fat32_find_trim_space(const char* str,int length)
{
int n ;
if(length < 1)
{
return 0;
}
for(n = length - 1; n >= 0; n--)
{
if(str[n] != ' ')
{
break;
}
}
return n + 1;
}
//-----------------------------------------------------------------------------
// apfs_fat32_copy_short_name: copy short name.
// Return the bytes copied.
//-----------------------------------------------------------------------------
void apfs_fat32_copy_short_name(char *dest,const char* src)
{
int i;
// Copy the main part
int main_length = apfs_fat32_find_trim_space(src, 8);
for(i = 0; i < main_length; i++)
{
dest[i] = src[i];
}
// Copy the externsion part
if(apfs_fat32_check_extension(src))
{
dest[i] = '.';
for(i = 0; i < 3; i++ )
{
dest[main_length + 1 + i] = src[8 + i];
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -