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