У меня есть файл .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();
, поэтомуЯ искал Regex, но просто не мог сделать регулярное выражение для проверки формата.Если бы кто-нибудь смог мне помочь, то это было бы очень важно.Также я должен извлечь данные, используя совпадения из регулярных выражений или просто проверить, находится ли файл в правильном формате, а затем применить мой подход?Спасибо.
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;
}