Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
95 Kevin 1
// This program generates test files for external sorting algorithms.
2
// Output files can be of any multiple of the 4096 byte block size.
3
// The data records are logically a two byte (short int) key value followed
4
// by a two byte (short int) data value.
5
// The values output by this program can either be "binary" or "ascii".
6
// "Binary" files have key and data values each in the range 1 to 30,000.
7
// "Ascii" files have their data and key values selected so that they are
8
// easy to read and test. The data value prints in ascii as two spaces.
9
// The key value prints in ascii as a space and then a capital letter (A-Z).
10
// This makes it simple to tell if the sorting algorithm is working.
11
 
12
#include <iostream>
13
#include <cstdlib>
14
#include <fstream>
15
#include <string>
16
#include <cstring>
17
 
18
using std::cout;
19
using std::endl;
20
using std::string;
21
using std::ostream;
22
using std::fstream;
23
using std::ios;
24
 
25
// Random number generator functions
26
 
27
inline void Randomize() // Seed the generator
28
  { srand(1); }
29
 
30
// Return a random value in range 0 to n-1
31
inline int Random(int n)
32
  { return rand() % (n); }
33
 
34
// A block is 4096 bytes, or 1024 logical records
35
#define NumRec 1024
36
 
37
typedef int E;
38
 
39
int main(int argc, char** argv) {
40
  int filesize;
41
  E Array[NumRec];
42
  int i, j;
43
  fstream myfile;
44
  bool Ascii;        // True if ASCII option, false if binary option.
45
 
46
  if (argc < 4) {
47
    cout << "Usage: genfile <option> <filename> <size>\n";
48
    cout << "Size is measured in blocks of 4096 bytes.\n";
49
    cout << "Options must be '-a' for ASCII, or '-b' for binary.\n";
50
    cout << "ASCII files have a data value of 8224 (prints as 2 spaces)\n";
51
    cout << "and a key value between 8257 and 8282 (prints as a space\n";
52
    cout << "and a letter).\n";
53
    cout << "Binary files have key and data values between 1 and 30000\n";
54
    exit(-1);
55
  }
56
 
57
  if (!strcmp(argv[1], "-a"))
58
    Ascii = true;
59
  else if (!strcmp(argv[1], "-b"))
60
    Ascii = false;
61
  else {
62
    cout << "Illegal option '" << argv[1] << "'\n";
63
    cout << "Usage: genfile <option> <filename> <size>\n";
64
    exit(-1);
65
  }
66
 
67
  myfile.open(argv[2], ios::out | ios::binary);
68
  if (!myfile) {
69
    cout << "Unable to open file :" << argv[2] << ":\n";
70
    exit(-1);
71
  }
72
  filesize = atoi(argv[3]);
73
 
74
  Randomize();
75
  for (i=0; i<filesize; i++) { // For each block
76
    for (j=0; j<NumRec; j++)   // For each record
77
      if (Ascii)
78
        // A record has a 2-byte key that prints as <space><letter>
79
        // and a 2-byte value that prints as <space><space>
80
        Array[j] = (8224 << 16) + Random(26) + 0x2041;
81
      else // Keys and values in range 1 - 30,000.
82
        Array[j] = ((Random(29999) + 1) << 16) + ((Random(29999) + 1));
83
    myfile.write((char*)Array, sizeof(E) * NumRec);
84
  }
85
  return 0;
86
}