/**
 * Main parser for a tdf file. Parses initialization data, creates the header and
 * source files, and passes parameter definitions to the appropriate function.
 *
 * @param fp				file to parse
 *
 * @return 0 on success, -1 on error
 */
int parseTemplateDefinitionFile(File &fp)
{
TemplateDefinitionFile tdfFile;

	int result = tdfFile.parse(fp);
	if (result != 0)
		return result;

	if (tdfFile.getTemplateName() == ROOT_TEMPLATE_NAME)
	{
		fprintf(stderr, "Warning: will not generate C++ code for class %s, "
			"invalid template name\n", tdfFile.getTemplateName().c_str());
		return -1;
	}

	tdfFile.setWriteForCompiler(false);
	result = writeTemplate(tdfFile, tdfFile.getPath());
	if (result == 0)
	{
		tdfFile.setWriteForCompiler(true);
		result = writeTemplate(tdfFile, tdfFile.getCompilerPath());
	}

	return result;
}	// parseTemplateDefinitionFile
Ejemplo n.º 2
0
int
_evlWriteTemplate(const template_t *t, const char *path)
{
	tfile_t *tf;
	size_t fileSize;
	int fd;
	int nBytes;

	tf = allocTfile(path, 1000);
	writeTemplate(t, tf);
	fileSize = tf->tf_next - tf->tf_buf;

	fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0644);
	if (fd < 0) {
		perror(path);
		freeTfile(tf);
		return -1;
	}
	nBytes = safeWrite(fd, tf->tf_buf, fileSize);
	assert(nBytes == fileSize);

	freeTfile(tf);
	(void) close(fd);
	return 0;
}
Ejemplo n.º 3
0
void KisTemplateTree::writeTemplateTree()
{
    QString localDir = KoResourcePaths::saveLocation("data", m_templatesResourcePath);

    Q_FOREACH (KisTemplateGroup *group, m_groups) {
        //dbgUI <<"---------------------------------";
        //dbgUI <<"group:" << group->name();

        bool touched = false;
        QList<KisTemplate*> templates = group->templates();
        QList<KisTemplate*>::iterator it = templates.begin();
        for (; it != templates.end() && !touched && !group->touched(); ++it)
            touched = (*it)->touched();

        if (group->touched() || touched) {
            //dbgUI <<"touched";
            if (!group->isHidden()) {
                //dbgUI <<"not hidden";
                QDir path;
                path.mkpath(localDir + group->name()); // create the local group dir
            } else {
                //dbgUI <<"hidden";
                if (group->dirs().count() == 1 && group->dirs().contains(localDir)) {
                    //dbgUI <<"local only";
                    QFile f(group->dirs().first());
                    f.remove();
                    //dbgUI <<"removing:" << group->dirs().first();
                } else {
                    //dbgUI <<"global";
                    QDir path;
                    path.mkpath(localDir + group->name());
                }
            }
        }
        Q_FOREACH (KisTemplate *t, templates) {
            if (t->touched()) {
                //dbgUI <<"++template:" << t->name();
                writeTemplate(t, group, localDir);
            }
            if (t->isHidden() && t->touched()) {
                //dbgUI <<"+++ delete local template ##############";
                writeTemplate(t, group, localDir);
                QFile::remove(t->file());
                QFile::remove(t->picture());
            }
        }
    }
bool VCSharedProject::writeProject() const
{
  LabelHandlerFnMap nodeHandlers;
  nodeHandlers["GlobalProperties"] = &VCSharedProject::writeGlobalProperties;
  nodeHandlers["ProjectItems"] = &VCSharedProject::writeProjectItems;

  return writeTemplate(getPath(), nodeHandlers);
}
Ejemplo n.º 5
0
void KoTemplateTree::writeTemplateTree()
{

    const QString localDir = KoResourcePaths::saveLocation("data", m_templatesResourcePath);

    foreach (KoTemplateGroup *group, m_groups) {
        //kDebug( 30003 ) <<"---------------------------------";
        //kDebug( 30003 ) <<"group:" << group->name();

        bool touched = false;
        QList<KoTemplate*> templates = group->templates();
        QList<KoTemplate*>::iterator it = templates.begin();
        for (; it != templates.end() && !touched && !group->touched(); ++it)
            touched = (*it)->touched();

        if (group->touched() || touched) {
            //kDebug( 30003 ) <<"touched";
            if (!group->isHidden()) {
                //kDebug( 30003 ) <<"not hidden";
                QDir().mkpath(localDir + group->name()); // create the local group dir
            } else {
                //kDebug( 30003 ) <<"hidden";
                if (group->dirs().count() == 1 && group->dirs().contains(localDir)) {
                    //kDebug( 30003 ) <<"local only";
                    KIO::NetAccess::del(QUrl::fromLocalFile(group->dirs().first()), 0);
                    //kDebug( 30003 ) <<"removing:" << group->dirs().first();
                } else {
                    //kDebug( 30003 ) <<"global";
                    QDir().mkpath(localDir + group->name());
                }
            }
        }
        foreach (KoTemplate *t, templates) {
            if (t->touched()) {
                //kDebug( 30003 ) <<"++template:" << t->name();
                writeTemplate(t, group, localDir);
            }
            if (t->isHidden() && t->touched()) {
                //kDebug( 30003 ) <<"+++ delete local template ##############";
                writeTemplate(t, group, localDir);
                QFile::remove(t->file());
                QFile::remove(t->picture());
            }
        }
    }
Ejemplo n.º 6
0
bool VCProject::writeFilters() const
{
  std::string filtersFilePath = getPath() + ".filters";
  LabelHandlerFnMap nodeHandlers;
  nodeHandlers["FilterItemDescriptions"] = &VCProject::writeFilterItemDescriptions;
  nodeHandlers["FilterDescriptions"] = &VCProject::writeFilterDescriptions;

  return writeTemplate(filtersFilePath, nodeHandlers);
}
Ejemplo n.º 7
0
bool VCProject::writeProject() const
{
  LabelHandlerFnMap nodeHandlers;
  nodeHandlers["ProjectConfigSummary"] = &VCProject::writeProjectConfigSummary;
  nodeHandlers["GlobalProperties"] = &VCProject::writeGlobalProperties;
  nodeHandlers["ConfigurationProperties"] = &VCProject::writeConfigurationProperties;
  nodeHandlers["BuildExtensionProperties"] = &VCProject::writeBuildExtensionProperties;
  nodeHandlers["BuildExtensionTargets"] = &VCProject::writeBuildExtensionTargets;
  nodeHandlers["SharedProjects"] = &VCProject::writeSharedProjects;
  nodeHandlers["ConfigurationPropertySheets"] = &VCProject::writeConfigurationPropertySheets;
  nodeHandlers["UserMacros"] = &VCProject::writeUserMacros;
  nodeHandlers["ConfigurationItemDefinitions"] = &VCProject::writeConfigurationItemDefinitions;
  nodeHandlers["ProjectItems"] = &VCProject::writeProjectItems;
  nodeHandlers["ProjectReferences"] = &VCProject::writeProjectReferences;

  return writeTemplate(getPath(), nodeHandlers);
}
Ejemplo n.º 8
0
void TextOutputStream::write(long double source) const 
{
	writeTemplate(source);
}
Ejemplo n.º 9
0
void TextOutputStream::write(float source) const 
{
	writeTemplate(source);
}
Ejemplo n.º 10
0
void TextOutputStream::write(uint64 source) const 
{
	writeTemplate(source);
}
Ejemplo n.º 11
0
void TextOutputStream::write(const void* source) const 
{
	writeTemplate(source);
}
Ejemplo n.º 12
0
void TextOutputStream::write(bool source) const 
{
	writeTemplate(source);
}
	int ljocr::MQDFClassification::train(std::string character, std::vector< std::vector<float> >& features, float ratio, std::string path) {
		Mat samples(Size(features.front().size(), features.size()), CV_32F, cv::Scalar(0));
		for (int j = 0; j < features.size(); ++j) {
			vector<float> feature = features[j];
			for (int i = 0; i < feature.size(); ++i) {
				samples.at<float>(j, i) = feature[i];
			}
		}
#if _DEBUG
		//Mat_<double> samples =
		//(Mat_<double>(3, 2) <<
		//2.0, 2.0, 2.0, 5.0, 6.0, 5.0);
#endif
		Mat cov, mean;
		calcCovarMatrix(samples, cov, mean, CV_COVAR_NORMAL | CV_COVAR_ROWS);
		MQDFParam mqdf_param;
		//mqdf_param.mean = mean;
		mean.convertTo(mean, CV_32F);
		mqdf_param.mean = mean.clone();
		cov /= (samples.rows - 1);   //cov/(N-1)
		SVD svd;
		Mat w, u, vt;
		svd.compute(cov, w, u, vt, SVD::FULL_UV);
#if _DEBUG
		//Mat_<double> cov1 =
		//(Mat_<double>(3, 3) <<
		//2.0, 1.0, 0.0, 1.0, 4.0, 5.0, 0, 5, 3);
		//svd.compute(cov1, w, u, vt, SVD::FULL_UV);
#endif
		double sum_w = 0;
		for (int j = 0; j < w.rows; ++j) {
			sum_w += w.at<double>(j, 0);
		}
		mqdf_param.principle_dim = 0;
		double log_lamda = 0;
		if (ratio < 1.0) {
			int j;
			double temp_sum = 0;
			for (j = 0; j < w.rows; ++j) {
				double lamda_temp = w.at<double>(j, 0);
				temp_sum += w.at<double>(j, 0);
				if (temp_sum / sum_w > ratio) {
					break;
				}
				log_lamda += log(lamda_temp);
			}
			mqdf_param.principle_dim = j;
		} else {
			mqdf_param.principle_dim = ratio;
			for (int j = 0; j < mqdf_param.principle_dim; ++j) {
				double lamda_temp = w.at<double>(j, 0);
				log_lamda += log(lamda_temp);
			}
		}
		float principle_eigenvalue_sum = 0;
		for (int j = 0; j < mqdf_param.principle_dim; ++j) {
			principle_eigenvalue_sum += w.at<double>(j, 0);
		}
		//mqdf_param.delta = w.at<double>(mqdf_param.principle_dim, 0);
		//mqdf_param.delta = (sum_w - principle_eigenvalue_sum) / (w.rows - mqdf_param.principle_dim);  //ML estimation value, the performance of generation is not good
		//mqdf_param.remainder = log_lamda + (w.rows - mqdf_param.principle_dim)*log(mqdf_param.delta);

		//lj: all classes share the same delta, so the delta in remainder is ignored
#ifdef CROSS_VALIDATION
		mqdf_param.delta = sum_w / w.rows;  //prepare for cross validation
#else
		//mqdf_param.delta = 27.6436;  //the value is estimated by cross_validation for 300 samples
		//mqdf_param.delta = 26.2426969;  //the value is estimated by cross_validation for 1000 samples
		//mqdf_param.delta = 32.6721689;  //the value is estimated by cross_validation for 3755 samples
		//mqdf_param.delta = 64.919496*0.7;  //the value is estimated by cross_validation for 3755 samples
		//mqdf_param.delta = 1.896899*0.7;  //the value is estimated by cross_validation for 3755 samples. best for MPF
		//mqdf_param.delta = 1.361794*0.7;  //the value is estimated by cross_validation for 500 samples. best for MPF
		//mqdf_param.delta = 23.766482*0.7;  //the value is estimated by cross_validation for 3755 samples
		//mqdf_param.delta = 0.960328*0.7;  //the value is estimated by cross_validation for 500 samples. best for MPF class 0
		//mqdf_param.delta = 1.114572*0.7;  //the value is estimated by cross_validation for 500 samples. best for MPF class 1
		//mqdf_param.delta = 0.997763*0.7;  //the value is estimated by cross_validation for 500 samples. best for MPF class 1
		//mqdf_param.delta = 1.604470*0.7;  //the value is estimated by cross_validation for 1000 samples. best for MPF 
		//mqdf_param.delta = 1.339773*0.7;  //the value is estimated by cross_validation for 1000 samples. best for MPF class 0
		//mqdf_param.delta = 1.228499*0.7;  //the value is estimated by cross_validation for 1000 samples. best for MPF class 1
		//mqdf_param.delta = 1.677936*0.7;  //the value is estimated by cross_validation for 3755 samples. best for MPF class 0
		//mqdf_param.delta = 1.675047*0.7;  //the value is estimated by cross_validation for 3755 samples. best for MPF class 1
		//mqdf_param.delta = 1.541195*0.7;  //the value is estimated by cross_validation for 3755 samples. best for MPF class 2, 3 class
		//mqdf_param.delta = 1.571957*0.7;  //the value is estimated by cross_validation for 3755 samples. best for MPF class 1, 3 class
		//mqdf_param.delta = 1.52195*0.7;  //the value is estimated by cross_validation for 3755 samples. best for MPF class 0, 3 class

		//mqdf_param.delta = 1.385178*0.7;  //the value is estimated by cross_validation for 3755 samples. best for MPF class 4, 5 class
		//mqdf_param.delta = 1.430945*0.7;  //the value is estimated by cross_validation for 3755 samples. best for MPF class 3, 5 class
		//mqdf_param.delta = 1.249550*0.7;  //the value is estimated by cross_validation for 3755 samples. best for MPF class 2, 5 class
		//mqdf_param.delta = 1.322204*0.7;  //the value is estimated by cross_validation for 3755 samples. best for MPF class 1, 5 class
		//mqdf_param.delta = 1.358019*0.7;  //the value is estimated by cross_validation for 3755 samples. best for MPF class 0, 5 class

		//mqdf_param.delta = 1.496428*0.7;  //the value is estimated by cross_validation for 3755 samples. best for MPF class 0, 5 class pca95
		//mqdf_param.delta = 1.435780*0.7;  //the value is estimated by cross_validation for 3755 samples. best for MPF class 1, 5 class pca95
		//mqdf_param.delta = 1.333554*0.7;  //the value is estimated by cross_validation for 3755 samples. best for MPF class 2, 5 class pca95
		//mqdf_param.delta = 1.287792*0.7;  //the value is estimated by cross_validation for 3755 samples. best for MPF class 3, 5 class pca95
		mqdf_param.delta = 1.309553*0.7;  //the value is estimated by cross_validation for 3755 samples. best for MPF class 4, 5 class pca95
#endif
		mqdf_param.remainder = log_lamda;
		//lj end
		Rect rect(0, 0, 1, mqdf_param.principle_dim);
		cv::Mat principle_eigenvalue = w(rect).clone();
		principle_eigenvalue.convertTo(principle_eigenvalue, CV_32F);
		mqdf_param.principle_eigenvalue = principle_eigenvalue;
		rect.x = 0;
		rect.y = 0;
		rect.height = u.rows;
		rect.width = mqdf_param.principle_dim;
		cv::Mat principle_eigenvector = u(rect).clone();
		principle_eigenvector.convertTo(principle_eigenvector, CV_32F);
		mqdf_param.principle_eigenvector = principle_eigenvector;
#ifdef CROSS_VALIDATION
		character_mqdf_param_.insert(std::make_pair(character, mqdf_param));
#else
		writeTemplate(character, mqdf_param, path);
#endif
		return 0;
	}