Thread: [Refdb-cvs] CVS: refdb/src refdbdbib.c,1.29,1.30
Status: Beta
Brought to you by:
mhoenicka
|
From: Markus H. <mho...@us...> - 2004-01-18 21:17:43
|
Update of /cvsroot/refdb/refdb/src
In directory sc8-pr-cvs1:/tmp/cvs-serv28035
Modified Files:
refdbdbib.c
Log Message:
added character encoding conversion
Index: refdbdbib.c
===================================================================
RCS file: /cvsroot/refdb/refdb/src/refdbdbib.c,v
retrieving revision 1.29
retrieving revision 1.30
diff -u -U2 -r1.29 -r1.30
--- refdbdbib.c 3 Jan 2004 20:11:43 -0000 1.29
+++ refdbdbib.c 18 Jan 2004 21:17:37 -0000 1.30
@@ -34,4 +34,5 @@
#include <arpa/inet.h>
#include <expat.h>
+#include <iconv.h>
#include <dbi/dbi.h>
@@ -96,5 +97,5 @@
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
-int getbib(struct CLIENT_REQUEST* ptr_clrequest, struct bibinfo* biblio_info, char *table_name, int n_ref_format, int n_send_stylespec, struct ADDRESULT* ptr_addresult) {
+int getbib(struct CLIENT_REQUEST* ptr_clrequest, struct bibinfo* ptr_biblio_info, char *table_name, int n_ref_format, int n_send_stylespec, struct ADDRESULT* ptr_addresult) {
XML_Parser p;
struct getbib_data gbdata;
@@ -156,6 +157,8 @@
const char* item;
const char* item1;
+ const char* db_encoding;
struct BIBCONNS bibconns;
struct lilimem sentinel;
+ iconv_t conv_descriptor;
sentinel.ptr_mem = NULL;
@@ -163,6 +166,6 @@
sentinel.varname[0] = '\0';
- biblio_info->entry_id = NULL;
- biblio_info->xreflabel = xreflabel;
+ ptr_biblio_info->entry_id = NULL;
+ ptr_biblio_info->xreflabel = xreflabel;
bibconns.conn = NULL; /* connection to the default reference database */
@@ -170,5 +173,5 @@
bibconns.conn_source = NULL; /* connection to source reference database */
- nref_counter = biblio_info->n_startnumber - 1; /* numbering starts here */
+ nref_counter = ptr_biblio_info->n_startnumber - 1; /* numbering starts here */
sql_command_len = 4096;
@@ -254,9 +257,42 @@
}
- myjournal = strdup(biblio_info->format_string);
+
+
+ /* get the database encoding */
+ db_encoding = dbi_conn_get_encoding(bibconns.conn);
+
+ /* if we need to convert, create a conversion descriptor for iconv() */
+ if (db_encoding && *(ptr_biblio_info->encoding) && strcmp(db_encoding, ptr_biblio_info->encoding)) {
+ conv_descriptor = iconv_open(!strcmp(ptr_biblio_info->encoding, "US-ASCII") ? "ASCII" : ptr_biblio_info->encoding, !strcmp(db_encoding, "US-ASCII") ? "ASCII" : db_encoding);
+ if (conv_descriptor == (iconv_t)(-1)) {
+ LOG_PRINT(LOG_WARNING, "cannot set conversion descriptor");
+ iwrite(ptr_clrequest->fd, "cannot set conversion descriptor\n", 33);
+ delete_all_lilimem(&sentinel);
+ close_dbi_connections(&bibconns, drivername);
+ return 0;
+ }
+ else {
+ LOG_PRINT(LOG_DEBUG, "database encoding is:");
+ LOG_PRINT(LOG_DEBUG, db_encoding);
+ }
+ }
+ else {
+ conv_descriptor = NULL;
+ LOG_PRINT(LOG_DEBUG, "no character encoding conversion required");
+ }
+
+ LOG_PRINT(LOG_DEBUG, "output encoding is:");
+ LOG_PRINT(LOG_DEBUG, ptr_biblio_info->encoding);
+
+
+
+ myjournal = strdup(ptr_biblio_info->format_string);
if (!myjournal || insert_lilimem(&sentinel, (void**)&myjournal, NULL)) {
LOG_PRINT(LOG_WARNING, outomem.text);
delete_all_lilimem(&sentinel);
close_dbi_connections(&bibconns, drivername);
+ if (conv_descriptor) {
+ iconv_close(conv_descriptor);
+ }
return 0;
}
@@ -277,4 +313,7 @@
close_dbi_connections(&bibconns, drivername);
LOG_PRINT(LOG_WARNING, "could not write to client");
+ if (conv_descriptor) {
+ iconv_close(conv_descriptor);
+ }
return 0;
}
@@ -289,4 +328,7 @@
close_dbi_connections(&bibconns, drivername);
delete_all_lilimem(&sentinel);
+ if (conv_descriptor) {
+ iconv_close(conv_descriptor);
+ }
return 0;
}
@@ -294,4 +336,7 @@
close_dbi_connections(&bibconns, drivername);
delete_all_lilimem(&sentinel);
+ if (conv_descriptor) {
+ iconv_close(conv_descriptor);
+ }
return 0;
}
@@ -301,4 +346,7 @@
close_dbi_connections(&bibconns, drivername);
delete_all_lilimem(&sentinel);
+ if (conv_descriptor) {
+ iconv_close(conv_descriptor);
+ }
return 0;
}
@@ -307,4 +355,7 @@
close_dbi_connections(&bibconns, drivername);
delete_all_lilimem(&sentinel);
+ if (conv_descriptor) {
+ iconv_close(conv_descriptor);
+ }
return 0;
}
@@ -379,4 +430,7 @@
close_dbi_connections(&bibconns, drivername);
delete_all_lilimem(&sentinel);
+ if (conv_descriptor) {
+ iconv_close(conv_descriptor);
+ }
return 0;
}
@@ -398,4 +452,7 @@
close_dbi_connections(&bibconns, drivername);
delete_all_lilimem(&sentinel);
+ if (conv_descriptor) {
+ iconv_close(conv_descriptor);
+ }
return 0;
}
@@ -425,4 +482,7 @@
close_dbi_connections(&bibconns, drivername);
delete_all_lilimem(&sentinel);
+ if (conv_descriptor) {
+ iconv_close(conv_descriptor);
+ }
return 0;
}
@@ -443,4 +503,7 @@
delete_all_lilimem(&sentinel);
close_dbi_connections(&bibconns, drivername);
+ if (conv_descriptor) {
+ iconv_close(conv_descriptor);
+ }
return 0;
}
@@ -450,4 +513,7 @@
delete_all_lilimem(&sentinel);
close_dbi_connections(&bibconns, drivername);
+ if (conv_descriptor) {
+ iconv_close(conv_descriptor);
+ }
return 0;
}
@@ -460,4 +526,7 @@
delete_all_lilimem(&sentinel);
close_dbi_connections(&bibconns, drivername);
+ if (conv_descriptor) {
+ iconv_close(conv_descriptor);
+ }
return 0;
}
@@ -477,4 +546,7 @@
delete_all_lilimem(&sentinel);
close_dbi_connections(&bibconns, drivername);
+ if (conv_descriptor) {
+ iconv_close(conv_descriptor);
+ }
return 0;
}
@@ -485,4 +557,7 @@
delete_all_lilimem(&sentinel);
close_dbi_connections(&bibconns, drivername);
+ if (conv_descriptor) {
+ iconv_close(conv_descriptor);
+ }
return 0;
}
@@ -520,5 +595,5 @@
/* write doctype line etc */
- prepare_render_dbib(outbuffer, biblio_info, bibconns.conn_refdb, n_ref_format);
+ prepare_render_dbib(outbuffer, ptr_biblio_info, bibconns.conn_refdb, n_ref_format);
bibsequence = dbi_result_get_string(dbires, "BIBLIOSEQUENCE");
@@ -637,5 +712,5 @@
if (!nis_numeric) { /* only useful for author/year style */
- if (fill_in_authoryear_info(&bibconns, dbires, dbires_cit, ptr_clrequest, ptr_addresult, biblio_info, table_name, intext_string, current_mset, &nref_counter)) {
+ if (fill_in_authoryear_info(&bibconns, dbires, dbires_cit, ptr_clrequest, ptr_addresult, ptr_biblio_info, table_name, intext_string, current_mset, &nref_counter)) {
LOG_PRINT(LOG_DEBUG, "fill in author/year info failed");
dbi_result_free(dbires_cit);
@@ -769,6 +844,6 @@
}
- biblio_info->entry_id = my_dbi_result_get_string_copy(dbires, "entry_id");
- if (!biblio_info->entry_id || !strcmp(biblio_info->entry_id, "ERROR") || insert_lilimem(&sentinel, (void**)&(biblio_info->entry_id), "id_string")) {
+ ptr_biblio_info->entry_id = my_dbi_result_get_string_copy(dbires, "entry_id");
+ if (!ptr_biblio_info->entry_id || !strcmp(ptr_biblio_info->entry_id, "ERROR") || insert_lilimem(&sentinel, (void**)&(ptr_biblio_info->entry_id), "id_string")) {
iwrite(ptr_clrequest->fd, "", 1);
if (!assemble_return_msg(ptr_addresult, notempselectcolon.text, bibconns.conn)) {
@@ -780,5 +855,5 @@
goto Finish;
}
- biblio_info->year_unique_suffix = dbi_result_get_string(dbires, "year_uni_suffix");
+ ptr_biblio_info->year_unique_suffix = dbi_result_get_string(dbires, "year_uni_suffix");
/* compare current string with the string of the previous iteration */
@@ -801,8 +876,8 @@
if (item && *item && prev_authorconcat && *prev_authorconcat
&& strcmp(item, prev_authorconcat) == 0) {
- biblio_info->is_subseq = 1;
+ ptr_biblio_info->is_subseq = 1;
}
else {
- biblio_info->is_subseq = 0;
+ ptr_biblio_info->is_subseq = 0;
}
@@ -817,5 +892,5 @@
item = dbi_result_get_string(dbires, "dbname");
- if ((new_outbuffer = render_dbib(&outbuffer, &outbuffer_len, &bibconns, dbires1 /* reference info */, item /* database */, biblio_info, ptr_clrequest->username, "", nref_counter)) == NULL) {
+ if ((new_outbuffer = render_dbib(&outbuffer, &outbuffer_len, &bibconns, dbires1 /* reference info */, item /* database */, ptr_biblio_info, ptr_clrequest->username, "", nref_counter)) == NULL) {
iwrite(ptr_clrequest->fd, "", 1);
nfailed++;
@@ -833,4 +908,61 @@
}
+ /* todo: run iconv here */
+
+
+ /* run a character encoding conversion if required */
+ if (conv_descriptor && *outbuffer) {
+ size_t inlength;
+ size_t outlength;
+ char* my_outbuffer = NULL; /* this ptr will be modified by iconv() */
+ char* my_outbuffer_start = NULL; /* records initial state of my_elvalue */
+ const char* my_instring = NULL; /* this ptr will be modified by iconv() */
+ inlength = strlen(outbuffer) + 1;
+ /* with the encodings supported by our database engines, the converted
+ string can't be longer than three times the input string */
+ /* todo: is this assumption correct? */
+ outlength = 3*inlength;
+
+ if ((my_outbuffer = (char*)malloc(outlength)) == NULL) {
+ iwrite(ptr_clrequest->fd, outomem_n.text, outomem_n.length);
+ /* dbi_result_free(rendinfo.dbires); */
+ dbi_result_free(dbires);
+ close_dbi_connections(&bibconns, drivername);
+ goto Finish;
+ }
+
+ /* keep start of the converted string */
+ my_outbuffer_start = my_outbuffer;
+
+ /* variable will be modified by iconv, so don't use original */
+ my_instring = (const char*)outbuffer;
+
+ /* now actually do the conversion */
+ if (iconv(conv_descriptor, &my_instring, &inlength, &my_outbuffer, &outlength) == (size_t)(-1)) {
+ if (errno == EILSEQ) {
+ iwrite(ptr_clrequest->fd, "iconv: invalid input character sequence\n", 40);
+ LOG_PRINT(LOG_WARNING, "iconv: invalid input character sequence");
+ }
+ else if (errno == E2BIG) {
+ iwrite(ptr_clrequest->fd, "iconv: output buffer too small\n", 31);
+ LOG_PRINT(LOG_WARNING, "iconv: output buffer too small");
+ }
+ else if (errno == EINVAL) {
+ iwrite(ptr_clrequest->fd, "iconv: incomplete input character\n", 34);
+ LOG_PRINT(LOG_WARNING, "iconv: incomplete input character");
+ }
+
+ close_dbi_connections(&bibconns, drivername);
+ goto Finish;
+ }
+ /* else: conversion went ok. We free the original string and replace
+ it with the converted copy */
+ if (outbuffer) {
+ free(outbuffer);
+ }
+ outbuffer = my_outbuffer_start;
+ }
+ /* else: no conversion required */
+
iwrite(ptr_clrequest->fd, outbuffer, strlen(outbuffer));
outbuffer[0] = '\0';
@@ -847,5 +979,5 @@
} /* end loop over all distinct references */
- if ((new_outbuffer = finish_render_dbib(&outbuffer, &outbuffer_len, &bibconns, biblio_info, table_name, ptr_clrequest)) == NULL) {
+ if ((new_outbuffer = finish_render_dbib(&outbuffer, &outbuffer_len, &bibconns, ptr_biblio_info, table_name, ptr_clrequest)) == NULL) {
iwrite(ptr_clrequest->fd, "", 1);
nfailed++;
@@ -887,5 +1019,5 @@
Finish:
- ptr_addresult->success = nref_counter - biblio_info->n_startnumber + 1;
+ ptr_addresult->success = nref_counter - ptr_biblio_info->n_startnumber + 1;
ptr_addresult->failure = nfailed;
numbyte = tread(ptr_clrequest->fd, inbuffer, COMMAND_INBUF_LEN);
@@ -899,4 +1031,8 @@
}
+ if (conv_descriptor) {
+ iconv_close(conv_descriptor);
+ }
+
return (nfailed) ? 1:0;
}
|