1 /* Message list header manipulation.
2 Copyright (C) 2007 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2007.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "msgl-header.h"
30 #define SIZEOF(a) (sizeof(a) / sizeof(a[0]))
34 msgdomain_list_set_header_field (msgdomain_list_ty *mdlp,
35 const char *field, const char *value)
37 /* The known fields in their usual order. */
45 { "Project-Id-Version:", sizeof ("Project-Id-Version:") - 1 },
46 { "Report-Msgid-Bugs-To:", sizeof ("Report-Msgid-Bugs-To:") - 1 },
47 { "POT-Creation-Date:", sizeof ("POT-Creation-Date:") - 1 },
48 { "PO-Revision-Date:", sizeof ("PO-Revision-Date:") - 1 },
49 { "Last-Translator:", sizeof ("Last-Translator:") - 1 },
50 { "Language-Team:", sizeof ("Language-Team:") - 1 },
51 { "Language:", sizeof ("Language:") - 1 },
52 { "MIME-Version:", sizeof ("MIME-Version:") - 1 },
53 { "Content-Type:", sizeof ("Content-Type:") - 1 },
54 { "Content-Transfer-Encoding:",
55 sizeof ("Content-Transfer-Encoding:") - 1 }
61 field_len = strlen (field);
63 /* Search the field in known_fields[]. */
65 for (k = 0; k < SIZEOF (known_fields); k++)
66 if (strcmp (known_fields[k].name, field) == 0)
72 for (i = 0; i < mdlp->nitems; i++)
74 message_list_ty *mlp = mdlp->item[i]->messages;
77 /* Search the header entry. */
78 for (j = 0; j < mlp->nitems; j++)
79 if (is_header (mlp->item[j]) && !mlp->item[j]->obsolete)
81 message_ty *mp = mlp->item[j];
83 /* Modify the header entry. */
84 const char *header = mp->msgstr;
86 XNMALLOC (strlen (header) + 1
87 + strlen (field) + 1 + strlen (value) + 1 + 1,
90 /* Test whether the field already occurs in the header entry. */
93 for (h = header; *h != '\0'; )
95 if (strncmp (h, field, field_len) == 0)
102 if (h != NULL && *h != '\0')
104 /* Replace the field. */
105 char *p = new_header;
106 memcpy (p, header, h - header);
108 p = stpcpy (p, field);
109 p = stpcpy (stpcpy (stpcpy (p, " "), value), "\n");
110 h = strchr (h, '\n');
117 else if (field_index < 0)
119 /* An unknown field. Append it at the end. */
120 char *p = new_header;
121 p = stpcpy (p, header);
122 if (p > new_header && p[-1] != '\n')
124 p = stpcpy (p, field);
125 stpcpy (stpcpy (stpcpy (p, " "), value), "\n");
129 /* Find the appropriate position for inserting the field. */
130 for (h = header; *h != '\0'; )
132 /* Test whether h starts with a field name whose index is
134 for (k = field_index + 1; k < SIZEOF (known_fields); k++)
135 if (strncmp (h, known_fields[k].name, known_fields[k].len)
138 if (k < SIZEOF (known_fields))
140 h = strchr (h, '\n');
145 if (h != NULL && *h != '\0')
147 /* Insert the field at position h. */
148 char *p = new_header;
149 memcpy (p, header, h - header);
151 p = stpcpy (p, field);
152 p = stpcpy (stpcpy (stpcpy (p, " "), value), "\n");
157 /* Append it at the end. */
158 char *p = new_header;
159 p = stpcpy (p, header);
160 if (p > new_header && p[-1] != '\n')
162 p = stpcpy (p, field);
163 stpcpy (stpcpy (stpcpy (p, " "), value), "\n");
167 mp->msgstr = new_header;