-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cpp
109 lines (100 loc) · 2.12 KB
/
utils.cpp
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
#include "utils.h"
#include "math.h"
bool checktype(frame* f, char c)//used in global analysis
{
if (c == '0') return true; //TOT
if (c == '1') return ((f->type == 'K') || (f->type == 'I')); //K+I
if (c == '2') return ((f->type == 'P') || (f->type == 'B')); //P+B
return (f->type == c);
}
int readline(FILE* stream, char* buffer, int buf_size) { //standard pipe
//Legge i caratteri finchè non trova '\n'
//ritorna il numero di caratteri letti (eventualmente 0)
//ritorna -1 in caso di errore o EOF
//ritorna -2 in caso di overflow
int ccounter = 0;
int c;
char* cptr = buffer;
while (1)
{
if (ccounter == buf_size - 1) return -2;
c = fgetc(stream);
if (c == EOF)
{
*cptr = 0;
return -1;
}
if ((c == '\n') || (c == 0))
{
*cptr = 0;
return ccounter;
}
*cptr = c;
cptr++;
ccounter++;
}
}
int readline(HANDLE stream, char* buffer, int buf_size) { //named pipe
//Legge i caratteri finchè non trova '\n'
//ritorna il numero di caratteri letti (eventualmente 0)
//ritorna -1 in caso di errore o EOF
//ritorna -2 in caso di overflow
int ccounter = 0;
//int c;
char* cptr = buffer;
while (1)
{
if (ccounter == buf_size - 1) return -2;
if (!ReadFile(stream, cptr, 1, NULL, NULL))
return -1;
if ((*cptr == '\n') || (*cptr == 0))
{
*cptr = 0;
return ccounter;
}
cptr++;
ccounter++;
}
}
int64_t sttoint(char* pp)
{
int64_t segno = 1;
int64_t n = 0;
if (*pp == '-')
{
segno = -1;
pp++;
}
while ((*pp - '0' >= 0) && (*pp - '0' < 10))
{
n *= 10;
n += *pp - '0';
pp++;
}
return segno * n;
}
double sttodouble(char* pp)
{
int64_t segno = 1;
int expn = 0;
double n = 0;
int nn = -1;
if (*pp == '-') { segno = -1; pp++; }
while ((*pp - '0' >= 0) && (*pp - '0' < 10))
{
n *= 10; n += *pp - '0';
pp++;
}
if ((*pp == '.') || (*pp == ',')) pp++;
while ((*pp - '0' >= 0) && (*pp - '0' < 10))
{
n += (*pp - '0') * pow(10.0, nn);
nn--;
pp++;
}
if ((*pp == 'e') || (*pp == 'E'))
{
expn = (int) (sttoint(pp + 1));
}
return segno * n * pow(10.0, expn);
}