Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
L
libcifpp
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open
libcifpp
Commits
ce14593f
Unverified
Commit
ce14593f
authored
May 31, 2023
by
Maarten L. Hekkelman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
improved loading resources from absolute path
better error reporting when loading dictionary
parent
1c02a451
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
65 additions
and
55 deletions
+65
-55
src/utilities.cpp
+3
-0
src/validate.cpp
+62
-55
No files found.
src/utilities.cpp
View file @
ce14593f
...
...
@@ -911,6 +911,9 @@ std::unique_ptr<std::istream> resource_pool::load(fs::path name)
if
(
mLocalResources
.
count
(
name
.
string
()))
result
=
open
(
mLocalResources
[
name
.
string
()]);
if
(
fs
::
exists
(
p
,
ec
)
and
not
ec
)
result
=
open
(
p
);
for
(
auto
di
=
mDirs
.
begin
();
not
result
and
di
!=
mDirs
.
end
();
++
di
)
{
auto
p2
=
*
di
/
p
;
...
...
src/validate.cpp
View file @
ce14593f
...
...
@@ -24,10 +24,10 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "cif++/dictionary_parser.hpp"
#include "cif++/validate.hpp"
#include "cif++/
utilities
.hpp"
#include "cif++/
dictionary_parser
.hpp"
#include "cif++/gzio.hpp"
#include "cif++/utilities.hpp"
#include <cassert>
#include <fstream>
...
...
@@ -401,87 +401,94 @@ void validator::report_error(const std::string &msg, bool fatal) const
const
validator
&
validator_factory
::
operator
[](
std
::
string_view
dictionary_name
)
{
std
::
lock_guard
lock
(
m_mutex
);
for
(
auto
&
validator
:
m_validators
)
{
if
(
iequals
(
validator
.
name
(),
dictionary_name
))
return
validator
;
}
// not found, try to see if it helps if we tweak the name a little
// too bad clang version 10 did not have a constructor for std::filesystem::path that accepts a std::string_view
std
::
filesystem
::
path
dictionary
(
dictionary_name
.
data
(),
dictionary_name
.
data
()
+
dictionary_name
.
length
());
if
(
dictionary
.
extension
()
!=
".dic"
)
try
{
auto
dict_name
=
dictionary
.
filename
().
string
()
+
".dic"
;
std
::
lock_guard
lock
(
m_mutex
)
;
for
(
auto
&
validator
:
m_validators
)
{
if
(
iequals
(
validator
.
name
(),
dict_name
))
if
(
iequals
(
validator
.
name
(),
dict
ionary
_name
))
return
validator
;
}
}
// not found, add it
// not found, try to see if it helps if we tweak the name a little
// too bad clang version 10 did not have a constructor for std::filesystem::path that accepts a std::string_view
std
::
filesystem
::
path
dictionary
(
dictionary_name
.
data
(),
dictionary_name
.
data
()
+
dictionary_name
.
length
());
auto
data
=
load_resource
(
dictionary_name
);
if
(
dictionary
.
extension
()
!=
".dic"
)
{
auto
dict_name
=
dictionary
.
filename
().
string
()
+
".dic"
;
if
(
not
data
and
dictionary
.
extension
().
string
()
!=
".dic"
)
data
=
load_resource
(
dictionary
.
parent_path
()
/
(
dictionary
.
filename
().
string
()
+
".dic"
));
for
(
auto
&
validator
:
m_validators
)
{
if
(
iequals
(
validator
.
name
(),
dict_name
))
return
validator
;
}
}
if
(
data
)
construct_validator
(
dictionary_name
,
*
data
);
else
{
std
::
error_code
ec
;
// not found, add it
auto
data
=
load_resource
(
dictionary_name
);
if
(
not
data
and
dictionary
.
extension
().
string
()
!=
".dic"
)
data
=
load_resource
(
dictionary
.
parent_path
()
/
(
dictionary
.
filename
().
string
()
+
".dic"
))
;
// might be a compressed dictionary on disk
std
::
filesystem
::
path
p
=
dictionary
;
if
(
p
.
extension
()
==
".dic"
)
p
=
p
.
parent_path
()
/
(
p
.
filename
().
string
()
+
".gz"
);
if
(
data
)
construct_validator
(
dictionary_name
,
*
data
);
else
p
=
p
.
parent_path
()
/
(
p
.
filename
().
string
()
+
".dic.gz"
);
{
std
::
error_code
ec
;
// might be a compressed dictionary on disk
std
::
filesystem
::
path
p
=
dictionary
;
if
(
p
.
extension
()
==
".dic"
)
p
=
p
.
parent_path
()
/
(
p
.
filename
().
string
()
+
".gz"
);
else
p
=
p
.
parent_path
()
/
(
p
.
filename
().
string
()
+
".dic.gz"
);
#if defined(CACHE_DIR) or defined(DATA_DIR)
if
(
not
std
::
filesystem
::
exists
(
p
,
ec
)
or
ec
)
{
for
(
const
char
*
dir
:
{
if
(
not
std
::
filesystem
::
exists
(
p
,
ec
)
or
ec
)
{
for
(
const
char
*
dir
:
{
#if defined(CACHE_DIR)
CACHE_DIR
,
CACHE_DIR
,
#endif
#if defined(DATA_DIR)
DATA_DIR
DATA_DIR
#endif
})
{
auto
p2
=
std
::
filesystem
::
path
(
dir
)
/
p
;
if
(
std
::
filesystem
::
exists
(
p2
,
ec
)
and
not
ec
)
})
{
swap
(
p
,
p2
);
break
;
auto
p2
=
std
::
filesystem
::
path
(
dir
)
/
p
;
if
(
std
::
filesystem
::
exists
(
p2
,
ec
)
and
not
ec
)
{
swap
(
p
,
p2
);
break
;
}
}
}
}
#endif
if
(
std
::
filesystem
::
exists
(
p
,
ec
)
and
not
ec
)
{
gzio
::
ifstream
in
(
p
);
if
(
std
::
filesystem
::
exists
(
p
,
ec
)
and
not
ec
)
{
gzio
::
ifstream
in
(
p
);
if
(
not
in
.
is_open
())
throw
std
::
runtime_error
(
"Could not open dictionary ("
+
p
.
string
()
+
")"
);
if
(
not
in
.
is_open
())
throw
std
::
runtime_error
(
"Could not open dictionary ("
+
p
.
string
()
+
")"
);
construct_validator
(
dictionary_name
,
in
);
construct_validator
(
dictionary_name
,
in
);
}
else
throw
std
::
runtime_error
(
"Dictionary not found or defined ("
+
dictionary
.
string
()
+
")"
);
}
else
throw
std
::
runtime_error
(
"Dictionary not found or defined ("
+
dictionary
.
string
()
+
")"
);
}
return
m_validators
.
back
();
return
m_validators
.
back
();
}
catch
(
const
std
::
exception
&
ex
)
{
std
::
string
msg
=
"Error while loading dictionary "
;
msg
+=
dictionary_name
;
std
::
throw_with_nested
(
std
::
runtime_error
(
msg
));
}
}
void
validator_factory
::
construct_validator
(
std
::
string_view
name
,
std
::
istream
&
is
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment