[sword-svn] r1830 - trunk/utilities

dglassey at crosswire.org dglassey at crosswire.org
Fri Jun 10 09:39:09 MST 2005


Author: dglassey
Date: 2005-06-10 09:39:09 -0700 (Fri, 10 Jun 2005)
New Revision: 1830

Modified:
   trunk/utilities/imp2ld.cpp
Log:
Change to using std::string, fstream and vector



Modified: trunk/utilities/imp2ld.cpp
===================================================================
--- trunk/utilities/imp2ld.cpp	2005-06-10 16:25:38 UTC (rev 1829)
+++ trunk/utilities/imp2ld.cpp	2005-06-10 16:39:09 UTC (rev 1830)
@@ -1,22 +1,14 @@
-#include <ctype.h>
-#include <stdio.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <stdlib.h>
-
-#ifndef __GNUC__
-#include <io.h>
-#else
-#include <unistd.h>
-#endif
-
 #include <iostream>
 #include <string>
+#include <vector>
+#include <fstream>
 #include <rawld.h>
 #include <rawld4.h>
 #include <zld.h>
 #include <zipcomprs.h>
 
+using std::string;
+
 #ifndef NO_SWORD_NAMESPACE
 using sword::zLD;
 using sword::ZipCompress;
@@ -25,34 +17,19 @@
 using sword::SWKey;
 #endif
 
-int readline(FILE* infile, char* linebuffer) {
-  signed char c;
-  char* lbPtr = linebuffer;
-  while ((c = fgetc(infile)) != EOF) {
-    *lbPtr++ = c;
-    if (c == 10) {
-      *lbPtr = 0;
-      return (strlen(linebuffer));
-    }
-  }
-  return 0;
-}
 
 int main(int argc, char **argv) {
 
   const char * helptext ="imp2ld 1.0 Lexicon/Dictionary/Daily Devotional/Glossary module creation tool for the SWORD Project\n  usage:\n   %s <filename> [modname] [ 4 (default) | 2 | z - module driver]\n";
 
   signed long i = 0;
-  char* keybuffer = new char[2048];
-  char* entbuffer = new char[1048576];
-  char* linebuffer = new char[1048576];
+  string keybuffer;
+  string entbuffer;
+  string linebuffer;
   char modname[16];
   char links = 0;
-  char* linkbuffer[32];
-  while (i < 32) {
-        linkbuffer[i] = new char[256];
-        i++;
-  }
+  std::vector<string> linkbuffer;
+
   if (argc > 2) {
     strcpy (modname, argv[2]);
   }
@@ -67,8 +44,7 @@
     exit(-1);
   }
 
-  FILE *infile;
-  infile = fopen(argv[1], "r");
+  std::ifstream infile(argv[1]);
 
   char mode = 1;
   if (argc > 3) {
@@ -107,74 +83,74 @@
 
   key->Persist(1);
 
-  while (readline(infile, linebuffer)) {
-    if (!strncmp(linebuffer, "$$$", 3)) {
-      if (strlen(keybuffer) && strlen(entbuffer)) {
+  while (!infile.eof()) {
+    std::getline(infile, linebuffer);
+    if (linebuffer.substr(0,3) == "$$$") {
+      if (keybuffer.size() && entbuffer.size()) {
 	std::cout << keybuffer << std::endl;
-	*key = keybuffer;
+	*key = keybuffer.c_str();
 
 	if (mode == 3) {
 	  modZ->setKey(*key);
-	  modZ->setEntry(entbuffer, strlen(entbuffer));
+	  modZ->setEntry(entbuffer.c_str(), entbuffer.size());
           for (i = 0; i < links; i++) {
-                SWKey tmpkey = linkbuffer[i];
+                SWKey tmpkey = linkbuffer[i].c_str();
                 modZ->linkEntry(&tmpkey);
                 std::cout << "Linking: " << linkbuffer[i] << std::endl;
           }
 	}
 	else if (mode == 2) {
 	  mod2->setKey(*key);
-	  mod2->setEntry(entbuffer, strlen(entbuffer));
+	  mod2->setEntry(entbuffer.c_str(), entbuffer.size());
           for (i = 0; i < links; i++) {
-                SWKey tmpkey = linkbuffer[i];
+                SWKey tmpkey = linkbuffer[i].c_str();
                 mod2->linkEntry(&tmpkey);
                 std::cout << "Linking: " << linkbuffer[i] << std::endl;
           }
 	}
 	else if (mode == 1) {
 	  mod4->setKey(*key);
-	  mod4->setEntry(entbuffer, strlen(entbuffer));
+	  mod4->setEntry(entbuffer.c_str(), entbuffer.size());
           for (i = 0; i < links; i++) {
-                SWKey tmpkey = linkbuffer[i];
+                SWKey tmpkey = linkbuffer[i].c_str();
                 mod4->linkEntry(&tmpkey);
                 std::cout << "Linking: " << linkbuffer[i] << std::endl;
           }
 	}
       }
-      linebuffer[strlen(linebuffer) - 1] = 0;
-      strcpy (keybuffer, linebuffer + 3);
-      *entbuffer = 0;
+      keybuffer = linebuffer.substr(3,linebuffer.size()) ;
+      entbuffer.resize(0);
+      linkbuffer.clear();
       links = 0;
     }
-    else if (!strncmp(linebuffer, "%%%", 3)) {
-      strcpy (linkbuffer[links], linebuffer + 3);
-      linkbuffer[links][strlen(linkbuffer[links]) - 1] = 0;
+    else if (linebuffer.substr(0,3) == "%%%") {
+      linkbuffer.push_back(linebuffer.substr(3,linebuffer.size()));
       links++;
     }
     else {
-      strcat (entbuffer, linebuffer);
+      entbuffer += linebuffer;
     }
   }
 
   //handle final entry
-  if (strlen(keybuffer) && strlen(entbuffer)) {
+  if (keybuffer.size() && entbuffer.size()) {
     std::cout << keybuffer << std::endl;
-    *key = keybuffer;
+    *key = keybuffer.c_str();
 
     if (mode == 3) {
 	  modZ->setKey(*key);
-          modZ->setEntry(entbuffer, strlen(entbuffer));
+          modZ->setEntry(entbuffer.c_str(), entbuffer.size());
           for (i = 0; i < links; i++) {
-                SWKey tmpkey = linkbuffer[i];
+                SWKey tmpkey = linkbuffer[i].c_str();
                 modZ->linkEntry(&tmpkey);
                 std::cout << "Linking: " << linkbuffer[i] << std::endl;
           }
     }
     else if (mode == 2) {
 	  mod2->setKey(*key);
-          mod2->setEntry(entbuffer, strlen(entbuffer));
+          mod2->setEntry(entbuffer.c_str(), entbuffer.size());
           for (i = 0; i < links; i++) {
-                SWKey tmpkey = linkbuffer[i];
+                SWKey tmpkey = linkbuffer[i].c_str();
                 mod2->linkEntry(&tmpkey);
                 std::cout << "Linking: " << linkbuffer[i] << std::endl;
           }
@@ -182,21 +158,16 @@
     }
     else if (mode == 1) {
           mod4->setKey(*key);
-          mod4->setEntry(entbuffer, strlen(entbuffer));
+          mod4->setEntry(entbuffer.c_str(), entbuffer.size());
           for (i = 0; i < links; i++) {
-                SWKey tmpkey = linkbuffer[i];
+                SWKey tmpkey = linkbuffer[i].c_str();
                 mod4->linkEntry(&tmpkey);
                 std::cout << "Linking: " << linkbuffer[i] << std::endl;
           }
 
     }
   }
+  infile.close();
 
-  //DEBUG  printTree(root, treeKey);
-
-  delete keybuffer;
-  delete entbuffer;
-  delete linebuffer;
-
   return 0;
 }



More information about the sword-cvs mailing list