gordon's note coding check string as valid filename

check string as valid filename

    bool ContainsInvalidChars(const CString& filename)
    {
        const TCHAR invalidChars[] = { '<', '>', ':', '"', '/', '\\', '|', '?', '*' };
        for (int i = 0; i < _countof(invalidChars); ++i)
        {
            if (filename.Find(invalidChars[i]) != -1)
            {
                return true;
            }
        }
        return false;
    }

 
or


    bool isValidFilenameSyntax(const std::string& filename)
    {
        if (filename.empty()) {
            return false;
        }

        // Common invalid characters for Windows (adjust for other OS if needed)
        const std::string invalidChars = "\\/:*?\"<>|"; 
        for (char c : filename) {
            if (invalidChars.find(c) != std::string::npos) {
                return false;
            }
        }

        // Add checks for reserved names, length limits, etc. if required
        // For example, on Windows, check for CON, PRN, AUX, NUL
        // if (filename == "CON" || filename == "PRN" || ...) { return false; }

        return true;
    }

 

 
#cpp

Leave a Reply

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

Related Post