~ubuntu-branches/ubuntu/utopic/sip-tester/utopic

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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*

 *  This program 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 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 General 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
 *
 *  Author : Richard GAYRAUD - 04 Nov 2003
 *           From Hewlett Packard Company.
 *	     Charles P. Wright from IBM Research
 */

#include "sipp.hpp"
#include "screen.hpp"
#include "stat.hpp"
#include "infile.hpp"
#include <iostream>
#include <assert.h>

/* Read MAX_CHAR_BUFFER_SIZE size lines from the "fileName" and populate it in
 * the fileContents vector. Each line should be terminated with a '\n'
 */
FileContents::FileContents(const char *fileName) {
  ifstream *inFile    = new ifstream(fileName);
  char      line[MAX_CHAR_BUFFER_SIZE];
  int virtualLines = 0;

  if (!inFile->good()) {
    ERROR("Unable to open file %s", fileName);
  }

  this->fileName = fileName;

  realLinesInFile = lineCounter = numLinesInFile = 0;
  /* Initialize printf info. */
  printfFile = false;
  printfOffset = 0;
  printfMultiple = 1;
  

  line[0] = '\0';
  inFile->getline(line, MAX_CHAR_BUFFER_SIZE);

  if (NULL != strstr(line, "RANDOM")) {
      usage = InputFileRandomOrder;
  } else if (NULL != strstr(line, "SEQUENTIAL")) {
      usage = InputFileSequentialOrder;
  } else if (NULL != strstr(line, "USER")) {
      usage = InputFileUser;
  } else {
      ERROR("Unknown file type (valid values are RANDOM, SEQUENTIAL, and USER) for %s:%s\n", fileName, line);
  }

  char *useprintf;
  if ((useprintf = strstr(line, "PRINTF"))) {
    /* We are going to operate in printf mode, which uses the line as a format
     * string for printf with the line number. */
    useprintf += strlen("PRINTF");
    if (*useprintf != '=') {
	ERROR("Invalid file printf specification (requires =) for %s:%s\n", fileName, line);
    }
    useprintf++;
    char *endptr;
    virtualLines = strtoul(useprintf, &endptr, 0);
    if (*endptr && *endptr != '\r' && *endptr != '\n' && *endptr != ',') {
      ERROR("Invalid file printf specification for (invalid end character '%c') %s:%s\n", *endptr, fileName, line);
    }
    if (virtualLines == 0) {
      ERROR("A printf file must have at least one virtual line %s:%s\n", fileName, line);
    }
    printfFile = true;
  }

  if ((useprintf = strstr(line, "PRINTFOFFSET"))) {
    useprintf += strlen("PRINTFOFFSET");
    if (*useprintf != '=') {
	ERROR("Invalid file PRINTFOFFSET specification (requires =) for %s:%s\n", fileName, line);
    }
    useprintf++;
    char *endptr;
    printfOffset = strtoul(useprintf, &endptr, 0);
    if (*endptr && *endptr != '\n' && *endptr != ',') {
      ERROR("Invalid PRINTFOFFSET specification for (invalid end character '%c') %s:%s\n", *endptr, fileName, line);
    }
  }

  if ((useprintf = strstr(line, "PRINTFMULTIPLE"))) {
    useprintf += strlen("PRINTFMULTIPLE");
    if (*useprintf != '=') {
	ERROR("Invalid PRINTFMULTIPLE specification (requires =) for %s:%s\n", fileName, line);
    }
    useprintf++;
    char *endptr;
    printfMultiple = strtoul(useprintf, &endptr, 0);
    if (*endptr && *endptr != '\n' && *endptr != ',') {
      ERROR("Invalid PRINTFOFFSET specification for (invalid end character '%c') %s:%s\n", *endptr, fileName, line);
    }
  }

  while (!inFile->eof()) {
    line[0] = '\0';
    inFile->getline(line, MAX_CHAR_BUFFER_SIZE);
    if (line[0]) {
      if ('#' != line[0]) {
        fileLines.push_back(line);
        realLinesInFile++; /* this counts number of valid data lines */
      }
    } else {
      break;
    }
  }

  if (realLinesInFile == 0) {
    ERROR("Input file has zero lines: %s\n", fileName);
  }

  if (printfFile) {
    numLinesInFile = virtualLines;
  } else {
    numLinesInFile = realLinesInFile;
  }

  delete inFile;

  indexMap = NULL;
  indexField = -1;
}

int FileContents::getLine(int line, char *dest, int len) {
  if (printfFile) {
    line %= realLinesInFile;
  }
  return snprintf(dest, len, "%s", fileLines[line].c_str());
}

int FileContents::getField(int lineNum, int field, char *dest, int len) {
  int curfield = field;
  int curline = lineNum;

  dest[0] = '\0';
  if (lineNum >= numLinesInFile) {
    return 0;
  }

  if (printfFile) {
	curline %= realLinesInFile;
  }
  const string & line = fileLines[curline];

  size_t pos(0), oldpos(0);

  do {
    oldpos = pos;
    size_t localpos = line.find(';', oldpos);

    if (localpos != string::npos) {
      pos = localpos + 1;
    } else {
      pos = localpos;
      break;
    }

    if (curfield == 0) {
      break;
    }

    curfield --;
  } while (oldpos != string::npos);


  if (curfield) {
      WARNING("Field %d not found in the file %s", field, fileName);
      return 0;
  }


  if (string::npos == oldpos) {
    return 0;
  }

  if (string::npos != pos) {
    // should not be decremented for fieldN
    pos -= (oldpos + 1);
  }

  string x = line.substr(oldpos, pos);
  if (x.length()) {
    if (printfFile) {
      const char *s = x.c_str();
      int l = strlen(s);
      int copied = 0;
      for (int i = 0; i < l; i++) {
	if (s[i] == '%') {
	  if (s[i + 1] == '%') {
	    dest[copied++] = s[i];
	  } else {
	    const char *format = s + i;
	    i++;
	    while (s[i] != 'd') {
	      if (i == l) {
		ERROR("Invalid printf injection field (ran off end of line): %s", s);
	      }
	      if (!(isdigit(s[i]) || s[i] == '.' || s[i] == '-')) {
		ERROR("Invalid printf injection field (only decimal values allowed '%c'): %s", s[i], s);
	      }
	      i++;
	    }
	    assert(s[i] == 'd');
	    char *tmp = (char *)malloc(s + i + 2 - format);
	    if (!tmp) {
	      ERROR("Out of memory!\n");
	    }
            memcpy(tmp, format, s + i + 1 - format);
	    tmp[s + i + 1 - format] = '\0';
	    copied += sprintf(dest + copied, tmp, printfOffset + (lineNum * printfMultiple));
	    free(tmp);
	  }
	} else {
	  dest[copied++] = s[i];
	}
      }
      dest[copied] = '\0';
      return copied;
    } else {
      return snprintf(dest, len, "%s", x.c_str());
    }
  } else {
    return 0;
  }
}

int FileContents::numLines() {
  return numLinesInFile;
}

int FileContents::nextLine(int userId) {
  switch(usage) {
    case InputFileRandomOrder:
      return rand() % numLinesInFile;
    case InputFileSequentialOrder:
      {
	int ret = lineCounter;
	lineCounter = (lineCounter + 1) % numLinesInFile;
	return ret;
      }
    case InputFileUser:
      if (userId == 0) {
	return -1;
      }
      if ((userId  - 1) >= numLinesInFile) {
	ERROR("%s has only %d lines, yet user %d was requested.", fileName, numLinesInFile, userId);
      }
      return userId - 1;
    default:
      ERROR("Internal error: unknown file usage mode!");
      return -1;
  }
}

void FileContents::dump(void)
{
    WARNING("Line choosing strategy is [%s]. m_counter [%d] numLinesInFile [%d] realLinesInFile [%d]",
               usage == InputFileSequentialOrder ? "SEQUENTIAL" :
		usage == InputFileRandomOrder ? "RANDOM" :
		usage == InputFileUser ? "USER" : "UNKNOWN",
               lineCounter, numLinesInFile, realLinesInFile);

    for (int i = 0; i < realLinesInFile && fileLines[i][0]; i++) {
        WARNING("%s:%d reads [%s]", fileName, i, fileLines[i].c_str());
    }
}

void FileContents::index(int field) {
  this->indexField = field;

  indexMap = new str_int_map;
  for (int line = 0; line < numLines(); line++) {
    reIndex(line);
  }
}

int FileContents::lookup(char *key) {
  if (indexField == -1) {
    ERROR("Invalid Index File: %s", fileName);
  }
  if (!indexMap) {
    ERROR("Invalid Index File: %s", fileName);
  }

  str_int_map::iterator index_it = indexMap->find(key);
  if (index_it == indexMap->end()) {
    return -1;
  }
  return index_it->second;
}


void FileContents::insert(char *value) {
  if (printfFile) {
    ERROR("Can not insert or replace into a printf file: %s", fileName);
  }
  fileLines.push_back(value);
  realLinesInFile++;
  numLinesInFile++;
  if (indexField != -1) {
    reIndex(realLinesInFile - 1);
  }
  char line[1024];
  getLine(realLinesInFile - 1, line, sizeof(line));
  char tmp[1024];
  getField(realLinesInFile - 1, 0, tmp, sizeof(tmp));
}

void FileContents::replace(int line, char *value) {
  if (printfFile) {
    ERROR("Can not insert or replace into a printf file: %s", fileName);
  }
  if (line >= realLinesInFile || line < 0) {
    ERROR("Invalid line number (%d) for file: %s (%d lines)", line, fileName, realLinesInFile);
  }
  deIndex(line);
  fileLines[line] = value;
  reIndex(line);
}

void FileContents::reIndex(int line) {
  if (indexField == -1) {
    return;
  }
  assert(line >= 0);
  assert(line < realLinesInFile);

  char tmp[SIPP_MAX_MSG_SIZE];
  getField(line, indexField, tmp, SIPP_MAX_MSG_SIZE);
  str_int_map::iterator index_it = indexMap->find(str_int_map::key_type(tmp));
  if (index_it != indexMap->end()) {
    indexMap->erase(index_it);
  }
  indexMap->insert(pair<str_int_map::key_type,int>(str_int_map::key_type(tmp), line));
}

void FileContents::deIndex(int line) {
  if (indexField == -1) {
    return;
  }
  assert(line >= 0);
  assert(line < realLinesInFile);

  char tmp[SIPP_MAX_MSG_SIZE];
  getField(line, indexField, tmp, SIPP_MAX_MSG_SIZE);
  str_int_map::iterator index_it = indexMap->find(str_int_map::key_type(tmp));
  if (index_it != indexMap->end()) {
    if (index_it->second == line) {
      indexMap->erase(index_it);
    }
  }
}