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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
/*
* Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
* Copyright (C) 2009 - DIGITEO - Allan CORNET
*
* This file must be used under the terms of the CeCILL.
* This source file is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at
* http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
*
*/
/*--------------------------------------------------------------------------*/
#include <windows.h>
#include <libxml/xpath.h>
#include <libxml/xmlreader.h>
#include "readBlasSpec.h"
#include "xmlEncoding.h"
/*--------------------------------------------------------------------------*/
static char *getshortpathname(wchar_t *wclongpathname, BOOL *convertok);
static wchar_t *GetXmlFileEncoding(const wchar_t *wcfilename);
/*--------------------------------------------------------------------------*/
struct cpu_struct ** readBlasSpec(wchar_t *blasspec_filename, int *sizeArrayReturned)
{
struct cpu_struct **CPUS_BLAS_SPEC = NULL;
if (blasspec_filename)
{
wchar_t *FileEncoding = GetXmlFileEncoding(blasspec_filename);
if (FileEncoding)
{
if (_wcsicmp(FileEncoding, L"UTF-8") == 0)
{
#define XPATH "//blas_specification/cpu"
BOOL bConvert = FALSE;
char *shortfilename = getshortpathname((wchar_t*)blasspec_filename, &bConvert);
if (shortfilename)
{
xmlXPathContextPtr xpathCtxt = NULL;
xmlXPathObjectPtr xpathObj = NULL;
xmlDocPtr BlasSpecxmlDocPtr = xmlParseFile (shortfilename);
free(shortfilename);
shortfilename = NULL;
xmlKeepBlanksDefault(0);
if (BlasSpecxmlDocPtr)
{
xpathCtxt = xmlXPathNewContext(BlasSpecxmlDocPtr);
xpathObj = xmlXPathEval((const xmlChar*)XPATH, xpathCtxt);
if (xpathObj && xpathObj->nodesetval->nodeMax)
{
/* the Xpath has been understood and there are node */
CPUS_BLAS_SPEC = (struct cpu_struct **) malloc(sizeof(struct cpu_struct *) * xpathObj->nodesetval->nodeNr);
if (CPUS_BLAS_SPEC)
{
int i = 0;
for (i = 0; i < xpathObj->nodesetval->nodeNr; i++)
{
xmlAttrPtr attrib = xpathObj->nodesetval->nodeTab[i]->properties;
CPUS_BLAS_SPEC[i] = (struct cpu_struct *) malloc(sizeof(struct cpu_struct));
if (CPUS_BLAS_SPEC[i])
{
while (attrib != NULL)
{
if (xmlStrEqual (attrib->name, (const xmlChar*) "cpu_manufacturer"))
{
/* we found the tag cpu_manufacturer */
CPUS_BLAS_SPEC[i]->cpu_vendor = toWideString((char*)attrib->children->content);
}
if (xmlStrEqual (attrib->name, (const xmlChar*) "cpu_family"))
{
/* we found the tag cpu_family */
if (strcmp((char*)attrib->children->content, "*") == 0)
{
CPUS_BLAS_SPEC[i]->cpu_family = 0;
}
else
{
CPUS_BLAS_SPEC[i]->cpu_family = atoi((char*)attrib->children->content);
}
}
if (xmlStrEqual (attrib->name, (const xmlChar*) "cpu_model"))
{
/* we found the tag cpu_model */
if (strcmp((char*)attrib->children->content, "*") == 0)
{
CPUS_BLAS_SPEC[i]->cpu_model = 0;
}
else
{
CPUS_BLAS_SPEC[i]->cpu_model = atoi((char*)attrib->children->content);
}
}
if (xmlStrEqual (attrib->name, (const xmlChar*) "dll_filename"))
{
/* we found the tag dll_filename */
CPUS_BLAS_SPEC[i]->dll_filename = toWideString((char*)attrib->children->content);
}
if (xmlStrEqual (attrib->name, (const xmlChar*) "comments"))
{
/* we found the tag comments */
CPUS_BLAS_SPEC[i]->comments = toWideString((char*)attrib->children->content);
}
attrib = attrib->next;
}
}
*sizeArrayReturned = i;
}
}
}
if (xpathObj)
{
xmlXPathFreeObject(xpathObj);
}
if (xpathCtxt)
{
xmlXPathFreeContext(xpathCtxt);
}
xmlFreeDoc (BlasSpecxmlDocPtr);
}
}
}
}
free(FileEncoding);
FileEncoding = NULL;
}
return CPUS_BLAS_SPEC;
}
/*--------------------------------------------------------------------------*/
wchar_t *GetXmlFileEncoding(const wchar_t *wcfilename)
{
wchar_t *encoding = NULL;
if (wcfilename)
{
xmlDocPtr doc = NULL;
BOOL bConvert = FALSE;
char *shortfilename = getshortpathname((wchar_t*)wcfilename, &bConvert);
if (shortfilename)
{
doc = xmlParseFile(shortfilename);
free(shortfilename);
shortfilename = NULL;
if (doc)
{
if (doc->encoding)
{
encoding = toWideString((char*)doc->encoding);
}
}
xmlFreeDoc (doc);
}
}
return encoding;
}
/*--------------------------------------------------------------------------*/
char *getshortpathname(wchar_t *wclongpathname, BOOL *convertok)
{
char *ShortName = NULL;
if (wclongpathname)
{
/* first we try to call to know path length */
wchar_t *ptwShortName = NULL;
int length = GetShortPathNameW(wclongpathname, NULL, 0);
if (length <= 0 )
{
length = MAX_PATH;
}
ptwShortName = (wchar_t*)calloc((length + 1), sizeof(wchar_t));
if (ptwShortName)
{
/* second converts path */
if ( GetShortPathNameW(wclongpathname, ptwShortName, length) )
{
ShortName = toUTF(ptwShortName);
*convertok = TRUE;
}
else
{
/* FAILED */
ShortName = toUTF(wclongpathname);
*convertok = FALSE;
}
if (ptwShortName)
{
free(ptwShortName);
ptwShortName = NULL;
}
}
else
{
/* FAILED */
ShortName = toUTF(wclongpathname);
*convertok = FALSE;
}
}
else
{
/* FAILED */
*convertok = FALSE;
}
return ShortName;
}
/*--------------------------------------------------------------------------*/
|