bes  Updated for version 3.20.6
focovjson_utils.cc
1 // -*- mode: c++; c-basic-offset:4 -*-
2 //
3 // focovjson_utils.cc
4 //
5 // This file is part of BES CovJSON File Out Module
6 //
7 // Copyright (c) 2018 OPeNDAP, Inc.
8 // Author: Corey Hemphill <hemphilc@oregonstate.edu>
9 // Author: River Hendriksen <hendriri@oregonstate.edu>
10 // Author: Riley Rimer <rrimer@oregonstate.edu>
11 //
12 // Adapted from the File Out JSON module implemented by Nathan Potter
13 //
14 // This library is free software; you can redistribute it and/or
15 // modify it under the terms of the GNU Lesser General Public
16 // License as published by the Free Software Foundation; either
17 // version 2.1 of the License, or (at your option) any later version.
18 //
19 // This library is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 // Lesser General Public License for more details.
23 //
24 // You should have received a copy of the GNU Lesser General Public
25 // License along with this library; if not, write to the Free Software
26 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 //
28 
29 #include "focovjson_utils.h"
30 
31 
32 #include <BESDebug.h>
33 
34 #include <sstream>
35 #include <iomanip>
36 
37 #define utils_debug_key "focovjson"
38 
39 namespace focovjson {
40 
41 void removeSubstring(std::string& str, const std::string subStr) {
42  int n = subStr.length();
43 
44  for (unsigned int i = str.find(subStr); i < str.length(); i = str.find(subStr))
45  str.erase(i, n);
46 }
47 
48 long computeConstrainedShape(libdap::Array *a, std::vector<unsigned int> *shape ) {
49  BESDEBUG(utils_debug_key, "focovjson::computeConstrainedShape() - BEGIN. Array name: "<< a->name() << endl);
50 
51  libdap::Array::Dim_iter dIt;
52  unsigned int start;
53  unsigned int stride;
54  unsigned int stop;
55 
56  unsigned int dimSize = 1;
57  int dimNum = 0;
58  long totalSize = 1;
59 
60  BESDEBUG(utils_debug_key, "focovjson::computeConstrainedShape() - Array has " << a->dimensions(true) << " dimensions."<< endl);
61 
62  for(dIt = a->dim_begin() ; dIt!=a->dim_end() ;dIt++){
63  BESDEBUG(utils_debug_key, "focovjson::computeConstrainedShape() - Processing dimension '" << a->dimension_name(dIt)<< "'. (dim# "<< dimNum << ")"<< endl);
64  start = a->dimension_start(dIt, true);
65  stride = a->dimension_stride(dIt, true);
66  stop = a->dimension_stop(dIt, true);
67  BESDEBUG(utils_debug_key, "focovjson::computeConstrainedShape() - start: " << start << " stride: " << stride << " stop: "<<stop<< endl);
68 
69  dimSize = 1 + ( (stop - start) / stride);
70  BESDEBUG(utils_debug_key, "focovjson::computeConstrainedShape() - dimSize: " << dimSize << endl);
71 
72  (*shape)[dimNum++] = dimSize;
73  totalSize *= dimSize;
74  }
75  BESDEBUG(utils_debug_key, "focovjson::computeConstrainedShape() - totalSize: " << totalSize << endl);
76  BESDEBUG(utils_debug_key, "focovjson::computeConstrainedShape() - END." << endl);
77 
78  return totalSize;
79 }
80 
81 string escape_for_covjson(const std::string &input) {
82  std::stringstream ss;
83  for (size_t i = 0; i < input.length(); ++i) {
84  if (unsigned(input[i]) < '\x20' || input[i] == '\\' || input[i] == '"') {
85  ss << "\\u" << std::setfill('0') << std::setw(4) << std::hex << unsigned(input[i]);
86  } else {
87  ss << input[i];
88  }
89  }
90  return ss.str();
91 }
92 
93 #if 0
94 std::string backslash_escape(std::string source, char char_to_escape) {
95  std::string escaped_result = source;
96  if(source.find(char_to_escape) != string::npos ){
97  size_t found = 0;
98  for(size_t i=0; i< source.length() ; i++){
99  if(source[i] == char_to_escape){
100  escaped_result.insert( i + found++, "\\");
101  }
102  }
103  }
104  return escaped_result;
105 }
106 #endif
107 
108 } /* namespace focovjson */