Add macro %isu_package to generate ISU Package
[platform/upstream/rpm.git] / build / misc.c
1 /** \ingroup rpmbuild
2  * \file build/misc.c
3  */
4 #include "system.h"
5
6 #include <ctype.h>
7 #include <stdlib.h>
8 #include <rpm/rpmstring.h>
9 #include "build/rpmbuild_misc.h"
10 #include "debug.h"
11
12 #define BUF_CHUNK 1024
13
14 struct StringBufRec {
15     char *buf;
16     char *tail;     /* Points to first "free" char */
17     int allocated;
18     int free;
19 };
20
21 StringBuf newStringBuf(void)
22 {
23     StringBuf sb = xmalloc(sizeof(*sb));
24
25     sb->free = sb->allocated = BUF_CHUNK;
26     sb->buf = xcalloc(sb->allocated, sizeof(*sb->buf));
27     sb->buf[0] = '\0';
28     sb->tail = sb->buf;
29     
30     return sb;
31 }
32
33 StringBuf freeStringBuf(StringBuf sb)
34 {
35     if (sb) {
36         sb->buf = _free(sb->buf);
37         sb = _free(sb);
38     }
39     return sb;
40 }
41
42 void stripTrailingBlanksStringBuf(StringBuf sb)
43 {
44     while (sb->free != sb->allocated) {
45         if (! risspace(*(sb->tail - 1)))
46             break;
47         sb->free++;
48         sb->tail--;
49     }
50     sb->tail[0] = '\0';
51 }
52
53 const char * getStringBuf(StringBuf sb)
54 {
55     return (sb != NULL) ? sb->buf : NULL;
56 }
57
58 void appendStringBufAux(StringBuf sb, const char *s, int nl)
59 {
60     int l;
61
62     l = strlen(s);
63     /* If free == l there is no room for NULL terminator! */
64     while ((l + nl + 1) > sb->free) {
65         sb->allocated += BUF_CHUNK;
66         sb->free += BUF_CHUNK;
67         sb->buf = xrealloc(sb->buf, sb->allocated);
68         sb->tail = sb->buf + (sb->allocated - sb->free);
69     }
70     
71     /* FIX: shrug */
72     strcpy(sb->tail, s);
73     sb->tail += l;
74     sb->free -= l;
75     if (nl) {
76         sb->tail[0] = '\n';
77         sb->tail[1] = '\0';
78         sb->tail++;
79         sb->free--;
80     }
81 }
82
83 uint32_t parseUnsignedNum(const char * line, uint32_t * res)
84 {
85     char * s1 = NULL;
86     unsigned long rc;
87     uint32_t result;
88
89     if (line == NULL) return 1;
90
91     while (isspace(*line)) line++;
92     if (!isdigit(*line)) return 1;
93
94     rc = strtoul(line, &s1, 10);
95
96     if (*s1 || s1 == line || rc == ULONG_MAX || rc > UINT_MAX)
97         return 1;
98
99     result = (uint32_t)rc;
100     if (res) *res = result;
101
102     return 0;
103 }