The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mod2vpl.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * mod2vpl.cpp - Utility to export a module in VPL format
4  *
5  * $Id: mod2vpl.cpp 3063 2014-03-04 13:04:11Z chrislit $
6  *
7  * Copyright 2000-2013 CrossWire Bible Society (http://www.crosswire.org)
8  * CrossWire Bible Society
9  * P. O. Box 2528
10  * Tempe, AZ 85280-2528
11  *
12  * This program is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU General Public License as published by the
14  * Free Software Foundation version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * General Public License for more details.
20  *
21  */
22 
23 #ifdef _MSC_VER
24  #pragma warning( disable: 4251 )
25 #endif
26 
27 #include <swmgr.h>
28 #include <swmodule.h>
29 #include <swkey.h>
30 #include <versekey.h>
31 #include <stdio.h>
32 #include <iostream>
33 
34 #ifndef NO_SWORD_NAMESPACE
35 using sword::SWMgr;
36 using sword::VerseKey;
37 using sword::SWModule;
38 using sword::SWKey;
39 using sword::SW_POSITION;
40 using sword::ModMap;
41 #endif
42 
43 void cleanbuf(char *buf) {
44  char *from = buf;
45  char *to = buf;
46 
47  while (*from) {
48  if ((*from != 10) && (*from != 13)) {
49  *to++ = *from++;
50  }
51  else {
52  from++;
53  }
54  }
55  *to = 0;
56 }
57 
58 int main(int argc, char **argv) {
59  char *buffer = 0;
60 
61  if (argc < 2) {
62  fprintf(stderr, "usage: %s <Mod Name> [0|1 - prepend verse reference to each line]\n", argv[0]);
63  exit(-1);
64  }
65 
66  SWMgr mgr;
67 
68  ModMap::iterator it = mgr.Modules.find(argv[1]);
69  if (it == mgr.Modules.end()) {
70  fprintf(stderr, "error: %s: couldn't find module: %s \n", argv[0], argv[1]);
71  exit(-2);
72  }
73 
74  bool vref = false;
75  if (argc > 2)
76  vref = (argv[2][0] == '0') ? false : true;
77 
78 
79  SWModule *mod = it->second;
80 
81  SWKey *key = (*mod);
82  VerseKey *vkey = 0;
83  SWTRY {
84  vkey = dynamic_cast<VerseKey *>(key);
85  }
86  SWCATCH (...) {}
87 
88  if (!vkey) {
89  fprintf(stderr, "error: %s: %s module is not keyed to verses \n", argv[0], argv[1]);
90  exit(-3);
91  }
92 
93  vkey->setIntros(true); // turn on mod/testmnt/book/chap headings
94 
95  (*mod) = TOP;
96 
97  while (!mod->popError()) {
98  buffer = new char [ mod->renderText().length() + 1 ];
99  strcpy(buffer, mod->renderText());
100  cleanbuf(buffer);
101  if (vref) {
102  if ((strlen(buffer) > 0) && (vref)) {
103  std::cout << (const char *)(*vkey) << " ";
104  std::cout << buffer << std::endl;
105  }
106  }
107  else std::cout << buffer << std::endl;
108 
109  delete [] buffer;
110  (*mod)++;
111  }
112 }
#define TOP
Definition: swkey.h:68
Definition: swmgr.h:93
unsigned long length() const
Definition: swbuf.h:197
ModMap Modules
Definition: swmgr.h:322
preg buffer
Definition: regex.c:8089
int main(int argc, char **argv)
Definition: addcomment.cpp:32
#define SWTRY
Definition: defs.h:57
SWBuf renderText(const char *buf, int len=-1, bool render=true) const
Definition: swmodule.cpp:1038
virtual void setIntros(bool val)
Definition: versekey.cpp:1663
#define SWCATCH(x)
Definition: defs.h:58
virtual char popError()
Definition: swmodule.cpp:185
std::map< SWBuf, SWModule *, std::less< SWBuf > > ModMap
Definition: swmgr.h:74
void cleanbuf(char *buf)
Definition: mod2vpl.cpp:43
Definition: swkey.h:77
SWMgr * mgr
Definition: installmgr.cpp:46