summaryrefslogtreecommitdiffstats
path: root/src/parameters/persistent.cpp
blob: 0711b4d7a9b883945b85097436b15bedc21108d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * Copyright 2010, Intel Corporation
 *
 * This file is part of PowerTOP
 *
 * This program file 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; version 2 of the License.
 *
 * This program 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 this program in a file named COPYING; if not, write to the
 * Free Software Foundation, Inc,
 * 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 * or just google for it.
 *
 * Authors:
 *	Arjan van de Ven <arjan@linux.intel.com>
 */
#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdlib.h>

#include "parameters.h"
#include "../measurement/measurement.h"

using namespace std;

void save_all_results(const char *filename)
{
	ofstream file;
	unsigned int i;
	struct result_bundle *bundle;
	char* pathname;

	pathname = get_param_directory(filename);

	file.open(pathname, ios::out);
	if (!file) {
		cout << _("Cannot save to file") << " " << pathname << "\n";
		return;
	}
	for (i = 0; i < past_results.size(); i++) {
		bundle = past_results[i];
		map<string, int>::iterator it;
		file << setiosflags(ios::fixed) <<  setprecision(5) << bundle->power << "\n";

		for (it = result_index.begin(); it != result_index.end(); it++) {
			file << it->first << "\t" << setprecision(5) << get_result_value(it->second, bundle) << "\n";
		}
		file << ":\n";
	}

	file.close();

}

void close_results()
{
	for (unsigned int i = 0; i < past_results.size(); i++) {
		delete past_results[i];
	}

	past_results.clear();
	return;
}

void load_results(const char *filename)
{
	ifstream file;
	char line[4096];
	char *c1;
	struct result_bundle *bundle;
	int first = 1;
	unsigned int count = 0;
	char* pathname;
	int bundle_saved = 0;

	pathname = get_param_directory(filename);

	file.open(pathname, ios::in);
	if (!file) {
		cout << _("Cannot load from file") << " " << pathname << "\n";
		return;
	}

	bundle = new struct result_bundle;

	while (file) {
		double d;
		if (first) {
			file.getline(line, 4096);
			if (strlen(line)>0) {
				sscanf(line, "%lf", &bundle->power);
				if (bundle->power < min_power)
					min_power = bundle->power;
			}
			first = 0;
			continue;
		}
		file.getline(line, 4096);
		if (strlen(line) < 3) {
			int overflow_index;

			bundle_saved = 1;
			overflow_index = 50 + (rand() % MAX_KEEP);
			if (past_results.size() >= MAX_PARAM) {
			/* memory leak, must free old one first */
				past_results[overflow_index] = bundle;
			} else {
				past_results.push_back(bundle);
			}
			bundle = new struct result_bundle;
			first = 1;
			count++;
			continue;
		}
		c1 = strchr(line, '\t');
		if (!c1)
			continue;
		*c1 = 0;
		c1++;
		sscanf(c1, "%lf", &d);
		set_result_value(line, d, bundle);
	}

	if (bundle_saved == 0)
		delete bundle;

	file.close();
	// '%i" is for count, do not translate
	fprintf(stderr, _("Loaded %i prior measurements\n"), count);
}

void save_parameters(const char *filename)
{
	ofstream file;
	char* pathname;

//	printf("result size is %i, #parameters is %i \n", (int)past_results.size(), (int)all_parameters.parameters.size());

	if (!global_power_valid())
		return;

	pathname = get_param_directory(filename);

	file.open(pathname, ios::out);
	if (!file) {
		cout << _("Cannot save to file") << " " << pathname << "\n";
		return;
	}

	map<string, int>::iterator it;

	for (it = param_index.begin(); it != param_index.end(); it++) {
		int index;
		index = it->second;
		file << it->first << "\t" << setprecision(9) << all_parameters.parameters[index] << "\n";
	}
	file.close();
}

void load_parameters(const char *filename)
{
	ifstream file;
	char line[4096];
	char *c1;
	char* pathname;

	pathname = get_param_directory(filename);

	file.open(pathname, ios::in);
	if (!file) {
		cout << _("Cannot load from file") << " " << pathname << "\n";
		cout << _("File will be loaded after taking minimum number of measurement(s) with battery only \n");
		return;
	}

	while (file) {
		double d;
		memset(line, 0, 4096);
		file.getline(line, 4095);

		c1 = strchr(line, '\t');
		if (!c1)
			continue;
		*c1 = 0;
		c1++;
		sscanf(c1, "%lf", &d);


		set_parameter_value(line, d);
	}

	file.close();
}