summaryrefslogtreecommitdiff
path: root/src/versions.c
blob: a35db6c71378cb8e22c51bc98b2362ebe50009e4 (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
125
126
127
128
129
130
131
/* versions.c - handling standard version numbers
   Copyright 1988-2017 Free Software Foundation, Inc.

   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 3, 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 "a2ps.h"
#include "versions.h"
#include "quotearg.h"
#include "routines.h"

/************************************************************************/
/*	The version handling						*/
/************************************************************************/
void
version_set_to_null (version_t version)
{
  size_t n;

  for (n = 0 ; n < VERSION_LENGTH ; n++)
    version[n] = 0;
}

bool
version_null_p (version_t version)
{
  size_t n;

  for (n = 0 ; n < VERSION_LENGTH ; n++)
    if (version[n])
      return false;
  return true;
}

int
version_cmp (version_t v1, version_t v2)
{
  int n;

  for (n = 0 ; n < VERSION_LENGTH ; n++)
    if (v1[n] < v2[n])
      return -1;
    else if (v1[n] > v2[n])
      return 1;

  return 0;
}

void
version_cpy (version_t d, version_t s)
{
  memcpy (d, s, VERSION_LENGTH * sizeof(s[0]));
}

void
version_self_print (version_t version, FILE * stream)
{
  if (version[2])
    fprintf (stream, "%d.%d%c",
	     version[0], version[1], version[2]);
  else
    fprintf (stream, "%d.%d",
	     version[0], version[1]);
}

/*
 * Return the length occupied by this version number once printed
 */
size_t
version_length (version_t version)
{
#define short_int_len(_i_) ((_i_) < 10 ? 1 : 2)
  if (version[2])
    return 2U
      + short_int_len(version[0])
      + short_int_len(version[1])
      + 1U;
  else
    return 1U
      + short_int_len(version[0])
      + short_int_len(version[1]);
}

void
version_add (version_t v1, version_t v2)
{
  int n;

  for (n = 0 ; n < VERSION_LENGTH ; n++)
    v1[n] += v2[n];
}

/*
 * Valid versions numbers are:
 * digit.digit
 * digit.digitchar
 */
void
string_to_version (const char * version_string, version_t version)
{
  char d;

  switch (sscanf (version_string, "%d.%d%c",
		  &(version[0]), &(version[1]), &d))
    {
    case 2:
      version[2] = 0;
      break;
    case VERSION_LENGTH:
      version[2] = d - 'a' + 1;
      break;
    default:
      error (1, 0,
	     _("invalid version number `%s'"), quotearg (version_string));
      break;
    };
}