TemplateMap* TemplateMap::loadTemplateFile(const char *id, const char *filename, bool onlyInfo) {
	FILE* fh;
	char lastTemplate[1024], tmp2[1024];
	unsigned int i=0;
	TemplateMap *tmap;
	if (filename == NULL || strlen(filename) == 0) {
		return NULL;
	}
	fh = fopen(filename, "rt");
	if (fh == NULL) {
		return NULL;
	}
	if (!onlyInfo) {
		tmap = TemplateMap::add(id, filename);
	} else {
		tmap = new TemplateMap(id);
	}
	char store[4096];
	bool wasTemplate = false;
	char *templateText = NULL;
	int templateTextSize = 0;
	while (fgets(store, sizeof(store), fh) != NULL) {
    	if (sscanf(store, "%s", tmp2) == EOF) continue;
	    //template start
    	bool bFound = false;
        for (unsigned i = 0; i < sizeof(templateNames) / sizeof (templateNames[0]); i++) {
    		if (!strncmp(store, templateNames[i].tokenString, templateNames[i].tokenLen)) {
    			bFound = true;
    			break;
    		}
    	}
        if (bFound) {
			if (wasTemplate) {
				tmap->addTemplate(lastTemplate, templateText);
			}
			if (templateText!=NULL) {
				free (templateText);
			}
			templateText = NULL;
			templateTextSize = 0;
			wasTemplate = true;
            sscanf(store, "<!--%[^-]", lastTemplate);
		} else if (wasTemplate) {
			Utils::appendText(&templateText, &templateTextSize, "%s", store);
		}
  	}
  	if (wasTemplate) {
		tmap->addTemplate(lastTemplate, templateText);
	}
  	fclose(fh);
	static const char *groupTemplates[] = {"MessageInGroupStart", "MessageInGroupInner",
	                                       "hMessageInGroupStart", "hMessageInGroupInner",
										   "MessageOutGroupStart", "MessageOutGroupInner",
	                                       "hMessageOutGroupStart", "hMessageOutGroupInner"};
	tmap->grouping = true;
	for (i = 0; i<SIZEOF(groupTemplates); i++) {
		if (tmap->getTemplate(groupTemplates[i])== NULL) {
			tmap->grouping = false;
			break;
		}
	}
	static const char *rtlTemplates[] = {"MessageInRTL", "MessageOutRTL"}; //"HTMLStartRTL",
	tmap->rtl = true;
	for (i=0; i<SIZEOF(rtlTemplates); i++) {
		if (tmap->getTemplate(rtlTemplates[i])== NULL) {
			tmap->rtl = false;
			break;
		}
	}
	return tmap;
}
bool TemplateMap::loadTemplateFile(const char *proto, const char *filename, bool onlyInfo) {
	FILE* fh;
	char lastTemplate[1024], tmp2[1024];
	char pathstring[500];

	TemplateMap *tmap = TemplateMap::add(proto, filename);
	if (filename == NULL || strlen(filename) == 0) {
		return false;
	}
	strcpy(pathstring, filename);
	char* pathrun = pathstring + strlen(pathstring);
	while ((*pathrun != '\\' && *pathrun != '/') && (pathrun > pathstring)) pathrun--;
	pathrun++;
	*pathrun = '\0';

	fh = fopen(filename, "rt");
	if (fh == NULL) {
		return false;
	}
	char store[4096];
	bool wasTemplate = false;
	char *templateText = NULL;
	int templateTextSize = 0;
	while (fgets(store, sizeof(store), fh) != NULL) {
    	if (sscanf(store, "%s", tmp2) == EOF) continue;
	    //template start
	    if (!onlyInfo) {
            if ( !strncmp(store, "<!--HTMLStart-->", 16) ||
				 !strncmp(store, "<!--MessageIn-->", 16) ||
				 !strncmp(store, "<!--MessageOut-->", 17) ||
				 !strncmp(store, "<!--hMessageIn-->", 17) ||
				 !strncmp(store, "<!--hMessageOut-->", 18) ||
				 !strncmp(store, "<!--File-->", 11) ||
				 !strncmp(store, "<!--hFile-->", 12) ||
				 !strncmp(store, "<!--URL-->", 10) ||
				 !strncmp(store, "<!--hURL-->", 11) ||
				 !strncmp(store, "<!--Status-->", 13) ||
				 !strncmp(store, "<!--hStatus-->", 14) ||
				 !strncmp(store, "<!--MessageInGroupStart-->", 26) ||
				 !strncmp(store, "<!--MessageInGroupInner-->", 26) ||
				 !strncmp(store, "<!--MessageInGroupEnd-->", 24) ||
				 !strncmp(store, "<!--hMessageInGroupStart-->", 27) ||
				 !strncmp(store, "<!--hMessageInGroupInner-->", 27) ||
				 !strncmp(store, "<!--hMessageInGroupEnd-->", 25) ||
				 !strncmp(store, "<!--MessageOutGroupStart-->", 27) ||
				 !strncmp(store, "<!--MessageOutGroupInner-->", 27) ||
				 !strncmp(store, "<!--MessageOutGroupEnd-->", 25) ||
				 !strncmp(store, "<!--hMessageOutGroupStart-->", 28) ||
				 !strncmp(store, "<!--hMessageOutGroupInner-->", 28) ||
				 !strncmp(store, "<!--hMessageOutGroupEnd-->", 26)
				 ) {
				if (wasTemplate) {
					tmap->addTemplate(lastTemplate, templateText);
	                //debugView->writef("1. %s<br>%s", lastTemplate, templateText);
				}
				if (templateText!=NULL) {
					free (templateText);
				}
				templateText = NULL;
				templateTextSize = 0;
				wasTemplate = true;
                sscanf(store, "<!--%[^-]", lastTemplate);
			} else if (wasTemplate) {
				Utils::appendText(&templateText, &templateTextSize, "%s", store);
			}
		}
  	}
  	if (wasTemplate) {
		tmap->addTemplate(lastTemplate, templateText);
        //debugView->writef("2. %s<br>%s", lastTemplate, templateText);
	}
  	fclose(fh);
	static const char *groupTemplates[] = {"MessageInGroupStart", "MessageInGroupInner",
	                                       "hMessageInGroupStart", "hMessageInGroupInner",
										   "MessageOutGroupStart", "MessageOutGroupInner",
	                                       "hMessageOutGroupStart", "hMessageOutGroupInner"};
	tmap->grouping = true;
	for (int i=0; i<8; i++) {
		if (tmap->getTemplate(groupTemplates[i])== NULL) {
			tmap->grouping = false;
			break;
		}
	}
	return true;
}
Exemple #3
0
TemplateMap* TemplateMap::loadTemplateFile(const char *id, const char *filename, bool onlyInfo)
{
	char lastTemplate[1024], tmp2[1024];
	unsigned int i = 0;
	if (filename == NULL || mir_strlen(filename) == 0)
		return NULL;

	FILE *fh = fopen(filename, "rt");
	if (fh == NULL)
		return NULL;

	TemplateMap *tmap;
	if (!onlyInfo)
		tmap = TemplateMap::add(id, filename);
	else
		tmap = new TemplateMap(id);

	char store[4096];
	bool wasTemplate = false;
	CMStringA templateText;
	while (fgets(store, sizeof(store), fh) != NULL) {
		if (sscanf(store, "%s", tmp2) == EOF) continue;
		//template start
		bool bFound = false;
		for (unsigned i = 0; i < _countof(templateNames); i++) {
			if (!strncmp(store, templateNames[i].tokenString, templateNames[i].tokenLen)) {
				bFound = true;
				break;
			}
		}
		if (bFound) {
			if (wasTemplate)
				tmap->addTemplate(lastTemplate, templateText);

			templateText.Empty();
			wasTemplate = true;
			sscanf(store, "<!--%[^-]", lastTemplate);
		}
		else if (wasTemplate)
			templateText.Append(store);
	}
	if (wasTemplate)
		tmap->addTemplate(lastTemplate, templateText);
	templateText.Empty();

	fclose(fh);
	static const char *groupTemplates[] = { "MessageInGroupStart", "MessageInGroupInner",
		"hMessageInGroupStart", "hMessageInGroupInner",
		"MessageOutGroupStart", "MessageOutGroupInner",
		"hMessageOutGroupStart", "hMessageOutGroupInner" };
	tmap->grouping = true;
	for (i = 0; i < _countof(groupTemplates); i++) {
		if (tmap->getTemplate(groupTemplates[i]) == NULL) {
			tmap->grouping = false;
			break;
		}
	}
	static const char *rtlTemplates[] = { "MessageInRTL", "MessageOutRTL" }; //"HTMLStartRTL",
	tmap->rtl = true;
	for (i = 0; i < _countof(rtlTemplates); i++) {
		if (tmap->getTemplate(rtlTemplates[i]) == NULL) {
			tmap->rtl = false;
			break;
		}
	}
	return tmap;
}