Catdoc 是一套 GNU Public License Open Source,最近摸了一下,稍微改动了一点地方,主要原因是 catdoc 在找寻字码表时,会采用原先在 compile time 所下的 define 关键字,这意味著程式若要搬移必须重制原先编译时期所决定的相关目录结构才行,例如一开始编译时,就说字码将安装在 /usr/local/share/catdoc 下,那你把 binary 程式去另一台机器时,也必须把字码一样设置相同的路径才行。

因此花了一点时间看了一下,还满容易的,仅需把两个变数(charset_path, map_path)的定义参数化,因为那个字码路径(CHARSETPATH)在各个程式之间算共享的资讯,在 C 语言中就是透过定义一份记忆体位置,其他用 extern 的方式来连结存取。改完后在多设个参数(-r /path/charsets)让执行时可以初始化就好。

程式码:

https://github.com/changyy/catdoc-portable

详细流程:

diff --git a/catdoc-0.94.2/src/catdoc.c b/catdoc-0.94.2/src/catdoc.c
index d8270fd..c3729a4 100644
--- a/catdoc-0.94.2/src/catdoc.c
+++ b/catdoc-0.94.2/src/catdoc.c
@@ -35,6 +35,8 @@ int _argc;
/* Processes options, reads charsets files and substitution */
/* maps and passes all remaining args to processfile */
/**************************************************************/
+char *charset_path = NULL;
+char *map_path = NULL;
int main(int argc, char **argv) {
FILE *f;
int c,i;
@@ -56,8 +58,15 @@ int main(int argc, char **argv) {
#ifdef HAVE_LANGINFO
get_locale_charset();
#endif
- while ((c=getopt(argc,argv,"Vls:d:f:taubxv8wm:"))!=-1) {
+ charset_path = strdup(CHARSETPATH);
+ map_path = charset_path;
+ while ((c=getopt(argc,argv,"Vls:d:f:taubxv8wm:r:"))!=-1) {
switch (c) {
+ case 'r':
+ free(charset_path);
+ charset_path = strdup(optarg);
+ map_path = charset_path;
+ break;
case 's':
check_charset(&source_csname,optarg);
forced_charset = 1;
@@ -181,12 +190,13 @@ int main(int argc, char **argv) {
fclose(f);
}
}
+ free(charset_path);
return c;
}
/************************************************************************/
/* Displays help message */
/************************************************************************/
void help (void) {
- printf("Usage:\n catdoc [-vu8btawxlV] [-m number] [-s charset] "
+ printf("Usage:\n catdoc [-vu8btawxlV] [-r /path/charsets] [-m number] [-s charset] "
"[-d charset] [ -f format] files\n");
}
diff --git a/catdoc-0.94.2/src/catppt.c b/catdoc-0.94.2/src/catppt.c
index 50fc04c..f74307a 100644
--- a/catdoc-0.94.2/src/catppt.c
+++ b/catdoc-0.94.2/src/catppt.c
@@ -34,7 +34,7 @@
*
*/
void help (void) {
- printf("Usage:\n ppt2text [-lV] [-b string] [-s charset] [-d charset] files\n");
+ printf("Usage:\n ppt2text [-lV] [-r /path/charsets] [-b string] [-s charset] [-d charset] files\n");
}


@@ -48,6 +48,8 @@ char *input_buffer, *output_buffer;
*
* @return
*/
+char *charset_path = NULL;
+char *map_path = NULL;
int main(int argc, char *argv[]) {
FILE *input;
FILE *new_file, *ole_file;
@@ -68,10 +70,17 @@ int main(int argc, char *argv[]) {
get_locale_charset();
#endif

+ charset_path = strdup(CHARSETPATH);
+ map_path = charset_path;
check_charset(&dest_csname,dest_csname);

- while ((c=getopt(argc,argv,"Vls:d:p:"))!=-1) {
+ while ((c=getopt(argc,argv,"Vls:d:p:r:"))!=-1) {
switch(c) {
+ case 'r':
+ free(charset_path);
+ charset_path = strdup(optarg);
+ map_path = charset_path;
+ break;
case 'l':
list_charsets(); exit(0);
case 's':
@@ -153,5 +162,6 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "%s is not OLE file or Error\n", filename);
}
}
+ free(charset_path);
return 0;
}
diff --git a/catdoc-0.94.2/src/charsets.c b/catdoc-0.94.2/src/charsets.c
index b52a1eb..9c0b699 100644
--- a/catdoc-0.94.2/src/charsets.c
+++ b/catdoc-0.94.2/src/charsets.c
@@ -12,7 +12,7 @@
#include <stdlib.h>
#include "catdoc.h"

-char *charset_path=CHARSETPATH;
+//char *charset_path=CHARSETPATH;
char *source_csname=SOURCE_CHARSET, *dest_csname=TARGET_CHARSET;
short int * source_charset;
int unknown_as_hex=0;
diff --git a/catdoc-0.94.2/src/substmap.c b/catdoc-0.94.2/src/substmap.c
index f29d629..a609a4c 100644
--- a/catdoc-0.94.2/src/substmap.c
+++ b/catdoc-0.94.2/src/substmap.c
@@ -13,7 +13,7 @@
#include <stdlib.h>
#include "catdoc.h"
SUBSTMAP spec_chars, replacements;
-char *map_path=CHARSETPATH;
+//char *map_path=CHARSETPATH;
void map_insert(SUBSTMAP map, int uc, const char *s) ;
int longest_sequence=6;/* six is longest character sequence which can be
generated by catdoc internally*/
diff --git a/catdoc-0.94.2/src/xls2csv.c b/catdoc-0.94.2/src/xls2csv.c
index 243e801..34184d1 100644
--- a/catdoc-0.94.2/src/xls2csv.c
+++ b/catdoc-0.94.2/src/xls2csv.c
@@ -31,13 +31,15 @@ extern char *sheet_separator;
/* Displays help message */
/************************************************************************/
void help (void) {
- printf("Usage:\n xls2csv [-xlV] [-g number] [-f date-format] [-b string] [-s charset] [-d charset] [-c char] [ -q number] files\n");
+ printf("Usage:\n xls2csv [-xlV] [-r /path/charsets] [-g number] [-f date-format] [-b string] [-s charset] [-d charset] [-c char] [ -q number] files\n");
}
/* Defines unicode chars which should be
replaced by strings before UNICODE->target chatset
mappigs are applied i.e. TeX special chars like %
*/
char *input_buffer, *output_buffer;
+char *charset_path = NULL;
+char *map_path = NULL;
int main(int argc, char *argv[])
{
FILE *input;
@@ -58,11 +60,17 @@ int main(int argc, char *argv[])
#ifdef HAVE_LANGINFO
get_locale_charset();
#endif
-
+ charset_path = strdup(CHARSETPATH);
+ map_path = charset_path;
check_charset(&dest_csname,dest_csname);
-
- while ((c=getopt(argc,argv,"Vlf:s:d:xq:c:b:g:p:"))!=-1) {
+
+ while ((c=getopt(argc,argv,"Vlf:s:d:xq:c:b:g:p:r:"))!=-1) {
switch(c) {
+ case 'r':
+ free(charset_path);
+ charset_path = strdup(optarg);
+ map_path = charset_path;
+ break;
case 'l':
list_charsets(); exit(0);
case 'x':
@@ -176,5 +184,6 @@ int main(int argc, char *argv[])
fprintf(stderr, "%s is not OLE file or Error\n", filename);
}
}
+ free(charset_path);
return 0;
}

 


相关文章