#!/usr/bin/perl
#
# $Id$
#
# This file is part of the Virtual Leaf.
#
# The Virtual Leaf is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# The Virtual Leaf is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with the Virtual Leaf. If not, see .
#
# Copyright 2010 Roeland Merks.
#
# input: parameter file + types
# format: par_name = default_value/type
# output: C++ source code of class Parameter
# and sample parameter file
%funname = (
"double" => fgetpar,
"float" => fgetpar,
"int" => igetpar,
"bool" => bgetpar,
"char *" => sgetpar,
"string" => sgetpar,
"double *" => dgetparlist,
);
%typetrans = (
"double" => "double",
"float" => "double",
"int" => "int",
"bool" => "bool",
"char *" => "char *",
"string" => "char *",
"directory" => "char *",
"doublelist" => "double *",
"label" => "label",
"title" => "title",
);
open parfile,"<$ARGV[0]";
open cppfile,">parameter.cpp";
$i=0;
while () {
#ignore comments
if (/^#/) {
next;
}
@line=split(/=/);
#ignore empty lines
if ($#line<1) {
next;
}
$param[$i]=$line[0];
$value_type=$line[1];
@typel=split(/ \/ /,$value_type);
$value[$i] = $typel[0];
$type[$i] = $typel[1];
#get rid of spaces
$param[$i] =~ s/ //g;
$value[$i] =~ s/ //g;
$type[$i] =~ s/ //g;
$type[$i] =~s/\n//g;
$convtype[$i]=$typetrans{$type[$i]};
if ($convtype[$i] eq "char *") {
$value[$i] = "\"$value[$i]\"";
}
#print cppfile "param = $param, value = $value, type = $type\n";
$i++;
}
$lines=$i;
print cppfile <
#include
#include
#include
#include
#include
#include "output.h"
#include "parse.h"
#include "xmlwrite.h"
#include "warning.h"
#include
#include
#include
using namespace std;
Parameter::Parameter() {
END_HEADER
for ($i=0;$i<$lines;$i++) {
if ($convtype[$i] ne "label" && $convtype[$i] ne "title") {
if ($convtype[$i] eq "char *") {
print cppfile " $param[$i] = strdup($value[$i]);\n";
if ($param[$i] eq "datadir") {
print cppfile " datadir = AppendHomeDirIfPathRelative(datadir);\n";
}
} else {
if ($convtype[$i] eq "double *") {
#comma separated list expected
@paramlist = split(/,/, $value[$i]);
$length = $#paramlist+1;
print cppfile " $param[$i] = new double\[$length\];\n";
for ($j=0;$j<=$#paramlist;$j++) {
print cppfile " $param[$i]\[$j\] = $paramlist[$j];\n";
}
} else {
print cppfile " $param[$i] = $value[$i];\n";
}
}
}
}
print cppfile < valarray) {\n";
print cppfile;
for ($i=0;$i<$lines;$i++) {
if ($convtype[$i] eq "double *") {
@paramlist = split(/,/,$value[$i]);
print cppfile "if (!strcmp(namec, \"$param[$i]\")) {\n";
print cppfile " int i=0;\n";
print cppfile " vector::const_iterator v=valarray.begin();\n";
print cppfile " while (v!=valarray.end() && i <= $#paramlist ) {\n";
print cppfile " $param[$i]\[i++\]=*(v++);\n";
print cppfile " }\n";
print cppfile "}\n";
}
}
print cppfile "}\n";
print cppfile <parameter.h";
print hfile <
#include
#include
class Parameter {
public:
Parameter();
~Parameter();
void CleanUp(void);
void Read(const char *filename);
void Write(ostream &os) const;
void XMLAdd(xmlNode *root) const;
void XMLRead(xmlNode *root);
void AssignValToPar(const char *namec, const char *valc);
void AssignValArrayToPar(const char *namec, vector valarray);
END_HEADER2
for ($i=0;$i<$lines;$i++) {
if ($convtype[$i] ne "label" && $convtype[$i] ne "title") {
print hfile " $convtype[$i] $param[$i];\n";
}
}
print hfile <default.par";
for ($i=0;$i<$lines;$i++) {
if ($type[$i] ne "title" && $type[$i] ne "label") {
$value[$i] =~ s/\"//g;
print parfile " $param[$i] = $value[$i]\n";
}
}
# finis