GRASS 8 Programmer's Manual  8.5.0dev(2025)-e5e60895a1
commas.c
Go to the documentation of this file.
1 /*!
2  * \file lib/gis/commas.c
3  *
4  * \brief GIS Library - Comma string functions.
5  *
6  * (C) 2001-2014 by the GRASS Development Team
7  *
8  * This program is free software under the GNU General Public License
9  * (>=v2). Read the file COPYING that comes with GRASS for details.
10  *
11  * \author GRASS Development Team
12  *
13  * \date 1999-2014
14  */
15 
16 #include <string.h>
17 #include <grass/gis.h>
18 
19 /**
20  * \brief Inserts commas into a number string.
21  *
22  * Examples:
23  *
24  * - 1234567 becomes 1,234,567
25  * - 1234567.89 becomes 1,234,567.89
26  * - 12345 becomes 12,345
27  * - 1234 stays 1234
28  *
29  * <b>Note:</b> Does not work with negative numbers.
30  *
31  * \param[in,out] buf string
32  * \return 1 if no commas inserted
33  * \return 0 if commas inserted
34  */
35 int G_insert_commas(char *buf)
36 {
37  char number[100];
38  int i, len;
39  int comma;
40 
41  while (*buf == ' ')
42  buf++;
43  G_strlcpy(number, buf, sizeof(number));
44  for (len = 0; number[len]; len++)
45  if (number[len] == '.')
46  break;
47  if (len < 5)
48  return 1;
49 
50  i = 0;
51  if ((comma = len % 3)) {
52  while (i < comma)
53  *buf++ = number[i++];
54  *buf++ = ',';
55  }
56 
57  for (comma = 0; number[i]; comma++) {
58  if (number[i] == '.')
59  break;
60  if (comma && (comma % 3 == 0))
61  *buf++ = ',';
62  *buf++ = number[i++];
63  }
64  while (number[i])
65  *buf++ = number[i++];
66  *buf = 0;
67 
68  return 0;
69 }
70 
71 /**
72  * \brief Removes commas from number string.
73  *
74  * Examples:
75  * - 1,234,567 becomes 1234567<br>
76  * - 1,234,567.89 becomes 1234567.89<br>
77  * - 12,345 becomes 12345<br>
78  * - 1234 stays 1234
79  *
80  * \param[in,out] buf string
81  * \return
82  */
83 void G_remove_commas(char *buf)
84 {
85  char *b;
86 
87  for (b = buf; *b; b++)
88  if (*b != ',')
89  *buf++ = *b;
90 
91  *buf = 0;
92 }
void G_remove_commas(char *buf)
Removes commas from number string.
Definition: commas.c:83
int G_insert_commas(char *buf)
Inserts commas into a number string.
Definition: commas.c:35
size_t G_strlcpy(char *, const char *, size_t)
Safe string copy function.
Definition: strlcpy.c:52
double b
Definition: r_raster.c:39