~ubuntu-branches/ubuntu/raring/voxbo/raring

« back to all changes in this revision

Viewing changes to dbutil/initScoreNames.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Hanke
  • Date: 2010-06-06 11:33:11 UTC
  • Revision ID: james.westby@ubuntu.com-20100606113311-v3c13imdkkd5n7ae
Tags: upstream-1.8.5~svn1172
ImportĀ upstreamĀ versionĀ 1.8.5~svn1172

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* initScoreNames.cpp: populate patient score (attribute) names from plain text file. 
 
2
 * NOTE: This program updates database WITHOUT db environment! */
 
3
 
 
4
#include "tokenlist.h"
 
5
#include "myDB.h"
 
6
#include "bdb_tab.h"
 
7
#include "db_util.h"
 
8
#include <iostream>
 
9
#include <fstream>
 
10
#include <vector>
 
11
#include <string>
 
12
#include <cstring>
 
13
 
 
14
using namespace std;
 
15
 
 
16
void addRecords(char*, myDB&);
 
17
 
 
18
/* Main function */
 
19
int main(int argc, char *argv[])
 
20
{
 
21
  if (argc != 3) {
 
22
    printf("Usage: initScoreNames <txt_filename> <db_dir>\n");
 
23
    exit(0);
 
24
  }
 
25
 
 
26
  const string inputFile = argv[1];
 
27
  if (!vb_fileexists(inputFile)) {  // Make sure input file exists
 
28
    cout << "File not exists: " << inputFile << endl;
 
29
    exit(1);
 
30
  }
 
31
 
 
32
  string dirname = argv[2];
 
33
  if (!vb_direxists(dirname)) {  // Make sure db env dir exists
 
34
    cout << "DB env directory not exists: " << dirname << endl;
 
35
    exit(1);
 
36
  }
 
37
 
 
38
  if (dirname[dirname.size() - 1] != '/')
 
39
    dirname.append("/");  // don't let the trailing '/' mess up db file path
 
40
  string dbname = "scorenames.db";
 
41
  // Remove original db file if it already exists in env dir
 
42
  if (vb_fileexists(dirname + dbname)) {
 
43
    if (!rmDB(dirname + dbname)) {  
 
44
      cout << "Faild to remove original db file: " << dbname << endl;
 
45
      exit(1);
 
46
    }
 
47
  }
 
48
 
 
49
  // open db in non-transaction mode
 
50
  myDB newDB(dirname + dbname, myDB::cmp_lex);
 
51
  if (!newDB.isOpen()) {
 
52
    cout << "db open error: " << dirname + dbname << endl;
 
53
    exit(1);
 
54
  }
 
55
 
 
56
  addRecords(argv[1], newDB);
 
57
  newDB.close();
 
58
 
 
59
  return 0;
 
60
}
 
61
 
 
62
 
 
63
// Add records into db
 
64
void addRecords(char* txt_file, myDB& inputDB)
 
65
{
 
66
  FILE* ifp = fopen(txt_file, "r");
 
67
  // Make sure file is readable
 
68
  if (!ifp) {
 
69
    printf("File read error: %s\n", txt_file);
 
70
    return;
 
71
  }
 
72
 
 
73
  const int MAXLENGTH = 5000;
 
74
  char line[MAXLENGTH];
 
75
  int lineNo = 0;
 
76
  while (fgets(line, MAXLENGTH, ifp)) {
 
77
    lineNo++;
 
78
    // if a line starts with "#", ignore the line
 
79
    if (strchr("#\n", line[0]))
 
80
      continue;
 
81
 
 
82
    // remove the trailing "return" key
 
83
    stripchars(line, "\n");
 
84
    const string tmpStr(line);
 
85
    tokenlist myRow;
 
86
    myRow.SetQuoteChars("\"");
 
87
    myRow.SetSeparator(" ");
 
88
    myRow.ParseLine(tmpStr);
 
89
 
 
90
    // Print out error message if the line is not blank but the number of field is not 7
 
91
    if (myRow.size() < 2) {
 
92
      printf("%s line #%d: the number of fields is %d\n", txt_file, lineNo, myRow.size());
 
93
      continue;
 
94
    }
 
95
 
 
96
    string scoreName = myRow[0];
 
97
    DBscorename sData;
 
98
    sData.name = scoreName;
 
99
    sData.screen_name = scoreName;
 
100
    sData.datatype = myRow[1];
 
101
 
 
102
    for (int i = 2; i < myRow.size(); i++) {
 
103
      string flag_str = myRow[i];
 
104
      sData.flags[flag_str] = "true";
 
105
    }
 
106
 
 
107
    if (addScoreName(inputDB, NULL, sData)) {
 
108
      printf("%s line #%d: failed to add %s into score db\n", txt_file, lineNo, scoreName.c_str());
 
109
    }
 
110
    inputDB.getDb().sync(0);
 
111
   } 
 
112
 
 
113
  fclose(ifp);
 
114
}
 
115