Changeset 623
- Timestamp:
- 1999-09-23T17:05:37+12:00 (25 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gsdl/src/txt2db/txt2db.cpp
r536 r623 49 49 50 50 void print_usage (char *program_name) { 51 51 cerr << "usage: " << program_name << " database-name\n\n"; 52 52 } 53 53 54 54 55 55 int main (int argc, char *argv[]) { 56 int block_size = 0; 57 GDBM_FILE dbf; 58 char c; 59 text_t key; 60 text_t value; 61 text_t tmp; 62 int num_dashes = 0; 63 64 // sanity check 65 if (argc != 2) { 66 print_usage (argv[0]); 67 exit (0); 68 } 69 70 // open the database 71 dbf = gdbm_open (argv[1], block_size, GDBM_NEWDB, 00664, NULL); 72 if (dbf == NULL) { 73 cerr << "couldn't create " << argv[1] << "\n"; 74 exit (0); 75 } 76 77 cin.get(c); 78 while (!cin.eof()) { 79 num_dashes = 0; 80 key = ""; 81 value = ""; 82 83 // look for [key]\n 84 while (!cin.eof() && c != '[') cin.get(c); 85 if (!cin.eof()) cin.get(c); // skip [ 86 while (!cin.eof() && c != ']') { 87 key.push_back ((unsigned char)c); 88 cin.get(c); 89 } 90 if (!cin.eof()) cin.get(c); // skip ] 91 while (!cin.eof() && (c == '\n' || c == 'r')) cin.get(c); 92 93 // look for 70 dashes 94 tmp = ""; 95 while (!cin.eof() && (num_dashes < 70)) { 96 if (c == '\n' || c == '\r') { 97 tmp.push_back ((unsigned char)c); 98 num_dashes = 0; 99 100 } else if (c == '-') { 101 tmp.push_back ((unsigned char)c); 102 num_dashes++; 103 104 } else { 105 value += tmp; 106 value.push_back ((unsigned char)c); 107 tmp = ""; 108 num_dashes = 0; 109 } 110 cin.get(c); 111 } 112 113 // if the key is not an empty string store this key-value pair 114 if (!key.empty()) { 115 // convert key to a datum datatype 116 datum key_data; 117 key_data.dptr = key.getcstr(); 118 if (key_data.dptr == NULL) { 119 cerr << "NULL key_data.dptr\n"; 120 exit (0); 121 } 122 key_data.dsize = strlen(key_data.dptr); 123 124 // convert value to a datum datatype 125 datum value_data; 126 value_data.dptr = value.getcstr(); 127 if (value_data.dptr == NULL) { 128 cerr << "NULL value_data.dptr\n"; 129 exit (0); 130 } 131 value_data.dsize = strlen(value_data.dptr); 132 133 // store the value 134 if (gdbm_store (dbf, key_data, value_data, GDBM_REPLACE) < 0) { 135 cerr << "gdbm_store returned an error\n"; 136 exit (0); 137 } 138 139 delete key_data.dptr; 140 delete value_data.dptr; 141 } 142 } 143 144 145 gdbm_close (dbf); 146 147 148 return 0; 56 int block_size = 0; 57 GDBM_FILE dbf; 58 char c; 59 text_t key; 60 text_t value; 61 text_t tmp; 62 int num_dashes = 0; 63 64 // sanity check 65 if (argc != 2) { 66 print_usage (argv[0]); 67 exit (0); 68 } 69 70 // open the database 71 dbf = gdbm_open (argv[1], block_size, GDBM_NEWDB | GDBM_FAST, 00664, NULL); 72 if (dbf == NULL) { 73 cerr << "couldn't create " << argv[1] << "\n"; 74 exit (0); 75 } 76 77 cin.get(c); 78 while (!cin.eof()) { 79 num_dashes = 0; 80 key = ""; 81 value = ""; 82 83 // look for [key]\n 84 while (!cin.eof() && c != '[') cin.get(c); 85 if (!cin.eof()) cin.get(c); // skip [ 86 while (!cin.eof() && c != ']') { 87 key.push_back ((unsigned char)c); 88 cin.get(c); 89 } 90 if (!cin.eof()) cin.get(c); // skip ] 91 while (!cin.eof() && (c == '\n' || c == 'r')) cin.get(c); 92 93 // look for 70 dashes 94 tmp = ""; 95 while (!cin.eof() && (num_dashes < 70)) { 96 if (c == '\n' || c == '\r') { 97 tmp.push_back ((unsigned char)c); 98 num_dashes = 0; 99 100 } else if (c == '-') { 101 tmp.push_back ((unsigned char)c); 102 num_dashes++; 103 104 } else { 105 value += tmp; 106 value.push_back ((unsigned char)c); 107 tmp = ""; 108 num_dashes = 0; 109 } 110 cin.get(c); 111 } 112 113 // if the key is not an empty string store this key-value pair 114 if (!key.empty()) { 115 // convert key to a datum datatype 116 datum key_data; 117 key_data.dptr = key.getcstr(); 118 if (key_data.dptr == NULL) { 119 cerr << "NULL key_data.dptr\n"; 120 exit (0); 121 } 122 key_data.dsize = strlen(key_data.dptr); 123 124 // convert value to a datum datatype 125 datum value_data; 126 value_data.dptr = value.getcstr(); 127 if (value_data.dptr == NULL) { 128 cerr << "NULL value_data.dptr\n"; 129 exit (0); 130 } 131 value_data.dsize = strlen(value_data.dptr); 132 133 // store the value 134 if (gdbm_store (dbf, key_data, value_data, GDBM_REPLACE) < 0) { 135 cerr << "gdbm_store returned an error\n"; 136 exit (0); 137 } 138 139 delete key_data.dptr; 140 delete value_data.dptr; 141 } 142 } 143 144 gdbm_close (dbf); 145 146 return 0; 149 147 }
Note:
See TracChangeset
for help on using the changeset viewer.