无法使用正则表达式创建文本文件的格式检查

时间:2018-12-15 21:09:11

标签: c++ regex

我的引擎中现在有一个.material文件,如下所示:

diffuse:res/textures/container.png
specular:res/textures/containerspecular.png
displacement:res/textures/containerdisplacement.png
d_slot:0
s_slot:1
ds_slot:2
shineness:12.0
displacement_factor:0.2

我目前正在使用幼稚的方法来提取属性:

/* --------------Diffuse ----------------------------------------------------------------------*/
            getline(myfile, line);
            diffuseTexture = line;
            diffuseTexture = diffuseTexture.substr(line.find("diffuse:") + 8, line.length() - 8);


            /* --------------Specular ----------------------------------------------------------------------*/
            getline(myfile, line);
            specularTexture = line.substr(line.find("specular:") + 9, line.length() - 9);

            /* --------------Displacement-------------------------------------------------------------------*/
            getline(myfile, line);
            displacementTexture = line.substr(line.find("displacement:") + 13, line.length() - 13);

            /* --------------Diffuse Slot -------------------------------------------------------------------*/
            getline(myfile, line);
            diffSlot = std::stoi(line.substr(line.find("d_slot:") + 7, line.length() - 7));

            /* --------------Specular Slot -------------------------------------------------------------------*/
            getline(myfile, line);
            specSlot = std::stoi(line.substr(line.find("s_slot:") + 7, line.length() - 7));

            /* --------------Displacement Slot ---------------------------------------------------------------*/
            getline(myfile, line);
            dispSlot = std::stoi(line.substr(line.find("ds_slot:") + 8, line.length() - 8));

            /* --------------Shineness------------------------------------------------------------------------*/
            getline(myfile, line);
            shineness = std::stof(line.substr(line.find("shineness:") + 10, line.length() - 10));

            /* --------------Displacement Factor--------------------------------------------------------------*/
            getline(myfile, line);
            displacementFactor = std::stof(line.substr(line.find("displacement_factor:") + 20, line.length() - 20));

            myfile.close();

所以我一直在查找正则表达式,但无法使正则表达式检查格式。 如果somoene可以帮助我,那将不胜感激。 我还应该使用正则表达式中的匹配项提取数据,还是只检查文件格式是否正确,然后应用我的方法?谢谢。

bool Material::_checkFileFormat(const String& filepath)
    {
        std::ifstream t(filepath);
        std::string str((std::istreambuf_iterator<char>(t)),
            std::istreambuf_iterator<char>());

        std::smatch matches;
        std::regex reg("(diffuse:)");

        std::sregex_iterator currentMatch(str.begin(), str.end(), reg);
        std::sregex_iterator lastMatch;

        while (currentMatch != lastMatch) 
        {
            std::smatch match = *currentMatch;
            std::cout << match.str() << "/n";
            currentMatch++;
        }

        std::cout << std::endl;

        return true;
    }

1 个答案:

答案 0 :(得分:0)

正如galik所说,一般的方式是最好的解决方法。

void Material::_checkFileFormat(const String& filepath)
{


    std::vector<std::string> lines;
    std::ifstream myMaterialFile(filepath);

    while (!myMaterialFile.eof() && myMaterialFile.is_open())
    {
        std::string temp;
        getline(myMaterialFile, temp);
        lines.push_back(temp);
    }

    myMaterialFile.close();

    int line_number = 0;
    for(std::string line : lines)
    {
        line_number++;

        switch (line_number) 
        {
            case 1:
                if (line.find("diffuse:") == std::string::npos)
                {
                    MAGMA_FATAL("Error in reading diffuse texture");
                }
                    break;
            case 2:
                if (line.find("specular:") == std::string::npos)
                {
                    MAGMA_FATAL("Error in reading specular map");
                }
                break;
            case 3:
                if (line.find("displacement:") == std::string::npos)
                {
                    MAGMA_FATAL("Error in reading displacement map");
                }
                break;
            case 4:
                if (line.find("d_slot:") == std::string::npos)
                {
                    MAGMA_FATAL("Error in reading diffuse slot");
                }
                break;
            case 5:
                if (line.find("s_slot:") == std::string::npos)
                {
                    MAGMA_FATAL("Error in reading specular slot");
                }
                break;
            case 6:
                if (line.find("ds_slot:") == std::string::npos)
                {
                    MAGMA_FATAL("Error in reading displacement slot");
                }
                break;
            case 7:
                if (line.find("shineness:") == std::string::npos)
                {
                    MAGMA_FATAL("Error in reading shineness");
                }
                break;                  
        }

    }   

}

在代码中,我检查磁盘中是否存在纹理:

if (myfile.is_open())
    {

        /* --------------Diffuse ----------------------------------------------------------------------*/
        getline(myfile, line);

        diffuseTexture = line.substr(line.find("diffuse:") + 8, line.length() - 8);

        if (!std::experimental::filesystem::exists(diffuseTexture)) //TODO check if is cross-plataform
        {
            MAGMA_FATAL("Invalid diffuse texture path!"); //correct path checking
        }
        /*-----------------------------------------------------------------------------------------------*/


        /* --------------Specular ----------------------------------------------------------------------*/
        getline(myfile, line);

        specularTexture = line.substr(line.find("specular:") + 9, line.length() - 9);

        if (!std::experimental::filesystem::exists(specularTexture))
        {
            MAGMA_FATAL("Invalid specular texture map path!");
        }
        /*-----------------------------------------------------------------------------------------------*/

        /* --------------Displacement-------------------------------------------------------------------*/
        getline(myfile, line);

        displacementTexture = line.substr(line.find("displacement:") + 13, line.length() - 13);

        if (!std::experimental::filesystem::exists(specularTexture))
        {
            MAGMA_FATAL("Invalid displacement texture map path!");
        }
        /*-----------------------------------------------------------------------------------------------*/

        /* --------------Diffuse Slot -------------------------------------------------------------------*/

        getline(myfile, line);
        try
        {
            diffSlot = std::stoi(line.substr(line.find("d_slot:") + 7, line.length() - 7));
        }
        catch (std::exception& e)//Check for invalid value in d slot!
        {
            MAGMA_FATAL("Invalid diffuse slot value.");
        }

        /*-----------------------------------------------------------------------------------------------*


        /* --------------Specular Slot -------------------------------------------------------------------*/
        getline(myfile, line);

        try
        {
            specSlot = std::stoi(line.substr(line.find("s_slot:") + 7, line.length() - 7));
        }
        catch (std::exception& e) //Check for invalid value in s slot!
        {
            MAGMA_FATAL("Invalid specular slot value.");
        }

        /* --------------Displacement Slot ---------------------------------------------------------------*/

        getline(myfile, line);


        try
        {
            dispSlot = std::stoi(line.substr(line.find("ds_slot:") + 8, line.length() - 8));
        }
        catch (std::exception& e) //Check for invalid value in ds slot!
        {
            MAGMA_FATAL("Invalid dispalcement slot value.");
        }


        /* --------------Shineness------------------------------------------------------------------------*/
        getline(myfile, line);

        try
        {
            shineness = std::stof(line.substr(line.find("shineness:") + 10, line.length() - 10));
        }
        catch (std::exception& e) //Check for invalid shineness
        {
            MAGMA_FATAL("Invalid shineness value.");
        }

        /* --------------Displacement Factor--------------------------------------------------------------*/
        getline(myfile, line);

        try
        {

            displacementFactor = std::stof(line.substr(line.find("displacement_factor:") + 20, line.length() - 20));
        }
        catch (std::exception& e) //Check for invalid value in slot!
        {
            MAGMA_FATAL("Invalid dispalcement factor value.");
        }
        /*-------------------------------------------------------------------------------------------------*/


        /*▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬*/
        /*▬▬*/  this->_resName = filepath.substr(line.find("\res\materials") + 15, filepath.length() - 14);//Nome do Material!  //▬▬▬▬
        /*▬▬*/  MAGMA_LOG("Material Name:" << _resName);                                                                        //▬▬▬▬
        /*▬▬*/  this->_diffuse = magma_new_resource<Texture>(diffuseTexture, TT_DIFFUSE);                                       //▬▬▬▬
        /*▬▬*/  this->_specular = magma_new_resource<Texture>(specularTexture, TT_TEXTURE_MAP);                                 //▬▬▬▬
        /*▬▬*/  this->_displacement = magma_new_resource<Texture>(displacementTexture, TT_TEXTURE_MAP);                         //▬▬▬▬
        /*▬▬*/  this->_diffuseSlot = diffSlot;                                                                                  //▬▬▬▬
        /*▬▬*/  this->_specularSlot = specSlot;                                                                                 //▬▬▬▬
        /*▬▬*/  this->_displacementSlot = dispSlot;                                                                             //▬▬▬▬
        /*▬▬*/  this->_shineness = shineness;                                                                                   //▬▬▬▬
        /*▬▬*/  this->_displacementFactor = displacementFactor;                                                                 //▬▬▬▬
        /*▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬*/

        myfile.close();

        return true;
    }
    /*==================================================================================================================*/

    return false;
}