1
/* initScoreNames.cpp: populate patient score (attribute) names from plain text file.
2
* NOTE: This program updates database WITHOUT db environment! */
16
void addRecords(char*, myDB&);
19
int main(int argc, char *argv[])
22
printf("Usage: initScoreNames <txt_filename> <db_dir>\n");
26
const string inputFile = argv[1];
27
if (!vb_fileexists(inputFile)) { // Make sure input file exists
28
cout << "File not exists: " << inputFile << endl;
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;
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;
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;
56
addRecords(argv[1], newDB);
63
// Add records into db
64
void addRecords(char* txt_file, myDB& inputDB)
66
FILE* ifp = fopen(txt_file, "r");
67
// Make sure file is readable
69
printf("File read error: %s\n", txt_file);
73
const int MAXLENGTH = 5000;
76
while (fgets(line, MAXLENGTH, ifp)) {
78
// if a line starts with "#", ignore the line
79
if (strchr("#\n", line[0]))
82
// remove the trailing "return" key
83
stripchars(line, "\n");
84
const string tmpStr(line);
86
myRow.SetQuoteChars("\"");
87
myRow.SetSeparator(" ");
88
myRow.ParseLine(tmpStr);
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());
96
string scoreName = myRow[0];
98
sData.name = scoreName;
99
sData.screen_name = scoreName;
100
sData.datatype = myRow[1];
102
for (int i = 2; i < myRow.size(); i++) {
103
string flag_str = myRow[i];
104
sData.flags[flag_str] = "true";
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());
110
inputDB.getDb().sync(0);