1 /* GNU Mailutils -- a suite of utilities for electronic mail
2    Copyright (C) 2004-2021 Free Software Foundation, Inc.
3 
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 3 of the License, or (at your option) any later version.
8 
9    This library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General
15    Public License along with this library.  If not, see
16    <http://www.gnu.org/licenses/>. */
17 
18 #include <mailutils/cpp/header.h>
19 
20 using namespace mailutils;
21 
22 //
23 // Header
24 //
25 
Header()26 Header :: Header ()
27 {
28   this->hdr = NULL;
29 }
30 
Header(const mu_header_t hdr)31 Header :: Header (const mu_header_t hdr)
32 {
33   if (hdr == 0)
34     throw Exception ("Header::Header", EINVAL);
35 
36   this->hdr = hdr;
37 }
38 
39 bool
has_key(const std::string & name)40 Header :: has_key (const std::string& name)
41 {
42   const char* buf = NULL;
43 
44   int status = mu_header_sget_value (hdr, name.c_str (), &buf);
45   if (status == MU_ERR_NOENT)
46     return false;
47   else if (status)
48     throw Exception ("Header::has_key", status);
49 
50   return true;
51 }
52 
53 std::string
get_value(const std::string & name)54 Header :: get_value (const std::string& name)
55 {
56   const char* buf = NULL;
57 
58   int status = mu_header_sget_value (hdr, name.c_str (), &buf);
59   if (status)
60     throw Exception ("Header::get_value", status);
61 
62   return std::string (buf);
63 }
64 
65 std::string
get_value(const std::string & name,const std::string & def)66 Header :: get_value (const std::string& name, const std::string& def)
67 {
68   const char* buf = NULL;
69 
70   int status = mu_header_sget_value (hdr, name.c_str (), &buf);
71   if (status == MU_ERR_NOENT)
72     return std::string (def);
73   else if (status)
74     throw Exception ("Header::get_value", status);
75 
76   return std::string (buf);
77 }
78 
79 size_t
size()80 Header :: size ()
81 {
82   size_t c_size;
83   int status = mu_header_size (hdr, &c_size);
84   if (status)
85     throw Exception ("Header::size", status);
86   return c_size;
87 }
88 
89 size_t
lines()90 Header :: lines ()
91 {
92   size_t c_lines;
93   int status = mu_header_lines (hdr, &c_lines);
94   if (status)
95     throw Exception ("Header::lines", status);
96   return c_lines;
97 }
98 
99