1 #include "InteractionsParser.h"
2 #include "StringTokenizer.h"
8 InteractionsParser::InteractionsParser(const char *ini_filename
)
9 : _ini(false, true, false)
11 SI_Error rc
= _ini
.LoadFile(ini_filename
);
13 throw runtime_error("Failed loading interaction ini file: " +
14 CSimpleIni::str_SI_Error(rc
));
16 const CSimpleIni::TKeyVal
*section
= _ini
.GetSection("Interactions");
18 throw runtime_error("Interactions ini file is missing "
19 "'Interactions' section.");
21 CSimpleIni::TNamesDepend interactionTypes
;
22 _ini
.GetAllKeys("Interactions", interactionTypes
);
24 for (CSimpleIni::TNamesDepend::const_iterator key
=
25 interactionTypes
.begin();
26 key
!= interactionTypes
.end(); ++key
) {
28 CSimpleIni::TNamesDepend interactions
;
29 _ini
.GetAllValues("Interactions", key
->pItem
, interactions
);
31 for (CSimpleIni::TNamesDepend::const_iterator value
=
33 value
!= interactions
.end(); ++value
) {
34 parseInteraction(key
, value
);
40 void InteractionsParser::parseInteraction(
41 CSimpleIni::TNamesDepend::const_iterator key
,
42 CSimpleIni::TNamesDepend::const_iterator value
)
44 StringTokenizer strtok
= StringTokenizer(value
->pItem
, ",");
45 int cnt
= strtok
.countTokens();
47 throw runtime_error("Empty \"" + string(key
->pItem
)
49 Interaction interaction
;
50 interaction
.name
= "";
52 for (int i
= 0; i
< cnt
; i
++) {
53 double x_frac
, y_frac
; // interactions may be given as absolute coordinates and scaled to unit cell size later!
54 string tmp
= strtok
.nextToken();
55 if (sscanf(tmp
.c_str(), "%lf/%lf", &x_frac
, &y_frac
) != 2 ||
56 x_frac
< 0 || y_frac
< 0) {
57 // Treat last tuple as comment
59 interaction
.name
= tmp
;
61 throw runtime_error("Skrewed fraction coordinates: " +
64 interaction
.directions
.push_back(
65 Direction(x_frac
, y_frac
));
67 // TODO: check cardinality (trio, etc...)
68 _interactions
.push_back(interaction
);
71 Interactions
InteractionsParser::getInteractions()