IOSS 2.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Iotm_TextMeshFuncs.h
Go to the documentation of this file.
1// Copyright(C) 1999-2020, 2022, 2023, 2025 National Technology & Engineering Solutions
2// of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3// NTESS, the U.S. Government retains certain rights in this software.
4
5#pragma once
6
7// ####################### Start Clang Header Tool Managed Headers ########################
8// clang-format off
9#include <cctype> // for toupper
10#include <cstddef> // for size_t
11#include <algorithm> // for remove, etc
12#include <iterator> // for insert_iterator
13#include <set> // for set
14#include <string> // for basic_string, etc
15#include <utility> // for pair
16#include <vector> // for vector
17#include <sstream> // for ostringstream
18#include <iostream>
19#include <stdexcept>
20#include <numeric>
21
22#if defined(_WIN32) && !defined(__MINGW32__)
23#include <string.h>
24#define strcasecmp _stricmp
25#define strncasecmp _strnicmp
26#else
27#include <strings.h>
28#endif
29
30// clang-format on
31// ####################### End Clang Header Tool Managed Headers ########################
32namespace Iotm {
33 namespace text_mesh {
34
35 template <class EXCEPTION> void handle_error(const std::ostringstream &message)
36 {
37 throw EXCEPTION((message).str());
38 }
39
40 inline void default_error_handler(const std::ostringstream &message)
41 {
43 }
44
45 template <class ForwardIt, class T>
46 ForwardIt bound_search(ForwardIt first, ForwardIt last, const T &value)
47 {
48 first = std::lower_bound(first, last, value);
49 if (!(first == last) && !(value < *first))
50 return first;
51
52 return last;
53 }
54
55 template <class ForwardIt, class T, class Compare>
56 ForwardIt bound_search(ForwardIt first, ForwardIt last, const T &value, Compare comp)
57 {
58 first = std::lower_bound(first, last, value, comp);
59 if (!(first == last) && !(comp(value, *first)))
60 return first;
61
62 return last;
63 }
64
65 inline std::string strip_whitespace(const std::string &inpt)
66 {
67 auto start_it = inpt.begin();
68 auto end_it = inpt.rbegin();
69 while (std::isspace(*start_it))
70 ++start_it;
71 while (std::isspace(*end_it))
72 ++end_it;
73 return std::string(start_it, end_it.base());
74 }
75
76 inline std::vector<std::string> get_tokens(const std::string &str,
77 const std::string &separators)
78 {
79 std::vector<std::string> tokens;
80 auto first = std::begin(str);
81 while (first != std::end(str)) {
82 const auto second =
83 std::find_first_of(first, std::end(str), std::begin(separators), std::end(separators));
84 if (first != second) {
85 std::string token = strip_whitespace(std::string(first, second));
86 tokens.emplace_back(token);
87 }
88 if (second == std::end(str)) {
89 break;
90 }
91 first = std::next(second);
92 }
93 return tokens;
94 }
95
96 inline void convert_to_uppercase(std::string &str)
97 {
98 std::transform(str.begin(), str.end(), str.begin(), ::toupper);
99 }
100
101 inline void convert_to_lowercase(std::string &str)
102 {
103 std::transform(str.begin(), str.end(), str.begin(), ::tolower);
104 }
105
106 inline bool is_positive_number(const std::string &str)
107 {
108 for (char const &c : str) {
109 if (std::isdigit(c) == 0)
110 return false;
111 }
112 return true;
113 }
114
115 template <typename T> std::set<T> transform_to_set(const std::vector<T> &dataAsVector)
116 {
117 std::set<T> dataAsSet;
118
119 for (const T &data : dataAsVector) {
120 dataAsSet.insert(data);
121 }
122
123 return dataAsSet;
124 }
125
126 inline std::pair<unsigned, bool> get_id_from_part_name(const std::string &name,
127 const std::string &prefix)
128 {
129 const unsigned prefixLength = prefix.length();
130
131 if (name.length() < prefixLength + 1)
132 return std::make_pair(0, false);
133
134 const std::string namePrefix = name.substr(0, prefixLength);
135 const std::string nameSuffix = name.substr(prefixLength);
136
137 if (strcasecmp(namePrefix.c_str(), prefix.c_str()) != 0)
138 return std::make_pair(0, false);
139
140 unsigned id;
141 std::istringstream nameSuffixStream(nameSuffix);
142 nameSuffixStream >> id;
143 if (nameSuffixStream.fail()) {
144 return std::make_pair(0, false);
145 }
146 return std::make_pair(id, true);
147 }
148
149 } // namespace text_mesh
150} // namespace Iotm
Definition Iotm_TextMeshAdjacencyGraph.h:39
void handle_error(const std::ostringstream &message)
Definition Iotm_TextMeshFuncs.h:35
bool is_positive_number(const std::string &str)
Definition Iotm_TextMeshFuncs.h:106
ForwardIt bound_search(ForwardIt first, ForwardIt last, const T &value)
Definition Iotm_TextMeshFuncs.h:46
void convert_to_uppercase(std::string &str)
Definition Iotm_TextMeshFuncs.h:96
void convert_to_lowercase(std::string &str)
Definition Iotm_TextMeshFuncs.h:101
std::vector< std::string > get_tokens(const std::string &str, const std::string &separators)
Definition Iotm_TextMeshFuncs.h:76
void default_error_handler(const std::ostringstream &message)
Definition Iotm_TextMeshFuncs.h:40
std::set< T > transform_to_set(const std::vector< T > &dataAsVector)
Definition Iotm_TextMeshFuncs.h:115
std::pair< unsigned, bool > get_id_from_part_name(const std::string &name, const std::string &prefix)
Definition Iotm_TextMeshFuncs.h:126
std::string strip_whitespace(const std::string &inpt)
Definition Iotm_TextMeshFuncs.h:65
A namespace for the textmesh database format.
Definition Iotm_DatabaseIO.C:95