[C/C++标准库]_[初级]_[过滤Windows(MacOSX)文件名中的非法字符]
2014-12-19 22:51
1316 查看
场景:
1. 通常生成文件时需要一个文件名,而生成文件名的方式可能是通过用户输入的字符,但是有些字符在windows上是不能作为文件名的,强行创建这类文件会失败。
2.一般可以通过正则表达式替换所有的非法字符,这里实现的是C++98 template(模板)方式的替换无效字符,std::string,std::wstring. 基本上windows上和字符串打交道都离不开wstring.
函数:
调用:
输出:
MacOSX的简便实现:
20150929 更新,使用算法库里的replace_if也可以实现:
输出:
1. 通常生成文件时需要一个文件名,而生成文件名的方式可能是通过用户输入的字符,但是有些字符在windows上是不能作为文件名的,强行创建这类文件会失败。
2.一般可以通过正则表达式替换所有的非法字符,这里实现的是C++98 template(模板)方式的替换无效字符,std::string,std::wstring. 基本上windows上和字符串打交道都离不开wstring.
函数:
template<class T> void FilterInvalidFileNameChar(T& str) { T t; t.resize(9); t[0] = 0x5C; t[1] = 0x2F; t[2] = 0x3A; t[3] = 0x2A; t[4] = 0x3F; t[5] = 0x22; t[6] = 0x3C; t[7] = 0x3E; t[8] = 0x7C; int length = str.length(); for(int i = 0; i< length; ++i) { if(str[i] <= 0x1F || str[i] == 0x7F || t.find(str[i]) != T::npos) { str[i] = 0x5F; } } } inline char* Unicode2Ansi(const wchar_t* unicode) { int len; len = WideCharToMultiByte(CP_ACP, 0, unicode, -1, NULL, 0, NULL, NULL); char *szUtf8 = (char*)malloc(len + 1); memset(szUtf8, 0, len + 1); WideCharToMultiByte(CP_ACP, 0,unicode, -1, szUtf8, len, NULL,NULL); return szUtf8; }
调用:
std::wstring wss(L"/asfasdf中国asdfas*dfa.txt"); FilterInvalidFileNameChar(wss); cout << Unicode2Ansi(wss.c_str()) << endl; std::string ss("/asfasdf\\asdfas*dfa.txt"); FilterInvalidFileNameChar(ss); cout << ss.c_str() << endl;
输出:
_asfasdf中国asdfas_dfa.txt _asfasdf_asdfas_dfa.txt
MacOSX的简便实现:
+(NSString*) sanitizeFileNameString:(NSString *)fileName { NSCharacterSet* illegalFileNameCharacters = [NSCharacterSet characterSetWithCharactersInString:@"/\\?%*|\"<>"]; return [[fileName componentsSeparatedByCharactersInSet:illegalFileNameCharacters] componentsJoinedByString:@"-"]; }
20150929 更新,使用算法库里的replace_if也可以实现:
template <typename T> bool MatchInvalidCharPredicate(const T& t) { unsigned char t1 = (unsigned char)t; if(t1 <= 0x1F || t1 == 0x7F || t1 == 0x5C || t1 == 0x2F || t1 == 0x3A || t1 == 0x2A || t1 == 0x3F || t1 == 0x22 || t1 == 0x3C || t1 == 0x7C ) { return true; } return false; } template<typename C,class T> void FilterInvalidFileNameChar(T& c) { std::replace_if(c.begin(),c.end(),MatchInvalidCharPredicate<C>,L'_'); } inline char* Unicode2Ansi(const wchar_t* unicode) { int len; len = WideCharToMultiByte(CP_ACP, 0, unicode, -1, NULL, 0, NULL, NULL); char *szUtf8 = (char*)malloc(len + 1); memset(szUtf8, 0, len + 1); WideCharToMultiByte(CP_ACP, 0,unicode, -1, szUtf8, len, NULL,NULL); return szUtf8; } void TestFilterInvalidFileNameChar() { std::wstring wss(L"/as中国fasdfas?asdfas*dfa.txt"); FilterInvalidFileNameChar<wchar_t>(wss); std::cout << "======TestFilterInvalidFileNameChar=================" << std::endl; std::wcout << Unicode2Ansi(wss.c_str()) << std::endl; }
输出:
_as中国fasdfas_asdfas_dfa.txt
相关文章推荐
- [C/C++标准库]_[初级]_[过滤Windows(MacOSX)文件名中的非法字符]
- [C/C++标准库]_[初级]_[使用正则表达式过滤Windows文件名中的非法字符]
- windows文件名非法字符过滤检测-正则表达式
- 过滤Windows文件名中的非法字符
- [Windows/MacOSX]_初级_[在英文环境下不使用软键盘输入希腊字母]
- 过滤文件名中的非法字符
- [Windows/MacOSX]_初级_[在英文环境下不使用软键盘输入希腊字母]
- C#创建目录,文件名过滤特殊字符串,非法字符
- [C/C++]_[Linux,Windows,MacOSX控制台即时响应按键消息]
- [C/C++]_[初级]_[使用remove,remove_if,remove_copy_if过滤元素或字符]
- [C/C++]_[初级]_[使用remove,remove_if,remove_copy_if过滤元素或字符]
- windows下文件名非法字符
- 过滤文件名中的非法字符
- [C/C++]_[初级]_[替换过滤utf8字符串里无效字符]
- [C/C++]_[初级]_[标准库之几种算法库函数的运用,如字符串查找(字符个数查找)、分隔、删除、替换、排序、字符交换位置、大小写转换等等]
- [C/C++]_[Linux,Windows,MacOSX控制台即时响应按键消息]
- 过滤文件名非法字符
- Cross-Platform Development in C++: Building Mac OS X, Linux, and Windows Applications
- [C/C++]_[初级]_[替换过滤utf8字符串里无效字符]
- 《Windows Via C/C++》边学习,边翻译(七)操作字符和字符串-6