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
110
111
112
113
114
115
116
117
118
119
|
#include <stdlib.h>
#include <string.h>
#include "parts.h"
/*
part known[] = {
{"XC18V01","f5034093","0ffeffff","alg_18V",0x3FC0},
{"XC2S100","f0614093","0fffffff","alg_VIRTEX",0l},
{NULL,NULL,NULL,NULL,0l}
};
*/
#define TOKDELIM " \t/,;.\n"
part *
text_to_part(char *text)
{
part temp, *ret;
char *token;
token=strtok(text,TOKDELIM);
if (token==NULL) return NULL;
temp.name=strdup(token);
token=strtok(NULL,TOKDELIM);
if (token==NULL) return NULL;
temp.id=strdup(token);
token=strtok(NULL,TOKDELIM);
if (token==NULL) return NULL;
temp.idmask=strdup(token);
token=strtok(NULL, TOKDELIM);
if (token==NULL) return NULL;
temp.alg_tpl=strdup(token);
token=strtok(NULL, TOKDELIM);
if (token==NULL) return NULL;
temp.bsize=strdup(token);
token=strtok(NULL,TOKDELIM);
if (token==NULL) return NULL;
temp.msize=strtol(token,NULL,0);
if((ret=(part *)malloc(sizeof(part)))==NULL)
{
fprintf(stderr,"Cannot asign memory needed for a part\n");
return NULL;
}
*ret=temp;
return ret;
}
int
select_part_from_file(char *name, char *file, part **ret)
{
char buff[1000];
FILE *fp;
if((fp=fopen(file,"rt"))==NULL)
{
fprintf(stderr,"Unable to open devices file %s.\n",file);
return 1;
}
fgets(buff,1000,fp);
while(!feof(fp))
{
if (strncasecmp(buff,"//",2) && (buff[0]!='\n')) /*If its not a comment*/
{ /* or a blank line ...*/
*ret=text_to_part(buff);
if (*ret==NULL)
fprintf(stderr,"Invalid line inside device file: %s\n",buff);
else if (!strcasecmp(name,(*ret)->name))
{ /* FOUND! */
fclose(fp);
return 0;
}
}
fgets(buff,1000,fp);
}
*ret=NULL;
fprintf(stderr,"Device not found inside device file: %s\n",name);
fclose(fp);
return 1;
}
/*
int
select_part_by_name(char *name, part **ret)
{
int found=0;
part *r;
r=known;
while (r->name && !found)
{
if (!strcasecmp(r->name,name))
found=1;
else
r++;
}
if (!found)
{
fprintf(stderr,"Device not found %s\n",name);
return 1;
}
*ret=r;
return 0;
}
*/
|