summaryrefslogtreecommitdiff
path: root/libmailutils/base/coord.c
blob: af1fafa5f8c2d17a0b2ac39ac5b9502ba40e8512 (plain)
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
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2020-2021 Free Software Foundation, Inc.

   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
   version 3 of the License, or (at your option) any later version.

   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.

   You should have received a copy of the GNU Lesser General
   Public License along with this library.  If not, see 
   <http://www.gnu.org/licenses/>. */

/*
 * Functions for dealing with message part coordinates.
 */

#include <config.h>
#include <stdlib.h>
#include <errno.h>
#include <mailutils/message.h>

int
mu_coord_alloc (mu_coord_t *ptr, size_t n)
{
  mu_coord_t p = calloc (n + 1, sizeof (p[0]));
  if (!p)
    return errno;
  p[0] = n;
  *ptr = p;
  return 0;
}

int
mu_coord_realloc (mu_coord_t *ptr, size_t n)
{
  if (!ptr)
    return EINVAL;
  if (!*ptr)
    return mu_coord_alloc (ptr, n);
  else
    {
      size_t i = mu_coord_length (*ptr);
      if (i != n)
	{
	  mu_coord_t nc = realloc (*ptr, (n + 1) * sizeof (nc[0]));
	  if (nc == NULL)
	    return ENOMEM;
	  while (++i <= n)
	    nc[i] = 0;
	  nc[0] = n;
	  *ptr = nc;
	}
    }
  return 0;
}      

int
mu_coord_dup (mu_coord_t orig, mu_coord_t *copy)
{
  size_t i, n = mu_coord_length (orig);
  int rc = mu_coord_alloc (copy, n);
  if (rc)
    return rc;
  for (i = 1; i <= n; i++)
    (*copy)[i] = orig[i];
  return 0;
}

static void
revstr (char *s, char *e)
{
  while (s < e)
    {
      char t = *s;
      *s++ = *--e;
      *e = t;
    }
}

char *
mu_coord_part_string (mu_coord_t c, size_t dim)
{
  size_t len = 0;
  size_t i;
  char *result, *p;
  
  for (i = 1; i <= dim; i++)
    {
      size_t n = c[i];
      do
	len++;
      while (n /= 10);
      len++;
    }

  result = malloc (len);
  if (!result)
    return NULL;
  p = result;
  
  for (i = 1; i <= dim; i++)
    {
      char *s;
      size_t n = c[i];
      if (i > 1)
	*p++ = '.';
      s = p;
      do
	{
	  unsigned x = n % 10;
	  *p++ = x + '0';
	}
      while (n /= 10);
      revstr(s, p);
    }
  *p = 0;

  return result;
}

Return to:

Send suggestions and report system problems to the System administrator.