X-Git-Url: https://git.dlugolecki.net.pl/?a=blobdiff_plain;ds=sidebyside;f=gedcom%2Fwrite.c;h=6e186b7b8c612d444d9895cfa7622df67288a36a;hb=6103dd898c4de86c68891cc0222543988a2caab7;hp=7554b560ea7832163748df254fd4926ffec76a9f;hpb=58c7f618a6964a7fa5fc95c67f13f0a52689a29a;p=gedcom-parse.git diff --git a/gedcom/write.c b/gedcom/write.c index 7554b56..6e186b7 100644 --- a/gedcom/write.c +++ b/gedcom/write.c @@ -24,6 +24,7 @@ #include "gedcom_internal.h" #include "gedcom.h" #include "encoding.h" +#include "encoding_state.h" #include "tag_data.h" #include "buffer.h" #include "utf8tools.h" @@ -34,12 +35,6 @@ #define MAXWRITELEN MAXGEDCLINELEN -/* SYS_NEWLINE is defined in config.h */ -struct encoding_state write_encoding = -{ "ASCII", "ASCII", ONE_BYTE, WITHOUT_BOM, SYS_NEWLINE }; -Enc_from write_encoding_from = ENC_FROM_FILE; -Enc_from write_terminator_from = ENC_FROM_SYS; - struct Gedcom_write_struct { int filedesc; convert_t conv; @@ -49,19 +44,6 @@ struct Gedcom_write_struct { int ctxt_level; }; -const char* default_encoding[] = { - /* ONE_BYTE */ "ASCII", - /* TWO_BYTE_HILO */ "UCS-2BE", - /* TWO_BYTE_LOHI */ "UCS-2LE" -}; - -const char* terminator[] = { - /* END_CR */ "\x0D", - /* END_LF */ "\x0A", - /* END_CR_LF */ "\x0D\x0A", - /* END_LF_CR */ "\x0A\x0D" -}; - void cleanup_write_buffer(); struct safe_buffer write_buffer = { NULL, 0, NULL, 0, cleanup_write_buffer }; @@ -122,7 +104,8 @@ int write_simple(Gedcom_write_hndl hndl, } int write_encoding_value(Gedcom_write_hndl hndl, - int level, char* xref, char* tag, char* value) + int level, const char* xref, const char* tag, + const char* value) { if (strcmp(value, write_encoding.charset)) gedcom_warning(_("Forcing HEAD.CHAR value to '%s'"), @@ -171,8 +154,13 @@ int write_long(Gedcom_write_hndl hndl, int elt_or_rec, if (prefix_len + line_len + term_len > MAXWRITELEN) { line_len = MAXWRITELEN - prefix_len - term_len; - if (!cont_as_conc) + if (!cont_as_conc) { cont_tag = "CONC"; + while (value_ptr[line_len] == ' ' + || value_ptr[line_len-1] == ' ') { + line_len--; + } + } } memset(value_part, 0, sizeof(value_part)); @@ -182,7 +170,7 @@ int write_long(Gedcom_write_hndl hndl, int elt_or_rec, if (line_len < value_len) { value_ptr = value_ptr + line_len; value_len = value_len - line_len; - while (*value_ptr == '\n') { + if (*value_ptr == '\n') { value_ptr++; value_len--; } @@ -201,80 +189,6 @@ int write_long(Gedcom_write_hndl hndl, int elt_or_rec, return 0; } -int gedcom_write_set_encoding(Enc_from from, const char* new_charset, - Encoding width, Enc_bom bom) -{ - char* new_encoding = NULL; - if (from == ENC_FROM_SYS) { - return 1; - } - write_encoding_from = from; - if (from == ENC_MANUAL) { - if (!strcmp(new_charset, "UNICODE")) { - if (width == ONE_BYTE) { - gedcom_error(_("Unicode cannot be encoded into one byte")); - return 1; - } - else { - new_encoding = get_encoding(new_charset, width); - if (new_encoding) { - write_encoding.encoding = new_encoding; - write_encoding.width = width; - write_encoding.bom = bom; - strncpy(write_encoding.charset, new_charset, MAX_CHARSET_LEN); - } - else - return 1; - } - } - else { - new_encoding = get_encoding(new_charset, ONE_BYTE); - if (new_encoding) { - write_encoding.encoding = new_encoding; - write_encoding.width = ONE_BYTE; - write_encoding.bom = bom; - strncpy(write_encoding.charset, new_charset, MAX_CHARSET_LEN); - } - else - return 1; - } - } - return 0; -} - -void copy_write_encoding_from_file() -{ - if (read_encoding.charset[0] != '\0') { - strncpy(write_encoding.charset, read_encoding.charset, MAX_CHARSET_LEN); - write_encoding.encoding = read_encoding.encoding; - write_encoding.width = read_encoding.width; - write_encoding.bom = read_encoding.bom; - } -} - -int gedcom_write_set_line_terminator(Enc_from from, Enc_line_end end) -{ - const char* new_term = NULL; - write_terminator_from = from; - if (from == ENC_FROM_SYS) { - new_term = SYS_NEWLINE; - } - else if (from == ENC_MANUAL) { - new_term = terminator[end]; - } - if (new_term) - strncpy(write_encoding.terminator, new_term, MAX_TERMINATOR_LEN); - return 0; -} - -void copy_write_terminator_from_file() -{ - if (read_encoding.terminator[0] != '\0') { - strncpy(write_encoding.terminator, read_encoding.terminator, - MAX_TERMINATOR_LEN); - } -} - Gedcom_write_hndl gedcom_write_open(const char *filename) { Gedcom_write_hndl hndl; @@ -284,10 +198,8 @@ Gedcom_write_hndl gedcom_write_open(const char *filename) if (!hndl) MEMORY_ERROR; else { - if (write_encoding_from == ENC_FROM_FILE) - copy_write_encoding_from_file(); - if (write_terminator_from == ENC_FROM_FILE) - copy_write_terminator_from_file(); + init_write_encoding(); + init_write_terminator(); hndl->total_conv_fails = 0; hndl->conv = initialize_utf8_conversion(write_encoding.encoding, 0); if (!hndl->conv) { @@ -415,7 +327,7 @@ char* convert_at(const char* input) int _gedcom_write_val(Gedcom_write_hndl hndl, int rec_or_elt, int tag, int parent_rec_or_elt, - char* xrefstr, char* val) + const char* xrefstr, const char* val) { int result = 1; int level = 0; @@ -436,7 +348,8 @@ int _gedcom_write_val(Gedcom_write_hndl hndl, } int gedcom_write_record_str(Gedcom_write_hndl hndl, - Gedcom_rec rec, char* xrefstr, char* val) + Gedcom_rec rec, const char* xrefstr, + const char* val) { int result = 1; if (check_type(rec, (val ? GV_CHAR_PTR : GV_NULL))) @@ -446,7 +359,7 @@ int gedcom_write_record_str(Gedcom_write_hndl hndl, int gedcom_write_element_str(Gedcom_write_hndl hndl, Gedcom_elt elt, int tag, int parent_rec_or_elt, - char* val) + const char* val) { int result = 1; if (check_type(elt, (val ? GV_CHAR_PTR : GV_NULL))) @@ -457,7 +370,7 @@ int gedcom_write_element_str(Gedcom_write_hndl hndl, int gedcom_write_element_xref(Gedcom_write_hndl hndl, Gedcom_elt elt, int tag, int parent_rec_or_elt, - struct xref_value* val) + const struct xref_value* val) { int result = 1; if (check_type(elt, (val ? GV_XREF_PTR : GV_NULL))) @@ -468,7 +381,7 @@ int gedcom_write_element_xref(Gedcom_write_hndl hndl, int gedcom_write_element_date(Gedcom_write_hndl hndl, Gedcom_elt elt, int tag, int parent_rec_or_elt, - struct date_value* val) + const struct date_value* val) { int result = 1; if (check_type(elt, (val ? GV_DATE_VALUE : GV_NULL))) @@ -479,7 +392,7 @@ int gedcom_write_element_date(Gedcom_write_hndl hndl, int gedcom_write_element_age(Gedcom_write_hndl hndl, Gedcom_elt elt, int tag, int parent_rec_or_elt, - struct age_value* val) + const struct age_value* val) { int result = 1; if (check_type(elt, (val ? GV_AGE_VALUE : GV_NULL))) @@ -488,8 +401,8 @@ int gedcom_write_element_age(Gedcom_write_hndl hndl, return result; } -int gedcom_write_user_str(Gedcom_write_hndl hndl, int level, char* tag, - char* xrefstr, char* value) +int gedcom_write_user_str(Gedcom_write_hndl hndl, int level, const char* tag, + const char* xrefstr, const char* value) { int result = 1; if (tag && tag[0] == '_') @@ -497,8 +410,8 @@ int gedcom_write_user_str(Gedcom_write_hndl hndl, int level, char* tag, return result; } -int gedcom_write_user_xref(Gedcom_write_hndl hndl, int level, char* tag, - char* xrefstr, struct xref_value* val) +int gedcom_write_user_xref(Gedcom_write_hndl hndl, int level, const char* tag, + const char* xrefstr, const struct xref_value* val) { int result = 1; if (tag && tag[0] == '_')