+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 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;
+ 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;
+ 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);
+ return 1;
+ }
+ else {
+ char value_part[MAXWRITELEN];
+ int cont_prefix_len, write_level = level;
+ cont_prefix_len = utf8_strlen("CONT") + 3;
+ if (level + 1 > 9) cont_prefix_len++;
+
+ while (value_ptr) {
+ char* cont_tag = "CONT";
+ int line_len = (nl_pos && cont_supported
+ ? nl_pos - value_ptr : value_len);
+
+ if (prefix_len + line_len + term_len > MAXWRITELEN) {
+ line_len = MAXWRITELEN - prefix_len - term_len;
+ if (!cont_as_conc)
+ cont_tag = "CONC";
+ }
+
+ memset(value_part, 0, sizeof(value_part));
+ strncpy(value_part, value_ptr, line_len);
+ write_simple(hndl, write_level, xref, tag, value_part);
+
+ if (line_len < value_len) {
+ value_ptr = value_ptr + line_len;
+ value_len = value_len - line_len;
+ while (*value_ptr == '\n') {
+ value_ptr++;
+ value_len--;
+ }
+ prefix_len = cont_prefix_len;
+ write_level = level + 1;
+ xref = NULL;
+ tag = cont_tag;
+ nl_pos = strchr(value_ptr, '\n');
+ }
+ else
+ value_ptr = NULL;
+ }
+ }
+ }
+
+ return 0;
+}
+