MsPASS C++ API  2.4.1.dev4+g92330b7a
Defines the C++ API for MsPASS
Loading...
Searching...
No Matches
Public Member Functions | List of all members
mspass::utility::AttributeCrossReference Class Reference

Cross reference between external and internal names. More...

#include <AttributeCrossReference.h>

Public Member Functions

 AttributeCrossReference ()
 
 AttributeCrossReference (const std::string lines_to_parse)
 
 AttributeCrossReference (const std::list< std::string > &lines)
 
 AttributeCrossReference (const std::map< std::string, std::string > internal2external, const mspass::utility::MetadataList &mdlist)
 
 AttributeCrossReference (const AttributeCrossReference &parent)
 
std::string internal (const std::string key) const
 
std::string external (const std::string key) const
 
MDtype type (const std::string key) const
 
AttributeCrossReferenceoperator= (const AttributeCrossReference &parent)
 
int size () const
 
void put (const std::string intern, const std::string ext)
 
std::set< std::string > internal_names () const
 
std::set< std::string > external_names () const
 

Detailed Description

Cross reference between external and internal names.

Data formats commonly have a frozen namespace with which people are very familiar. An example is SAC where scripts commonly manipulate header attribute by a fixed set of names. For good reasons one may want to use a different naming convention internally in a piece of software that loads data using an external format but wishes to use a different set of names internally. This object simplifies the task of managing the differences in internal and external names

Constructor & Destructor Documentation

◆ AttributeCrossReference() [1/5]

mspass::utility::AttributeCrossReference::AttributeCrossReference ( )
inline

Default constructor.

The default constructor is a pure placeholder that does nothing. Result is a null namespace mapper.

25{};

◆ AttributeCrossReference() [2/5]

mspass::utility::AttributeCrossReference::AttributeCrossReference ( const std::string  lines_to_parse)

Construct from a string.

This constructor assumes the string variable passed is a complete image of a set of (newline separated) lines defining the mapping. The format of each line is assumed to be: internal_name external_name type. As the names imply "internal_name" is the name to use internally and "external_name" is the foramt specific external name. The "type" variable is generic and should be one of the simple keywords real, int, string, or boolean.

Parameters
lines_to_parseis the (multiline) string in the format described above.
Exceptions
MsPASSErrorwill be thrown for parsing errors.
7 {
8 try {
9 istringstream instrm(lines_to_parse);
10 do {
11 string inkey, outkey;
12 string typestr;
13 instrm >> inkey;
14 // unfortunately this is the normal exit
15 if (instrm.eof())
16 break;
17 instrm >> outkey;
18 instrm >> typestr;
19 itoe.insert(pair<string, string>(inkey, outkey));
20 etoi.insert(pair<string, string>(outkey, inkey));
21 if (typestr == "int" || typestr == "INT" || typestr == "integer")
22 imdtypemap.insert(pair<string, MDtype>(inkey, MDtype::Integer));
23 else if (typestr == "real" || typestr == "REAL" || typestr == "double")
24 imdtypemap.insert(pair<string, MDtype>(inkey, MDtype::Double));
25 else if (typestr == "bool" || typestr == "BOOL" || typestr == "boolean")
26 imdtypemap.insert(pair<string, MDtype>(inkey, MDtype::Boolean));
27 else if (typestr == "string" || typestr == "STRING")
28 imdtypemap.insert(pair<string, MDtype>(inkey, MDtype::String));
29 else {
30 /* Note in seispp this condition only logs and error and
31 * sets attribute invalid. Here we throw an exception as
32 * this would always be called on startup and should normally
33 * cause an abort */
34 stringstream sserr;
35 sserr << "AttributeCrossReference string constructor: "
36 << " Attribute with tag=" << inkey << " is tagged with an "
37 << "illegal type name=" << typestr << endl
38 << "Repair input data passed to this constructor" << endl;
39 throw MsPASSError(sserr.str());
40 }
41
42 } while (!instrm.eof());
43 } catch (...) {
44 throw;
45 };
46}
Base class for error object thrown by MsPASS library routines.
Definition MsPASSError.h:38

◆ AttributeCrossReference() [3/5]

mspass::utility::AttributeCrossReference::AttributeCrossReference ( const std::list< std::string > &  lines)

Construct from a list container.

This constructor is nearly identical to the single string with newline constructor. The list elements are expected to be the contents of each line (newline break) for the string version.

Parameters
lineslist container with input lines in same format as that described above for single string constructor.
Exceptions
MsPASSErrorwill be thrown if there are parsing errors.

This constructor was produced by a revisio of the previous to change from a single string to a list container.

50 {
51 list<string>::const_iterator lptr;
52 for (lptr = lines.begin(); lptr != lines.end(); ++lptr) {
53 istringstream instrm(*lptr);
54 string inkey, outkey;
55 string typestr;
56 instrm >> inkey;
57 instrm >> outkey;
58 instrm >> typestr;
59 itoe.insert(pair<string, string>(inkey, outkey));
60 etoi.insert(pair<string, string>(outkey, inkey));
61 if (typestr == "int" || typestr == "INT" || typestr == "integer")
62 imdtypemap.insert(pair<string, MDtype>(inkey, MDtype::Integer));
63 else if (typestr == "real" || typestr == "REAL" || typestr == "double")
64 imdtypemap.insert(pair<string, MDtype>(inkey, MDtype::Double));
65 else if (typestr == "bool" || typestr == "BOOL" || typestr == "boolean")
66 imdtypemap.insert(pair<string, MDtype>(inkey, MDtype::Boolean));
67 else if (typestr == "string" || typestr == "STRING")
68 imdtypemap.insert(pair<string, MDtype>(inkey, MDtype::String));
69 else {
70 stringstream sserr;
71 sserr << "AttributeCrossReference string constructor: "
72 << " Attribute with tag=" << inkey << " is tagged with an "
73 << "illegal type name=" << typestr << endl
74 << "Repair input data passed to this constructor" << endl;
75 throw MsPASSError(sserr.str());
76 }
77 }
78}

◆ AttributeCrossReference() [4/5]

mspass::utility::AttributeCrossReference::AttributeCrossReference ( const std::map< std::string, std::string >  internal2external,
const mspass::utility::MetadataList &  mdlist 
)

Build for a set of STL containers.

This is lower level constructor that effectively builds this object from a set of components that are used to actually implement the concept.

Parameters
internal2externalis an associative array keyed by the internal name that defines external names linked to each internal name.
mdlistis a MsPASS::MetadataList object defining the complete internal namespace.
81 {
82 MetadataList::const_iterator mdlptr;
83 for (mdlptr = mdlist.begin(); mdlptr != mdlist.end(); ++mdlptr) {
84 imdtypemap.insert(pair<string, MDtype>(mdlptr->tag, mdlptr->mdt));
85 }
86 itoe = internal2external;
87 map<string, string>::iterator iptr;
88 /* This extracts each pair of the map and inverts them */
89 for (iptr = itoe.begin(); iptr != itoe.end(); ++iptr)
90 etoi.insert(pair<string, string>(iptr->second, iptr->first));
91}

◆ AttributeCrossReference() [5/5]

mspass::utility::AttributeCrossReference::AttributeCrossReference ( const AttributeCrossReference parent)

Standard copy constructor.

93 {
94 itoe = parent.itoe;
95 etoi = parent.etoi;
96 imdtypemap = parent.imdtypemap;
97}

Member Function Documentation

◆ external()

string mspass::utility::AttributeCrossReference::external ( const std::string  key) const

Get external name for attribute with internal name key.

116 {
117 map<string, string>::const_iterator iptr;
118 iptr = itoe.find(key);
119 if (iptr == itoe.end())
120 throw MsPASSError(string("AttribureCrossReference::external: ") +
121 "Cannot find attribute " + key +
122 " in internal to external namespace map");
123 return (iptr->second);
124}

◆ external_names()

set< string > mspass::utility::AttributeCrossReference::external_names ( ) const

Return the set of external names defined by this object.

Returns an std::set container of strings that are the external names defined by this object.

153 {
154 map<string, string>::const_iterator mptr;
155 set<string> keys;
156 for (mptr = etoi.begin(); mptr != etoi.end(); ++mptr) {
157 keys.insert(mptr->first);
158 }
159 return keys;
160}

◆ internal()

string mspass::utility::AttributeCrossReference::internal ( const std::string  key) const

Get internal name for attribute with external name key.

107 {
108 map<string, string>::const_iterator iptr;
109 iptr = etoi.find(key);
110 if (iptr == etoi.end())
111 throw MsPASSError(string("AttribureCrossReference::internal: ") +
112 "Cannot find attribute " + key +
113 " in external to internal namespace map");
114 return (iptr->second);
115}

◆ internal_names()

set< string > mspass::utility::AttributeCrossReference::internal_names ( ) const

Return the set of internal names defined by this object.

Returns an std::set container of strings that are the internal names defined by this object.

145 {
146 map<string, string>::const_iterator mptr;
147 set<string> keys;
148 for (mptr = itoe.begin(); mptr != itoe.end(); ++mptr) {
149 keys.insert(mptr->first);
150 }
151 return keys;
152}

◆ operator=()

AttributeCrossReference & mspass::utility::AttributeCrossReference::operator= ( const AttributeCrossReference parent)

Standard assignment operator.

99 {
100 if (this != &parent) {
101 itoe = parent.itoe;
102 etoi = parent.etoi;
103 imdtypemap = parent.imdtypemap;
104 }
105 return (*this);
106}

◆ put()

void mspass::utility::AttributeCrossReference::put ( const std::string  intern,
const std::string  ext 
)

Add a new entry to the map.

This method is used to extend the namespace.

Parameters
internis the internal name
extis the external name to be added.
137 {
138 itoe.insert(pair<string, string>(i, e));
139 etoi.insert(pair<string, string>(e, i));
140}

◆ size()

int mspass::utility::AttributeCrossReference::size ( ) const

Return number of entries in the cross reference map.

133 {
134 // Assume the two maps are the same size
135 return (itoe.size());
136}

◆ type()

MDtype mspass::utility::AttributeCrossReference::type ( const std::string  key) const

Get type information for attribute with internal name key.

125 {
126 map<string, MDtype>::const_iterator iptr;
127 iptr = imdtypemap.find(key);
128 if (iptr == imdtypemap.end())
129 throw MsPASSError(string("AttributeCrossReference::type: ") +
130 "Cannot find attribute " + key + " in type definitions");
131 return (iptr->second);
132}

The documentation for this class was generated from the following files: