]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
New function po_header_set_field.
authorBruno Haible <bruno@clisp.org>
Fri, 11 Feb 2005 11:12:17 +0000 (11:12 +0000)
committerBruno Haible <bruno@clisp.org>
Tue, 23 Jun 2009 10:12:12 +0000 (12:12 +0200)
gettext-tools/ChangeLog
gettext-tools/src/ChangeLog
gettext-tools/src/gettext-po.c
gettext-tools/src/gettext-po.h
gettext-tools/windows/gettextpo.def

index f03af03c99916dde8c7f154f7f357dbdbe6f1725..8961964f3dd9d7b26c930496903d2a0d2964220c 100644 (file)
@@ -1,3 +1,7 @@
+2005-02-10  Bruno Haible  <bruno@clisp.org>
+
+       * windows/gettextpo.def: Add po_header_set_field.
+
 2005-02-10  Bruno Haible  <bruno@clisp.org>
 
        * windows/gettextpo.def: Add libgettextpo_version.
index cc81f5f15aaaf35ad94fbb31d26d3995316a31bd..bebf28eba3b18f1b08165bb9e778abc24b60995d 100644 (file)
@@ -1,3 +1,9 @@
+2005-02-10  Bruno Haible  <bruno@clisp.org>
+
+       * gettext-po.h (po_header_set_field): New declaration.
+       * gettext-po.c (po_header_set_field): New function.
+       Suggested by Ross Golder <ross@golder.org>.
+
 2005-02-10  Bruno Haible  <bruno@clisp.org>
 
        * gettext-po.h (LIBGETTEXTPO_VERSION): New macro.
index 8e2413c805b74c3e6ba22b6d39f6b44d3c5e63ca..a8a67319d42ec5f8efe8c3889114525c5f3dcf5c 100644 (file)
@@ -254,19 +254,19 @@ po_file_domain_header (po_file_t file, const char *domain)
 char *
 po_header_field (const char *header, const char *field)
 {
-  size_t len = strlen (field);
+  size_t field_len = strlen (field);
   const char *line;
 
   for (line = header;;)
     {
-      if (strncmp (line, field, len) == 0
-         && line[len] == ':' && line[len + 1] == ' ')
+      if (strncmp (line, field, field_len) == 0
+         && line[field_len] == ':' && line[field_len + 1] == ' ')
        {
          const char *value_start;
          const char *value_end;
          char *value;
 
-         value_start = line + len + 2;
+         value_start = line + field_len + 2;
          value_end = strchr (value_start, '\n');
          if (value_end == NULL)
            value_end = value_start + strlen (value_start);
@@ -289,6 +289,83 @@ po_header_field (const char *header, const char *field)
 }
 
 
+/* Return the header entry with a given field set to a given value.  The field
+   is added if necessary.
+   The return value is a freshly allocated string.  */
+
+char *
+po_header_set_field (const char *header, const char *field, const char *value)
+{
+  size_t header_len = strlen (header);
+  size_t field_len = strlen (field);
+  size_t value_len = strlen (value);
+
+  {
+    const char *line;
+
+    for (line = header;;)
+      {
+       if (strncmp (line, field, field_len) == 0
+           && line[field_len] == ':' && line[field_len + 1] == ' ')
+         {
+           const char *oldvalue_start;
+           const char *oldvalue_end;
+           size_t oldvalue_len;
+           size_t header_part1_len;
+           size_t header_part3_len;
+           size_t result_len;
+           char *result;
+
+           oldvalue_start = line + field_len + 2;
+           oldvalue_end = strchr (oldvalue_start, '\n');
+           if (oldvalue_end == NULL)
+             oldvalue_end = oldvalue_start + strlen (oldvalue_start);
+           oldvalue_len = oldvalue_end - oldvalue_start;
+
+           header_part1_len = oldvalue_start - header;
+           header_part3_len = header + header_len - oldvalue_end;
+           result_len = header_part1_len + value_len + header_part3_len;
+                   /* = header_len - oldvalue_len + value_len */
+           result = (char *) xmalloc (result_len + 1);
+           memcpy (result, header, header_part1_len);
+           memcpy (result + header_part1_len, value, value_len);
+           memcpy (result + header_part1_len + value_len, oldvalue_end,
+                   header_part3_len);
+           *(result + result_len) = '\0';
+
+           return result;
+         }
+
+       line = strchr (line, '\n');
+       if (line != NULL)
+         line++;
+       else
+         break;
+      }
+  }
+  {
+    size_t newline;
+    size_t result_len;
+    char *result;
+
+    newline = (header_len > 0 && header[header_len - 1] != '\n' ? 1 : 0);
+    result_len = header_len + newline + field_len + 2 + value_len + 1;
+    result = (char *) xmalloc (result_len + 1);
+    memcpy (result, header, header_len);
+    if (newline)
+      *(result + header_len) = '\n';
+    memcpy (result + header_len + newline, field, field_len);
+    *(result + header_len + newline + field_len) = ':';
+    *(result + header_len + newline + field_len + 1) = ' ';
+    memcpy (result + header_len + newline + field_len + 2, value, value_len);
+    *(result + header_len + newline + field_len + 2 + value_len) = '\n';
+    *(result + result_len) = '\0';
+
+    return result;
+  }
+}
+
+
 /* Create an iterator for traversing a domain of a PO file in memory.
    The domain NULL denotes the default domain.  */
 
index 2def1ea3b4c21df0993931ba9c5ca1f464f1359d..5f8a6ca0ff01795c2f3c438f8022bf63d45fadc7 100644 (file)
@@ -120,6 +120,11 @@ extern const char * po_file_domain_header (po_file_t file, const char *domain);
    caller, or NULL.  */
 extern char * po_header_field (const char *header, const char *field);
 
+/* Return the header entry with a given field set to a given value.  The field
+   is added if necessary.
+   The return value is a freshly allocated string.  */
+extern char * po_header_set_field (const char *header, const char *field, const char *value);
+
 
 /* ======================= po_message_iterator_t API ======================= */
 
index a0a089004814f15093fb8fc6248435527c943ed1..bd74a9f7bffa28365a59b1742df906175b83cc24 100644 (file)
@@ -11,6 +11,7 @@ po_file_write
 po_filepos_file
 po_filepos_start_line
 po_header_field
+po_header_set_field
 po_message_add_filepos
 po_message_check_format
 po_message_comments