File: hdrutils.c

package info (click to toggle)
slrn 1.0.3+dfsg-3
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 6,412 kB
  • sloc: ansic: 39,852; sh: 3,777; perl: 467; makefile: 229; sed: 24
file content (134 lines) | stat: -rw-r--r-- 3,312 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 This file is part of SLRN.

 Copyright (c) 2007-2016 John E. Davis <jed@jedsoft.org>

 This program is free software; you can redistribute it and/or modify it
 under the terms of the GNU General Public License as published by the Free
 Software Foundation; either version 2 of the License, or (at your option)
 any later version.

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 more details.

 You should have received a copy of the GNU General Public License along
 with this program; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include "config.h"
#include "slrnfeat.h"
#include <stdio.h>
#include <string.h>
#include <errno.h>
#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif

#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif

#include <slang.h>

#include "group.h"
#include "art.h"
#include "strutil.h"
#include "hdrutils.h"

Slrn_Article_Line_Type *slrn_find_header_line (Slrn_Article_Type *a, char *header)
{
   Slrn_Article_Line_Type *line;
   unsigned char ch = (unsigned char) UPPER_CASE(*header);
   unsigned int len = strlen (header);

   if (a == NULL)
     return NULL;
   line = a->lines;

   while ((line != NULL) && (line->flags & HEADER_LINE))
     {
	unsigned char ch1 = (unsigned char) *line->buf;
	if ((ch == UPPER_CASE(ch1))
	    && (0 == slrn_case_strncmp (header, line->buf, len)))
	  return line;
	line = line->next;
     }
   return NULL;
}

/* Here it is assumed that buf is already allocated.  This routine steals
 * it, unless an error occurs, in which case it will return NULL.
 * If buf is NULL, then the header separator line will be added if it is not
 * already present.
 */

Slrn_Article_Line_Type *slrn_append_to_header (Slrn_Article_Type *a, char *buf, int free_on_error)
{
   Slrn_Article_Line_Type *hline, *bline;
   Slrn_Article_Line_Type *line;
   unsigned int flags = HEADER_LINE;

   hline = a->lines;
   bline = NULL;
   if (hline != NULL)
     {
	while ((hline != NULL)
	       && (hline->flags & HEADER_LINE)
	       && (NULL != (bline = hline->next))
	       && (bline->flags & HEADER_LINE))
	  hline = bline;
     }

   if (buf == NULL)
     {
	/* header separator */
	if ((bline != NULL) && (bline->buf[0] == 0))
	  return a->cline = bline;

	if (NULL == (buf = slrn_strmalloc ("", 1)))
	  return NULL;

	flags = 0;
     }

   line = (Slrn_Article_Line_Type *) slrn_malloc(sizeof(Slrn_Article_Line_Type),1,1);
   if (line == NULL)
     {
	if (free_on_error)
	  slrn_free (buf);
	return NULL;
     }

   line->flags = flags;
   line->buf = buf;
   if (hline == NULL)
     a->lines = line;
   else
     hline->next = line;

   line->prev = hline;
   line->next = bline;

   if (bline != NULL)
     bline->prev = line;

   return a->cline = line;
}

Slrn_Article_Line_Type *slrn_append_header_keyval (Slrn_Article_Type *a, char *key, char *value)
{
   unsigned int buflen;
   char *buf;

   buflen = strlen (key) + strlen(value) + 3;
   if (NULL == (buf = slrn_malloc (buflen, 0, 1)))
     return NULL;

   (void) SLsnprintf (buf, buflen, "%s: %s", key, value);

   return slrn_append_to_header (a, buf, 1);
}