-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.c
More file actions
98 lines (83 loc) · 2.07 KB
/
input.c
File metadata and controls
98 lines (83 loc) · 2.07 KB
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
#include "input.h"
#include <stdio.h>
#include <string.h>
int file2wnodes(const char *filename, WirelessNodes_t *wnodes)
{
char buf[64];
WirelessNode_t nodeNew;
FILE *nodeFile;
// Round 1
nodeFile = fopen(filename, "rb");
if (!nodeFile)
{
return 1;
}
while (fread(buf, 1, ADDR_LENGTH + 1, nodeFile) == ADDR_LENGTH + 1)
{
memcpy(nodeNew.addr, buf, ADDR_LENGTH);
if (buf[ADDR_LENGTH])
nodeNew.isRoot = 1;
else
nodeNew.isRoot = 0;
wnode_addNode(&nodeNew, wnodes);
}
if (!feof(nodeFile))
{
return 2;
}
fclose(nodeFile);
return 0;
}
int file2conns(const char *filename, WirelessNodes_t *wnodes, Conns_t *conns)
{
char buf[64];
FILE *nodeFile;
// Round 2
nodeFile = fopen(filename, "rb");
if (!nodeFile)
{
return 1;
}
while (fread(buf, 1, ADDR_LENGTH * 2 + sizeof(double), nodeFile) == (ADDR_LENGTH * 2 + sizeof(double)))
{
conn_setPdr(conns, wnodes, (unsigned char *)(buf + 0), (unsigned char *)(buf + ADDR_LENGTH), *(double *)(buf + ADDR_LENGTH * 2));
}
if (!feof(nodeFile))
{
return 2;
}
fclose(nodeFile);
return 0;
}
int input_getConfig(SimData_t *SData, const char *filename)
{
char buf[64];
FILE *f;
int r = 0;
// Default values
double thres = 0.918621;
unsigned int maxRetransmitTimes = 4; //首次嘗試 + 重傳3次
unsigned int noImprovementThres = 64;
unsigned int popSize = 1000;
if (filename != 0)
{
f = fopen(filename, "r");
if (!f)
r = 1;
else
{
fgets(buf, sizeof(buf), f);
r = sscanf(buf, "%u,%u,%u,%lf", &popSize, &maxRetransmitTimes, &noImprovementThres, &thres);
if (r == 4)
r = 0;
else
r = 2;
fclose(f);
}
}
SData->thres = thres;
SData->popSize = popSize;
SData->maxRetransmitTimes = maxRetransmitTimes;
SData->noImprovementThres = noImprovementThres;
return r;
}