-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
106 lines (97 loc) · 2.91 KB
/
utils.c
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
/********************************************************************
This file is part of ScriptInterpreter.
https://github.com/thfi/ScriptInterpreter
See file "AUTHORS" for a list of copyright holders.
ScriptInterpreter 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 of the
License, or (at your option) any later version.
ScriptInterpreter 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 ScriptInterpreter.
If not, see <http://www.gnu.org/licenses/>.
********************************************************************/
#include "utils.h"
/**
* For a given integer number n, return
* - 1 if n is zero or negative
* - n itself if n is a power of 2
* - the next power of 2 larger than n
*/
int roundup_powerof2(int n)
{
int result = 1;
while (n > 0) {
n /= 2;
result *= 2;
}
return result;
}
/**
* Continue reading from an input file,
* discarding all read data until
* - there is a line break denoted by \n
* - the end of file is reached
*/
void skipline(FILE *input)
{
int c;
while ((c = fgetc(input)) != EOF && c != '\n');
}
/**
* Write a character to an output file.
* Replace special characters like '&' or '<' by
* their escaped form like '&' or '<'.
*/
void xmlized_print(FILE *output, char c)
{
switch (c) {
case '<':
fprintf(output, "<");
break;
case '>':
fprintf(output, ">");
break;
case '&':
fprintf(output, "&");
break;
default:
fprintf(output, "%c", c);
}
}
/**
* Convert a sequence of characters into an integer number.
* The sequence is limited by 'len' or the first appearance
* of a (semi)colon or the null character.
* The function will convert a valid sequence like '6721' to
* the numeric value 6721.
* The function will return -1 if the sequence contains an
* invalid character like 'a' or 'x'.
*/
int ascii_to_dec(char *sequence, int *len)
{
int result = 0;
int left = *len;
*len = 0;
while (left > 0) {
if (*sequence == 0 || *sequence == 0x3b /* semicolon */ || *sequence == 0x3a /* colon */)
break;
if (*sequence >= 0x3c && *sequence <= 0x3f) {
fprintf(stderr, "Parameter string contains 'future use' bits\n");
return -1;
}
if (*sequence < 0x30 /* '0' */ || *sequence > 0x39 /* '9' */) {
fprintf(stderr, "Not a valid number char in sequence\n");
return -1;
}
result *= 10;
result += *sequence - '0';
++sequence;
*len += 1;
--left;
}
return result;
}