[go: up one dir, main page]

Menu

[r990]: / mia2 / vistaio / history.c  Maximize  Restore  History

Download this file

264 lines (215 with data), 6.7 kB

  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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
* Copyrigh (C) 2004 Max-Planck-Institute of Cognitive Neurosience
*
* The origional VISTA library is copyrighted of University of British Columbia.
* Copyright © 1993, 1994 University of British Columbia.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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 Lesser Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/* $Id: history.c 885 2006-02-27 10:13:57Z write1 $ */
/*! \file history.c
* \brief basic support for an history object
* \remark This file is part of the lipsia and via library, too.
* \author Gabi Lohmann, lohmann@cbs.mpg.de, 2004
*/
/* From the Vista library: */
#include "vistaio/vistaio.h"
/* From the standard C libaray: */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
#define snprintf _snprintf
#endif
#define MAXPARAMLENGTH 400
char*
VGetOptionValue (VOptionDescRec *option)
{
char *ret;
int n, i;
char *vp;
VDictEntry *dict;
VLong ivalue;
VDouble fvalue = 0.0;
VStringConst svalue;
ret = (char*)malloc(MAXPARAMLENGTH);
if (option->number == 0) {
n = ((VArgVector *) option->value)->number;
vp = (char *) ((VArgVector *) option->value)->vector;
} else {
n = option->number;
vp = (char *) option->value;
}
for (i = 0; i < n; i++, vp += VRepnSize (option->repn)) {
switch (option->repn) {
case VBitRepn:
ivalue = * (VBit *) vp;
goto PrintLong;
case VUByteRepn:
ivalue = * (VUByte *) vp;
goto PrintLong;
case VSByteRepn:
ivalue = * (VSByte *) vp;
goto PrintLong;
case VShortRepn:
ivalue = * (VShort *) vp;
goto PrintLong;
case VLongRepn:
ivalue = * (VLong *) vp;
PrintLong:
snprintf (ret, MAXPARAMLENGTH,"%d", ivalue);
break;
case VFloatRepn:
fvalue = * (VFloat *) vp;
goto PrintDbl;
case VDoubleRepn:
fvalue = * (VDouble *) vp;
PrintDbl:
snprintf (ret, MAXPARAMLENGTH, "%g", fvalue);
break;
case VBooleanRepn:
snprintf (ret,MAXPARAMLENGTH, "%s", * (VBoolean *) vp ? "true" : "false");
break;
case VStringRepn:
svalue = * (VString *) vp;
if (! svalue)
svalue = "(none)";
else if (option->dict &&
(dict = VLookupDictValue (option->dict, VDoubleRepn,
svalue)))
svalue = dict->keyword;
snprintf (ret, MAXPARAMLENGTH, "%s", svalue);
break;
default:
break;
}
}
return ret;
}
char*
VGetHistory(int noptions,VOptionDescRec *options,char *name)
{
int i,k;
char *history;
char *item;
char *tok, *tname, *sversion = NULL, *sname;
char cmd[]="; ";
char *ignore[] = { "in", "out", "ref", NULL };
tname = strdup(name);
tok = strtok(tname, ":");
sname = strdup(tok);
if (tok != NULL) {
tok = strtok(NULL, " ");
if (tok != NULL) {
tok = strtok(NULL, " ");
sversion = strdup(tok);
}
else VWarning("History function can not grab program version");
}
else VWarning("History function can not grab program name");
history = (char*) malloc(sizeof(char*) * MAXPARAMLENGTH);
history[0]='\0';
strncat(history, sname, strlen(sname));
strncat(history, " ", 1);
strncat(history, sversion, strlen(sversion));
strncat(history, cmd, strlen(cmd));
for (i = 0; i < noptions; i++, options++) {
for (k=0; ignore[k] != NULL; k++) {
if (strncmp(options->keyword, ignore[k], strlen(ignore[k])) == 0)
goto out;
}
item = VGetOptionValue (options);
strncat(history, "-", 1);
strncat(history, options->keyword, strlen(options->keyword));
strncat(history, " ", 1);
strncat(history, item, strlen(item));
if (i+1<noptions)
strncat(history, " ", 1);
out:
/* cannot happen */
break;
}
return history;
}
VAttrList
VReadHistory(VAttrList *list)
{
int i = 0;
VStringConst temponame;
VAttrListPosn posn;
VAttrList history_list=NULL;
char *temptok, history[]="history";
VString str;
for (VLastAttr((*list),&posn);VAttrExists(&posn);VPrevAttr(&posn)) {
if (strncmp(VGetAttrName(&posn), history, strlen(history)) != 0 )
continue;
/* old history format by SH */
if (VGetAttrRepn(&posn) == VStringRepn ) {
if (history_list==NULL) history_list = VCreateAttrList();
VGetAttrValue(&posn, NULL, VStringRepn, &str);
temptok=strtok(str, " ");
temponame = (VStringConst)strdup(temptok);
temptok = strtok(NULL, "\0");
VPrependAttr(history_list, temponame, NULL, VStringRepn, temptok);
i++;
}
/* new history format */
if (VGetAttrRepn(&posn) == VAttrListRepn ) {
if (i>0) VError("type mismatch while reading history");
VGetAttrValue(&posn, NULL, VAttrListRepn, &history_list);
break;
}
}
/* Warning */
if (history_list == NULL)
VWarning("VReadHistory: No history items found");
return history_list;
}
void
VPrependHistory(int noptions,VOptionDescRec *options,char *name,VAttrList *list)
{
char *tok;
char *newhistory;
VStringConst oname;
/* Generate the new history entry */
if ((newhistory = VGetHistory(noptions,options,name)) == NULL)
VError("Error while building history string\n");
tok = strtok(newhistory, " ");
oname = (VStringConst)strdup(tok);
tok = strtok(NULL, "\0");
/* Prepend history list */
if ((*list) == NULL) (*list) = VCreateAttrList();
VPrependAttr( (*list) ,oname, NULL, VStringRepn, tok);
}
void
VHistory(int noptions,VOptionDescRec *options,char *name,VAttrList *in_list,VAttrList *out_list)
{
VAttrList history_list=NULL;
VAttrListPosn posn;
//VBoolean sw=FALSE;
char history[]="history";
//VString str;
/* Read history from list */
history_list = VReadHistory(in_list);
/* Prepend new history entry */
VPrependHistory(noptions,options,name,&history_list);
/* DELETE ANY history attributes in dest */
for (VLastAttr((*out_list),&posn);VAttrExists(&posn);VPrevAttr(&posn)) {
if (strncmp(VGetAttrName(&posn), history, strlen(history)) == 0 )
VDeleteAttr(&posn);
}
/* Prepend history in dest */
VPrependAttr( (*out_list),history,NULL,VAttrListRepn,history_list);
}