Blame view

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

   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
8
   version 3 of the License, or (at your option) any later version.
9 10 11 12 13 14

   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.

15
   You should have received a copy of the GNU Lesser General
16 17
   Public License along with this library.  If not, see
   <http://www.gnu.org/licenses/>. */
18 19 20 21 22 23 24 25 26

#include <mailutils/cpp/address.h>

using namespace mailutils;

//
// Address
//

27 28 29 30 31
Address :: Address ()
{
  addr = NULL;
}

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

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

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

  this->addr = addr;
}

Address :: ~Address ()
{
56 57 58 59 60 61 62 63 64 65 66 67 68 69
  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;
70 71 72
}

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

  return (bool) isgroup;
}

size_t
86
Address :: get_count ()
87 88
{
  size_t count;
89
  mu_address_get_count (addr, &count);
90 91 92 93
  return count;
}

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

103
  return std::string (buf ? buf : "");
104 105 106
}

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

116
  return std::string (buf ? buf : "");
117 118 119
}

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

129
  return std::string (buf ? buf : "");
130 131 132
}

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

142
  return std::string (buf ? buf : "");
143 144 145
}

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

155
  return std::string (buf ? buf : "");
156 157 158
}

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

168 169 170 171 172 173 174 175 176 177 178 179
  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);

180 181 182
  return std::string (buf);
}

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