Project

General

Profile

Statistics
| Branch: | Revision:

root / src / scave / octaveexport.cc @ cbd2c699

History | View | Annotate | Download (4.65 KB)

1 01873262 Georg Kunz
//=========================================================================
2
//  OCTAVEEXPORT.CC - part of
3
//                  OMNeT++/OMNEST
4
//           Discrete System Simulation in C++
5
//
6
//  Author: Tamas Borbely
7
//
8
//=========================================================================
9
10
/*--------------------------------------------------------------*
11
  Copyright (C) 2006-2008 OpenSim Ltd.
12

13
  This file is distributed WITHOUT ANY WARRANTY. See the file
14
  `license' for details on this and other legal matters.
15
*--------------------------------------------------------------*/
16
17
#include <locale.h>
18
#include "opp_ctype.h"
19
#include "octaveexport.h"
20
21
#ifdef CHECK
22
#undef CHECK
23
#endif
24
25
USING_NAMESPACE
26
#define CHECK(fprintf)    if (fprintf<0) throw opp_runtime_error("Cannot write file `%s'", fileName.c_str())
27
28
29
OctaveExport::OctaveExport(const char *fileName)
30
{
31
    f = NULL;
32
    this->prec = DEFAULT_PRECISION;
33
    this->fileName = fileName;
34
}
35
36
OctaveExport::~OctaveExport()
37
{
38
    close();
39
}
40
41
void OctaveExport::openFileIfNeeded()
42
{
43
    if (!f)
44
    {
45
        // note: we have to open the file in binary mode, because on Windows,
46
        // cygwin-based Octave chokes on CR-LF...
47
        f = fopen(fileName.c_str(), "wb");
48
        if (!f)
49
            throw opp_runtime_error("cannot open `%s' for write", fileName.c_str());
50
51
        setlocale(LC_NUMERIC, "C");
52
        // print file header
53
        CHECK(fprintf(f,"# Created by OMNeT++/OMNEST scavetool\n"));
54
    }
55
}
56
57
void OctaveExport::close()
58
{
59
    if (f)
60
    {
61
        // close the file
62
        fclose(f);
63
        f = NULL;
64
    }
65
}
66
67
std::string OctaveExport::makeUniqueName(const char *nameHint)
68
{
69
    // first, process the name. Only alphanumeric chars are allowed, others
70
    // are replaced with underscore
71
    std::string name = nameHint;
72
    for (int i=0; i<(int)name.length(); i++)
73
        if (!opp_isalnum(name[i]))
74
            name[i] = '_';
75
76
    // check if it's already unique
77
    std::set<std::string>::const_iterator it = savedVars.find(name);
78
    if (it == savedVars.end())
79
        return name;
80
81
    // try appending "_1", "_2", etc until it becomes unique
82
    for (int i=1; i>0; i++)
83
    {
84
        char suffix[32];
85
        sprintf(suffix,"_%d", i);
86
        std::string newName = name + suffix;
87
88
        std::set<std::string>::const_iterator it = savedVars.find(newName);
89
        if (it == savedVars.end())
90
            return newName;
91
    }
92
    throw opp_runtime_error("banged head against the sky");
93
}
94
95
void OctaveExport::writeMatrixHeader(const char *name, int rows, int columns)
96
{
97
    savedVars.insert(name);
98
99
    CHECK(fprintf(f,"# name: %s\n"
100
                    "# type: matrix\n"
101
                    "# rows: %d\n"
102
                    "# columns: %d\n",
103
                    name, rows, columns));
104
}
105
106
void OctaveExport::writeString(const char *name, const char *value)
107
{
108
    CHECK(fprintf(f,"# name: %s\n"
109
                    "# type: string\n"
110
                    "# elements: 1\n"
111
                    "# length: %d\n",
112
                    name, strlen(value)));
113
    CHECK(fprintf(f,"%s\n", value));
114
}
115
116
void OctaveExport::writeDescription(const char *name, const char *description)
117
{
118
    writeString((std::string(name)+"_descr").c_str(), description);
119
}
120
121
void OctaveExport::saveVector(const char *name, const char *description,
122
                              const XYArray *vec, int startIndex, int endIndex)
123
{
124
    // write header
125
    openFileIfNeeded();
126
    if (description)
127
        writeDescription(name, description);
128
    if (endIndex==-1)
129
        endIndex = vec->length();
130
    writeMatrixHeader(name, endIndex-startIndex, 2);
131
132
    // write data
133
    for (int i=startIndex; i<endIndex; i++)
134
        CHECK(fprintf(f," %.*g %.*g\n", prec, vec->getX(i), prec, vec->getY(i)));
135
}
136
137
void OctaveExport::saveVectorX(const char *name, const char *description,
138
                               const XYArray *vec, int startIndex, int endIndex)
139
{
140
    // write header
141
    openFileIfNeeded();
142
    if (description)
143
        writeDescription(name, description);
144
    if (endIndex==-1)
145
        endIndex = vec->length();
146
    writeMatrixHeader(name, endIndex-startIndex, 1);
147
148
    // write data
149
    for (int i=startIndex; i<endIndex; i++)
150
        CHECK(fprintf(f," %.*g\n", prec, vec->getX(i)));
151
}
152
153
void OctaveExport::saveVectorY(const char *name, const char *description,
154
                               const XYArray *vec, int startIndex, int endIndex)
155
{
156
    // write header
157
    openFileIfNeeded();
158
    if (description)
159
        writeDescription(name, description);
160
    if (endIndex==-1)
161
        endIndex = vec->length();
162
    writeMatrixHeader(name, endIndex-startIndex, 1);
163
164
    // write data
165
    for (int i=startIndex; i<endIndex; i++)
166
        CHECK(fprintf(f," %.*g\n", prec, vec->getY(i)));
167
}