예제 #1
0
static int processIncludes(TextPackage *package, const char *input, DFBuffer *output, const char *path, DFError **error)
{
    int ok = 1;
    const char **lines = DFStringSplit(input,"\n",0);
    for (int lineno = 0; lines[lineno] && ok; lineno++) {
        const char *line = lines[lineno];
        if (DFStringHasPrefix(line,"#include \"") && DFStringHasSuffix(line,"\"")) {
            char *inclRelPath = DFSubstring(line,10,strlen(line)-1);
            char *inclAbsPath = DFAppendPathComponent(path,inclRelPath);
            char *inclDirName = DFPathDirName(inclAbsPath);
            char *inclContent = DFStringReadFromFile(inclAbsPath,error);
            if (inclContent == NULL) {
                DFErrorFormat(error,"%s: %s",inclRelPath,DFErrorMessage(error));
                ok = 0;
            }
            else if (!processIncludes(package,inclContent,output,inclDirName,error)) {
                ok = 0;
            }
            free(inclRelPath);
            free(inclAbsPath);
            free(inclDirName);
            free(inclContent);
        }
        else {
            DFBufferFormat(output,"%s\n",line);
        }
    }
    free(lines);
    return ok;
}
예제 #2
0
        virtual ReadResult readShader(std::istream& fin,const Options* options) const
        {
            // create shader
            osg::ref_ptr<osg::Shader> shader = new osg::Shader();

            {
                std::stringstream ss;
                ss << fin.rdbuf();
                shader->setShaderSource( ss.str() );
            }

            // check options which can define the type of the shader program
            if (options)
            {
                if (options->getOptionString().find("fragment")!=std::string::npos) shader->setType(osg::Shader::FRAGMENT);
                if (options->getOptionString().find("vertex")!=std::string::npos) shader->setType(osg::Shader::VERTEX);
                if (options->getOptionString().find("geometry")!=std::string::npos) shader->setType(osg::Shader::GEOMETRY);
                if (options->getOptionString().find("tesscontrol")!=std::string::npos) shader->setType(osg::Shader::TESSCONTROL);
                if (options->getOptionString().find("tessevaluation")!=std::string::npos) shader->setType(osg::Shader::TESSEVALUATION);
                if (options->getOptionString().find("compute")!=std::string::npos) shader->setType(osg::Shader::COMPUTE);
            }

            // return valid shader
            return processIncludes( shader.get(), options );
        }
예제 #3
0
bool CXMLConfiguration::loadFile(const std::string& filename)
{
	mFilename = filename;
	bool result = mXMLDocument.LoadFile(filename.c_str());
	if (mPRootConfigSection != NULL)
		delete mPRootConfigSection;
	TiXmlElement *rootElement = mXMLDocument.FirstChildElement(CONST_ConfRootSectionName);
	if (rootElement != NULL)
	{
		mPRootConfigSection = new IXMLConfigSection(rootElement);
		// Process includes inside the configuration. Can only come from other configuration files!

#ifdef XMLCONF_INCLUDETAG_DEBUG_OUTPUT
		mXMLDocument.SaveFile("XmlDEBUG_BeforeIncludes.xml");
#endif

		std::string filePath = mFilename.substr( 0, mFilename.rfind(CONST_PATH_SEPARATOR)+1);
		processIncludes(rootElement, rootElement, filePath);

#ifdef XMLCONF_INCLUDETAG_DEBUG_OUTPUT
		mXMLDocument.SaveFile("XmlDEBUG_AfterIncludes.xml");
#endif

		return result;
	}
	else
		return false;
}
예제 #4
0
static int parsePackage(TextPackage *package, const char *string, const char *path, DFError **error)
{
    DFBuffer *replaced = DFBufferNew();
    if (!strcmp(path,""))
        path = ".";

    if (!processIncludes(package,string,replaced,path,error)) {
        DFBufferRelease(replaced);
        return 0;
    }


    char *currentKey = strdup("");
    DFBuffer *currentValue = DFBufferNew();
    const char **lines = DFStringSplit(replaced->data,"\n",0);
    for (int lineno = 0; lines[lineno]; lineno++) {
        const char *line = lines[lineno];

        if (!DFStringHasPrefix(line,"#")) {
            DFBufferFormat(currentValue,"%s\n",line);
        }
        else if (DFStringHasPrefix(line,"#item ")) {
            package->keys = (char **)realloc(package->keys,(package->nkeys+2)*sizeof(char *));
            package->keys[package->nkeys++] = strdup(currentKey);
            package->keys[package->nkeys] = NULL;
            DFHashTableAdd(package->items,currentKey,currentValue->data);
            free(currentKey);
            DFBufferRelease(currentValue);
            currentKey = DFSubstring(line,6,strlen(line));
            currentValue = DFBufferNew();
        }
        else if (DFStringHasPrefix(line,"##")) {
            DFBufferFormat(currentValue,"%s\n",&line[1]);
        }
        else {
            DFErrorFormat(error,"Unknown command: %s on line %d",line,(lineno+1));
            return 0;
        }
    }
    package->keys = (char **)realloc(package->keys,(package->nkeys+2)*sizeof(char *));
    package->keys[package->nkeys++] = strdup(currentKey);
    package->keys[package->nkeys] = NULL;
    DFHashTableAdd(package->items,currentKey,currentValue->data);

    free(lines);
    free(currentKey);
    DFBufferRelease(currentValue);
    DFBufferRelease(replaced);
    return 1;
}
예제 #5
0
void CXMLConfiguration::processIncludes(TiXmlElement* rootNode, TiXmlElement* node, const std::string& filePath)
{
	// Walk through entire document and replace all include nodes
	TiXmlElement* nextChild;
	for(TiXmlElement* child = node->FirstChildElement(); child; child = nextChild)
	{
		nextChild = child->NextSiblingElement();	// Request next child here, because we may delete child if it turns out to be an <include> node!
		if (child->Value() == std::string("include"))
		{
			// Process the include
			TiXmlElement* filenameNode = child->FirstChildElement("filename");
			if (filenameNode != NULL)
			{
				std::string filename = filenameNode->GetText();
				// If filename is not absolute, interpret the filename as relative to the main filename (mFilename)
				if (filename[1] != ':' && filename[0] != '/')
				{
					// Add mFilename's path to filename
					filename = filePath + filename;
				}
				mLogDebugLn("Processing XML include file " << filename << " ...");
				TiXmlDocument includeDoc;
				if (includeDoc.LoadFile(filename.c_str()))
				{
					// We loaded the file, now do the following: 1. Add source paths, 2. Add constants and strings.
					TiXmlElement *includeRootNode = includeDoc.FirstChildElement(CONST_ConfRootSectionName);
					if (includeRootNode != NULL)
					{
						// 1) Add all provided paths to the original file, but find nested includes in the includeDoc itself first.
						//    Paths are assumed to be defined relative to the "configuration" root node of the file
						// Also process this root node
						std::string includeFilePath = filename.substr( 0, filename.rfind(CONST_PATH_SEPARATOR)+1);
						processIncludes(includeRootNode, includeRootNode, includeFilePath);
						// Process all path nodes
						TiXmlNode	*insertAfter = child;	// Maintain node order by incrementing insertion point
						for (TiXmlElement *pathNode = child->FirstChildElement("path"); pathNode; pathNode = pathNode->NextSiblingElement("path"))
						{
							TiXmlElementList resultNodes;
							bool searchResult = findXmlNode(includeRootNode, pathNode->GetText(), &resultNodes);
							if (searchResult)
							{
								for (unsigned int iResNode=0; iResNode<resultNodes.size(); iResNode++)
								{
									// Skip constants and strings sections
									if ((resultNodes[iResNode] != includeRootNode->FirstChildElement(CONST_ConfConstantsSectionName))
											&& (resultNodes[iResNode] != includeRootNode->FirstChildElement(CONST_ConfStringsSectionName)))
									insertAfter = node->InsertAfterChild(insertAfter, *(resultNodes[iResNode]));
								}
							}
							else
								mLogErrorLn("Could not find path \"" << pathNode->GetText() << "\" in XML file \"" << filename << "\"!");
						}
						// We're done with this include node; remove it
						node->RemoveChild(child);


						// 2) Add the constants and strings sections of this file automatically to the rootNode
						//    If they do not exist in the original document, create such a section. Otherwise, merge.
						//
						// Constants (CONST_ConfConstantsSectionName)
						TiXmlElement *includeConstantsSection = includeRootNode->FirstChildElement(CONST_ConfConstantsSectionName);
						if (includeConstantsSection != NULL)
						{
							TiXmlElement *rootConstantsSection = rootNode->FirstChildElement(CONST_ConfConstantsSectionName);
							if (rootConstantsSection == NULL)
								// Insert
								rootNode->InsertEndChild(*includeConstantsSection);
							else
							{	// Merge
								for (TiXmlElement *constElement = includeConstantsSection->FirstChildElement(); constElement; constElement = constElement->NextSiblingElement())
									rootConstantsSection->InsertEndChild(*constElement);
							}
						}
						// Strings (CONST_ConfStringsSectionName)
						TiXmlElement *includeStringsSection = includeRootNode->FirstChildElement(CONST_ConfStringsSectionName);
						if (includeStringsSection != NULL)
						{
							TiXmlElement *rootStringsSection = rootNode->FirstChildElement(CONST_ConfStringsSectionName);
							if (rootStringsSection == NULL)
								// Insert
								rootNode->InsertEndChild(*includeStringsSection);
							else
							{	// Merge
								for (TiXmlElement *stringElement = includeStringsSection->FirstChildElement(); stringElement; stringElement = stringElement->NextSiblingElement())
									rootStringsSection->InsertEndChild(*stringElement);
							}
						}
					}
					else
						mLogErrorLn("Could not find root node \"" << CONST_ConfRootSectionName << "\" in XML file \"" << filename << "\"! Include paths are always relative to this root node.");
				}
				else
					mLogErrorLn("[ERROR] Could not load input XML file \"" << filename << "\"! Error: " << includeDoc.ErrorDesc());
			}
		}
		else
			// Recursive processing
			processIncludes(rootNode, child, filePath);
	}

}