-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathnxingest_utils.cpp
More file actions
98 lines (84 loc) · 3.07 KB
/
nxingest_utils.cpp
File metadata and controls
98 lines (84 loc) · 3.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
/* =============================================================================
File Name : nxingest_utils.cpp
Version : 1.7
Component : nxingest
Developer : Laurent Lerusse
e-Science Center - Facility Support - Data Management Group
Purpose : nxingest extract the metadata from a NeXus file to create an
XML file according to a mapping file.
Revision History :
Version 1.0 05/06/2007 First version.
Version 1.3 31/08/2007
Bug correction in test. (Logical OR and not bitwise OR)
Add a filesize calculation function.
Copyright :
nxingest - extraction of metadata from NeXus files into a xml document.
Copyright (C) 2007 STFC e-Science Center
This program 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; version 2 of the License.
This program 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.
============================================================================= */
#include "nxingest_utils.h"
/* Version using stat.h which is not portable.
long fileSize( const string &fname )
{
struct stat fileStat;
int err = stat( fname.c_str(), &fileStat );
if (0 != err) return 0;
return fileStat.st_size;
}
*/
#include <fstream>
long fileSize( const string &fname )
{
/*
Function taken from : http://www.codeproject.com/file/filesize.asp
Reported pitfals :
* The file size may be bigger than can be represented by an int.
* The size of the file may be larger than what is reported.
*/
std::ifstream f;
f.open(fname.c_str(), std::ios_base::binary | std::ios_base::in);
if (!f.good() || f.eof() || !f.is_open()) { return 0; }
f.seekg(0, std::ios_base::beg);
std::ifstream::pos_type begin_pos = f.tellg();
f.seekg(0, std::ios_base::end);
return static_cast<int>(f.tellg() - begin_pos);
}
// *****************************************************************************
// Function : mxmlGetItem
//
// The function will read the text of an xml tag and return it.
// If a string is provided, multiple word will be concatenated
// before returning the string.
//
// *****************************************************************************
char* mxmlGetItem(mxml_node_t *node, char* str){
mxml_node_t *topNode;
topNode = node;
strcpy(str, "");
while( (node = mxmlWalkNext(node, topNode, MXML_DESCEND)) != NULL && mxmlGetType(node) == MXML_TEXT)
{
strcat(str, mxmlGetText(node, NULL));
strcat(str, " ");
}
while(strlen(str) > 0 && str[strlen(str)-1] == ' ')
str[strlen(str)-1] = 0;
return str;
}
char* mxmlGetItem(mxml_node_t *node){
mxml_node_t *topNode;
topNode = node;
while( (node = mxmlWalkNext(node, topNode, MXML_DESCEND)) != NULL )
{
if(mxmlGetType(node) == MXML_TEXT && strlen(mxmlGetText(node, NULL)) > 0)
{
return strdup(mxmlGetText(node, NULL));
}
}
return 0;
}