您的位置:首页 > 编程语言 > C语言/C++

[C/C++标准库]_[初级]_[过滤Windows(MacOSX)文件名中的非法字符]

2014-12-19 22:51 1316 查看
场景:
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: