Blame view

libmu_cpp/address.cc 4.5 KB
1 2
/*
   GNU Mailutils -- a suite of utilities for electronic mail
3 4
   Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software Foundation,
   Inc.
5 6 7 8

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
9
   version 3 of the License, or (at your option) any later version.
10 11 12 13 14 15

   This library 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
   Lesser General Public License for more details.

16 17 18 19
   You should have received a copy of the GNU Lesser General
   Public License along with this library; if not, write to the
   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301 USA
20 21 22 23 24 25 26 27 28 29
*/

#include <mailutils/cpp/address.h>

using namespace mailutils;

//
// Address
//

30 31 32 33 34
Address :: Address ()
{
  addr = NULL;
}

35 36
Address :: Address (const std::string& str)
{
37
  int status = mu_address_create (&addr, str.c_str ());
38 39 40 41
  if (status)
    throw Exception ("Address::Address", status);
}

42 43 44 45 46 47 48
Address :: Address (const char *sv[], size_t len)
{
  int status = mu_address_createv (&addr, sv, len);
  if (status)
    throw Exception ("Address::Address", status);
}

49
Address :: Address (const mu_address_t addr)
50 51 52 53 54 55 56 57 58
{
  if (addr == 0)
    throw Exception ("Address::Address", EINVAL);

  this->addr = addr;
}

Address :: ~Address ()
{
59 60 61 62 63 64 65 66 67 68 69 70 71 72
  if (addr)
    mu_address_destroy (&addr);
}

Address&
Address :: operator = (const Address& a)
{
  if (this != &a)
    {
      if (this->addr)
	mu_address_destroy (&this->addr);
      this->addr = mu_address_dup (a.addr);
    }
  return *this;
73 74 75
}

bool
76
Address :: is_group (size_t n)
77 78
{
  int isgroup;
79
  int status = mu_address_is_group (addr, n, &isgroup);
80
  if (status == EINVAL)
81
    throw Address::EInval ("Address::is_group", status);
82
  else if (status == ENOENT)
83
    throw Address::ENoent ("Address::is_group", status);
84 85 86 87 88

  return (bool) isgroup;
}

size_t
89
Address :: get_count ()
90 91
{
  size_t count;
92
  mu_address_get_count (addr, &count);
93 94 95 96
  return count;
}

std::string
97
Address :: get_email (size_t n)
98
{
Wojciech Polak authored
99 100
  const char* buf = NULL;
  int status = mu_address_sget_email (addr, n, &buf);
101
  if (status == EINVAL)
102
    throw Address::EInval ("Address::get_email", status);
103
  else if (status == ENOENT)
104
    throw Address::ENoent ("Address::get_email", status);
105

106
  return std::string (buf ? buf : "");
107 108 109
}

std::string
110
Address :: get_local_part (size_t n)
111
{
Wojciech Polak authored
112 113
  const char* buf = NULL;
  int status = mu_address_sget_local_part (addr, n, &buf);
114
  if (status == EINVAL)
115
    throw Address::EInval ("Address::get_local_part", status);
116
  else if (status == ENOENT)
117
    throw Address::ENoent ("Address::get_local_part", status);
118

119
  return std::string (buf ? buf : "");
120 121 122
}

std::string
123
Address :: get_domain (size_t n)
124
{
Wojciech Polak authored
125 126
  const char* buf = NULL;
  int status = mu_address_sget_domain (addr, n, &buf);
127
  if (status == EINVAL)
128
    throw Address::EInval ("Address::get_domain", status);
129
  else if (status == ENOENT)
130
    throw Address::ENoent ("Address::get_domain", status);
131

132
  return std::string (buf ? buf : "");
133 134 135
}

std::string
136
Address :: get_personal (size_t n)
137
{
Wojciech Polak authored
138 139
  const char* buf = NULL;
  int status = mu_address_sget_personal (addr, n, &buf);
140
  if (status == EINVAL)
141
    throw Address::EInval ("Address::get_personal", status);
142
  else if (status == ENOENT)
143
    throw Address::ENoent ("Address::get_personal", status);
144

145
  return std::string (buf ? buf : "");
146 147 148
}

std::string
149
Address :: get_comments (size_t n)
150
{
Wojciech Polak authored
151 152
  const char* buf = NULL;
  int status = mu_address_sget_comments (addr, n, &buf);
153
  if (status == EINVAL)
154
    throw Address::EInval ("Address::get_comments", status);
155
  else if (status == ENOENT)
156
    throw Address::ENoent ("Address::get_comments", status);
157

158
  return std::string (buf ? buf : "");
159 160 161
}

std::string
162
Address :: get_route (size_t n)
163
{
Wojciech Polak authored
164 165
  const char* buf = NULL;
  int status = mu_address_sget_route (addr, n, &buf);
166
  if (status == EINVAL)
167
    throw Address::EInval ("Address::get_route", status);
168
  else if (status == ENOENT)
169
    throw Address::ENoent ("Address::get_route", status);
170

171 172 173 174 175 176 177 178 179 180 181 182
  return std::string (buf ? buf : "");
}

std::string
Address :: to_string ()
{
  size_t n;
  char buf[1024];
  int status = mu_address_to_string (addr, buf, sizeof (buf), &n);
  if (status)
    throw Exception ("Address::to_string", status);

183 184 185
  return std::string (buf);
}

186 187 188 189 190 191 192
namespace mailutils
{
  std::ostream& operator << (std::ostream& os, Address& addr) {
    return os << addr.to_string ();
  };
}