X-Git-Url: https://git.dlugolecki.net.pl/?a=blobdiff_plain;f=gedcom%2Fwrite.c;h=f0b1529ca8ca9271d107dbf1cc69aa98bca241c6;hb=809c37c2f87962cbb9fcb84c851d961e276fef72;hp=aba4195be34c88f9dadeb63fc13a999dfe9badc3;hpb=082066d0c776403b70c366f9a7d1333c7a9fac15;p=gedcom-parse.git diff --git a/gedcom/write.c b/gedcom/write.c index aba4195..f0b1529 100644 --- a/gedcom/write.c +++ b/gedcom/write.c @@ -26,14 +26,16 @@ #include "encoding.h" #include "tag_data.h" #include "buffer.h" -#include "utf8.h" +#include "utf8tools.h" #include #include #include #include #define MAXWRITELEN MAXGEDCLINELEN +#define MAXCHARSETLEN 32 +char charset[MAXCHARSETLEN+1] = "ASCII"; const char* encoding = "ASCII"; int write_encoding_details = ONE_BYTE; /* SYS_NEWLINE is defined in config.h */ @@ -79,7 +81,8 @@ void cleanup_convert_at_buffer() } int write_simple(Gedcom_write_hndl hndl, - int level, char* xref, char* tag, char* value) + int level, const char* xref, const char* tag, + const char* value) { int res; @@ -119,31 +122,40 @@ int write_simple(Gedcom_write_hndl hndl, return 0; } +int write_encoding(Gedcom_write_hndl hndl, + int level, char* xref, char* tag, char* value) +{ + if (strcmp(value, charset)) + gedcom_warning(_("Forcing HEAD.CHAR value to '%s'"), charset); + return write_simple(hndl, level, xref, tag, charset); +} + int supports_continuation(int elt_or_rec, int which_continuation) { return tag_data[elt_or_rec].options & which_continuation; } int write_long(Gedcom_write_hndl hndl, int elt_or_rec, - int level, char* xref, char* tag, char* value) + int level, const char* xref, const char* tag, const char* value) { - int prefix_len, value_len, term_len; - char* nl_pos = strchr(value, '\n'); + int prefix_len, value_len = 0, term_len; + char* nl_pos = NULL; + if (value) nl_pos = strchr(value, '\n'); prefix_len = utf8_strlen(tag) + 3; /* for e.g. "0 INDI " */ if (level > 9) prefix_len++; if (xref) prefix_len += utf8_strlen(xref) + 1; - value_len = utf8_strlen(value); + if (value) value_len = utf8_strlen(value); term_len = strlen(hndl->term); if (!nl_pos && prefix_len + value_len + term_len <= MAXWRITELEN) write_simple(hndl, level, xref, tag, value); else { - char* value_ptr = value; + const char* value_ptr = value; int cont_supported = supports_continuation(elt_or_rec, OPT_CONT); int cont_as_conc = supports_continuation(elt_or_rec, OPT_CONT_AS_CONC); if (nl_pos && !cont_supported) { - gedcom_error (_("The tag %s doesn't support newlines\n"), tag); + gedcom_error (_("The tag %s doesn't support newlines"), tag); return 1; } else { @@ -189,30 +201,32 @@ int write_long(Gedcom_write_hndl hndl, int elt_or_rec, return 0; } -int gedcom_write_set_encoding(const char* charset, +int gedcom_write_set_encoding(const char* new_charset, Encoding width, Enc_bom bom) { char* new_encoding = NULL; - if (!strcmp(charset, "UNICODE")) { + 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(charset, width); + new_encoding = get_encoding(new_charset, width); if (new_encoding) { encoding = new_encoding; write_encoding_details = width | bom; + strncpy(charset, new_charset, MAXCHARSETLEN); } else return 1; } } else { - new_encoding = get_encoding(charset, ONE_BYTE); + new_encoding = get_encoding(new_charset, ONE_BYTE); if (new_encoding) { encoding = new_encoding; write_encoding_details = ONE_BYTE; + strncpy(charset, new_charset, MAXCHARSETLEN); } else return 1; @@ -297,7 +311,7 @@ char* get_tag_string(int elt_or_rec, int tag) } } else { - gedcom_error(_("The element or record type '%s' requires a specific tag" + gedcom_error(_("The element or record type '%s' requires a specific tag " "for writing"), tag_data[elt_or_rec].elt_name); return NULL; @@ -369,7 +383,9 @@ int _gedcom_write_val(Gedcom_write_hndl hndl, tag_str = get_tag_string(rec_or_elt, tag); level = get_level(hndl, rec_or_elt, parent_rec_or_elt); if (tag_str && (level != -1)) { - if (supports_continuation(rec_or_elt, OPT_CONT|OPT_CONC|OPT_CONT_AS_CONC)) + if (rec_or_elt == ELT_HEAD_CHAR) + result = write_encoding(hndl, level, xrefstr, tag_str, val); + else if (supports_continuation(rec_or_elt, OPT_CONT|OPT_CONC)) result = write_long(hndl, rec_or_elt, level, xrefstr, tag_str, val); else result = write_simple(hndl, level, xrefstr, tag_str, val); @@ -379,12 +395,11 @@ int _gedcom_write_val(Gedcom_write_hndl hndl, } int gedcom_write_record_str(Gedcom_write_hndl hndl, - Gedcom_rec rec, int tag, - char* xrefstr, char* val) + Gedcom_rec rec, char* xrefstr, char* val) { int result = 1; if (check_type(rec, (val ? GV_CHAR_PTR : GV_NULL))) - result = _gedcom_write_val(hndl, rec, tag, -1, xrefstr, convert_at(val)); + result = _gedcom_write_val(hndl, rec, 0, -1, xrefstr, convert_at(val)); return result; } @@ -399,24 +414,36 @@ int gedcom_write_element_str(Gedcom_write_hndl hndl, return result; } -int gedcom_write_record_xref(Gedcom_write_hndl hndl, - Gedcom_rec rec, int tag, - char* xrefstr, struct xref_value* val) +int gedcom_write_element_xref(Gedcom_write_hndl hndl, + Gedcom_elt elt, int tag, int parent_rec_or_elt, + struct xref_value* val) { int result = 1; - if (check_type(rec, (val ? GV_XREF_PTR : GV_NULL))) - result = _gedcom_write_val(hndl, rec, tag, -1, xrefstr, val->string); + if (check_type(elt, (val ? GV_XREF_PTR : GV_NULL))) + result = _gedcom_write_val(hndl, elt, tag, parent_rec_or_elt, NULL, + val->string); return result; } -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 xref_value* val) + struct date_value* val) { int result = 1; - if (check_type(elt, (val ? GV_XREF_PTR : GV_NULL))) + if (check_type(elt, (val ? GV_DATE_VALUE : GV_NULL))) result = _gedcom_write_val(hndl, elt, tag, parent_rec_or_elt, NULL, - val->string); + gedcom_date_to_string(val)); + return result; +} + +int gedcom_write_element_age(Gedcom_write_hndl hndl, + Gedcom_elt elt, int tag, int parent_rec_or_elt, + struct age_value* val) +{ + int result = 1; + if (check_type(elt, (val ? GV_AGE_VALUE : GV_NULL))) + result = _gedcom_write_val(hndl, elt, tag, parent_rec_or_elt, NULL, + gedcom_age_to_string(val)); return result; }