NimbleSM
NimbleSM is a solid mechanics simulation code for dynamic systems
Loading...
Searching...
No Matches
nimble_parser_util.h
Go to the documentation of this file.
1/*
2//@HEADER
3// ************************************************************************
4//
5// NimbleSM
6// Copyright 2018
7// National Technology & Engineering Solutions of Sandia, LLC (NTESS)
8//
9// Under the terms of Contract DE-NA0003525 with NTESS, the U.S. Government
10// retains certain rights in this software.
11//
12// Redistribution and use in source and binary forms, with or without
13// modification, are permitted provided that the following conditions are
14// met:
15//
16// 1. Redistributions of source code must retain the above copyright
17// notice, this list of conditions and the following disclaimer.
18//
19// 2. Redistributions in binary form must reproduce the above copyright
20// notice, this list of conditions and the following disclaimer in the
21// documentation and/or other materials provided with the distribution.
22//
23// 3. Neither the name of the Corporation nor the names of the
24// contributors may be used to endorse or promote products derived from
25// this software without specific prior written permission.
26//
27// THIS SOFTWARE IS PROVIDED BY NTESS "AS IS" AND ANY EXPRESS OR IMPLIED
28// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
29// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
30// NO EVENT SHALL NTESS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
32// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37//
38// Questions? Contact David Littlewood (djlittl@sandia.gov)
39//
40// ************************************************************************
41//@HEADER
42*/
43
44#ifndef SRC_NIMBLE_PARSER_UTIL_H_
45#define SRC_NIMBLE_PARSER_UTIL_H_
46
47#include <iostream>
48#include <limits>
49#include <sstream>
50#include <string>
51#include <vector>
52
53namespace nimble {
54
55namespace {
56
57inline std::vector<std::string>
58tokenize_string_with_separator(const std::string& s, const char sep)
59{
60 std::vector<std::string> tokens;
61 std::stringstream ss(s);
62 std::string token;
63 while (std::getline(ss >> std::ws, token, sep)) { tokens.push_back(token); }
64 return tokens;
65}
66
67} // namespace
68
69inline std::vector<std::string>
70tokenize_string(const std::string& s)
71{
72 std::vector<std::string> tokens;
73 auto quote_delimited_segments = tokenize_string_with_separator(s, '"');
74 auto is_quoted_segment = [](int i) -> bool { return (i % 2) == 1; };
75 auto quote = quote_delimited_segments.begin();
76 auto quote_end = quote_delimited_segments.end();
77 int i = 0;
78 for (; quote != quote_end; ++quote, ++i) {
79 if (is_quoted_segment(i)) {
80 tokens.push_back(*quote);
81 } else {
82 auto segment_tokens = tokenize_string_with_separator(*quote, ' ');
83 tokens.insert(tokens.end(), segment_tokens.begin(), segment_tokens.end());
84 }
85 }
86 return tokens;
87}
88
89inline double
90string_to_double(const std::string& s)
91{
92 std::stringstream ss;
93 ss << s;
94 double val = std::numeric_limits<double>::max();
95 ss >> val;
96 return val;
97}
98
99} // namespace nimble
100
101#endif /* EXTRAS_SRC_NIMBLE_PARSER_UTIL_H_ */
Definition kokkos_contact_manager.h:49
std::vector< std::string > tokenize_string(const std::string &s)
Definition nimble_parser_util.h:70
double string_to_double(const std::string &s)
Definition nimble_parser_util.h:90