📄 filestring.c
字号:
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// FAT32 File IO Library Linux Test Version
// V1.0L
// Rob Riglar
// Copyright 2003,2004
//
// Email: admin@robs-projects.com
//
// Compiled and tested on Redhat 'Shrike' with GNU gcc
//-----------------------------------------------------------------------------
//
// This file is part of FAT32 File IO Library.
//
// FAT32 File IO Library is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// FAT32 File IO Library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with FAT32 File IO Library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "FileString.h"
//-----------------------------------------------------------------------------
// FileString_SplitPath: Full path contains the passed in string. Returned is
// the Path string and file Name string
// Returns 0 if string too long else returns 1.
//-----------------------------------------------------------------------------
int FileString_SplitPath(char *FullPath, char *Path, char *FileName)
{
char *NameStart;
char *s;
if (strlen(FullPath)>maxLFNlength) return 0;
else
{
// Move to end of FullPath String
for (s = FullPath; *s; s++);
// Now move backward until '\' is found. The name starts after the '\'
for (; (*s != '\\') && (s != FullPath); s--);
if (*s == '\\') s++;
// And copy the string from this point until the end into Name
for (NameStart = s; (*FileName++ = *s++););
// Finally we copy the portion of FullPath before the file name
// into Path, removing the non-root trailing backslash if present.
for (s = FullPath; s < NameStart; s++, Path++) *Path = *s;
if ((NameStart > FullPath + 1) && (*(Path - 1) == '\\')) Path--;
*Path = 0;
return 1;
}
}
//-----------------------------------------------------------------------------
// FileString_Path_to_Folder: Takes a pointer to a string, and a sublevel
// required to extract. The found directory is
// returned in a pointer to a string.
// Returns 1 if sublevel to far, 0 is found.
// Starts counting levels from 0 upwards.
//-----------------------------------------------------------------------------
int FileString_Path_to_Folder(char *Path, int levelreq, char *output)
{
int i;
int pathlen=0;
int levels=0;
int copypnt=0;
// Get string length of Path
pathlen = strlen (Path);
// 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) && (*Path!='\\') ) output[copypnt++] = *Path;
// Increment through path string
*Path++;
}
// Null Terminate
output[copypnt] = '\0';
// If a string was copied return 0 else return 1
if (output[0]!='\0')
return 0; // Found
else
return 1; // Not Found
}
//-----------------------------------------------------------------------------
// FileString_PathTotalLevels: Count subdirs in path string
// A path is for example C:\Folder1\SubFolder2
// Counts from 0 upwards, or -1 if string empty
//-----------------------------------------------------------------------------
int FileString_PathTotalLevels(char *path)
{
int levels=0;
// 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 spurious last addition and return the value
return levels - 1;
}
//-----------------------------------------------------------------------------
// FileString_RemoveTrailingSpaces: Take the source string and remove trailing
// spaces.
//-----------------------------------------------------------------------------
void FileString_RemoveTrailingSpaces(char* source, char *dest)
{
int i;
int inputStringLength;
int lastNonSpacePos=0;
// Find total string length of source string
inputStringLength = strlen(source);
// Find last position in string which is not a string
i = inputStringLength;
while (i!=0)
{
if (source[--i]!=' ') break;
}
lastNonSpacePos = i + 1;
// Copy from beginning of string upto nonspace before '.'
for (i = 0; i < lastNonSpacePos; i++)
dest[i]=source[i];
dest[i] = '\0';
}
//-----------------------------------------------------------------------
// FileString_CaptureExtension: Copy the .ext of a filename
// Returning 1 if found, 0 if not.
// Note: Copys null termination if found.
//-----------------------------------------------------------------------
int FileString_CaptureExtension(char* source, char *dest)
{
int inputStringLength;
int lastdotPos=0;
int lastspacePos=0;
int i;
int flag = 0;
// Find total string length of source string
inputStringLength = strlen(source);
// Find last position in string which is not a '.'
i = inputStringLength;
while (i!=0)
{
// Check the '.' is not part of a directory name
if (source[--i]=='\\') flag = 1;
// If current char is a '.' and no '\' have been detected then quit loop
if ((source[i]=='.') && (!flag)) break;
}
// Stop exit from loop counter
lastdotPos = i;
// Find last position in string which is not a ' '
i = inputStringLength;
while (i!=0)
{
// If current char is a '.' and no '\' have been detected then quit loop
if ((source[--i]!=' ')) break;
}
// Stop exit from loop counter
lastspacePos = i+1;
if (lastspacePos<lastdotPos) lastspacePos = inputStringLength;
// Copy from '.' to end of string
for (i = lastdotPos; i < lastspacePos; i++)
dest[i-lastdotPos]=source[i];
dest[i-lastdotPos]='\0';
// If an extension was found then return 1 else return 0
if (lastdotPos>0) return 1;
else return 0;
}
//-----------------------------------------------------------------------------
// FileString_CompoundSpaces: Take the source string and remove trailing spaces
// before and after the extension (if exists).
// Null terminates.
// Uses *extension to work on the extension with
//-----------------------------------------------------------------------------
void FileString_CompoundSpaces(char* source, char *dest, char *extension)
{
int i;
int inputStringLength;
int lastNonSpacePos=0;
int extensionlength=0;
int extensionexists=0;
// Find total string length of source string
FileString_RemoveTrailingSpaces(source, dest);
inputStringLength = strlen(dest);
// Run extension capture algorithm and set flag if detected
if (FileString_CaptureExtension(dest, extension)) extensionexists=1;
// If an extension existed then find its length and subtract from main strings length
if (extensionexists)
{
extensionlength = strlen(extension);
inputStringLength = inputStringLength - extensionlength;
}
// Find last position in string which is not a space
i = inputStringLength;
while (i!=0)
{
if (dest[--i]!=' ') break;
}
lastNonSpacePos = i + 1;
// Copy from beginning of string upto nonspace
for (i = 0; i < lastNonSpacePos; i++)
dest[i]=dest[i];
// If extension existed put back on
if (extensionexists)
{
for (i = 0; i < extensionlength; i++)
dest[i+lastNonSpacePos] = extension[i];
}
// Null terminate
dest[lastNonSpacePos+extensionlength] = '\0';
}
//-----------------------------------------------------------------------
// FileString_CompareName: Compare two filenames to see if they match
//-----------------------------------------------------------------------
int FileString_CompareNames(char* name1, char* name2)
{
char name1_nospace[maxLFNlength], name2_nospace[maxLFNlength];
char extension[10];
// Crop both strings for excess end spaces
// Note: You can use the same destination and source for this
FileString_CompoundSpaces(name1, name1_nospace, extension);
FileString_CompoundSpaces(name2, name2_nospace, extension);
// Compare both strings and return 1 if they match
if (!strcmp(name1_nospace, name2_nospace) )
{
return 1;
}
else
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -