source: proiecte/HadoopJUnit/hadoop-0.20.1/src/c++/utils/impl/StringUtils.cc @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 5.0 KB
Line 
1/**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18#include "hadoop/StringUtils.hh"
19#include "hadoop/SerialUtils.hh"
20
21#include <errno.h>
22#include <stdint.h>
23#include <stdio.h>
24#include <strings.h>
25#include <sys/time.h>
26
27using std::string;
28using std::vector;
29
30namespace HadoopUtils {
31
32  string toString(int32_t x) {
33    char str[100];
34    sprintf(str, "%d", x);
35    return str;
36  }
37
38  int toInt(const string& val) {
39    int result;
40    char trash;
41    int num = sscanf(val.c_str(), "%d%c", &result, &trash);
42    HADOOP_ASSERT(num == 1,
43                  "Problem converting " + val + " to integer.");
44    return result;
45  }
46
47  float toFloat(const string& val) {
48    float result;
49    char trash;
50    int num = sscanf(val.c_str(), "%f%c", &result, &trash);
51    HADOOP_ASSERT(num == 1,
52                  "Problem converting " + val + " to float.");
53    return result;
54  }
55
56  bool toBool(const string& val) {
57    if (val == "true") {
58      return true;
59    } else if (val == "false") {
60      return false;
61    } else {
62      HADOOP_ASSERT(false,
63                    "Problem converting " + val + " to boolean.");
64    }
65  }
66
67  /**
68   * Get the current time in the number of milliseconds since 1970.
69   */
70  uint64_t getCurrentMillis() {
71    struct timeval tv;
72    struct timezone tz;
73    int sys = gettimeofday(&tv, &tz);
74    HADOOP_ASSERT(sys != -1, strerror(errno));
75    return tv.tv_sec * 1000 + tv.tv_usec / 1000;
76  }
77
78  vector<string> splitString(const std::string& str,
79                             const char* separator) {
80    vector<string> result;
81    string::size_type prev_pos=0;
82    string::size_type pos=0;
83    while ((pos = str.find_first_of(separator, prev_pos)) != string::npos) {
84      if (prev_pos < pos) {
85        result.push_back(str.substr(prev_pos, pos-prev_pos));
86      }
87      prev_pos = pos + 1;
88    }
89    if (prev_pos < str.size()) {
90      result.push_back(str.substr(prev_pos));
91    }
92    return result;
93  }
94
95  string quoteString(const string& str,
96                     const char* deliminators) {
97   
98    string result(str);
99    for(int i=result.length() -1; i >= 0; --i) {
100      char ch = result[i];
101      if (!isprint(ch) ||
102          ch == '\\' || 
103          strchr(deliminators, ch)) {
104        switch (ch) {
105        case '\\':
106          result.replace(i, 1, "\\\\");
107          break;
108        case '\t':
109          result.replace(i, 1, "\\t");
110          break;
111        case '\n':
112          result.replace(i, 1, "\\n");
113          break;
114        case ' ':
115          result.replace(i, 1, "\\s");
116          break;
117        default:
118          char buff[4];
119          sprintf(buff, "\\%02x", static_cast<unsigned char>(result[i]));
120          result.replace(i, 1, buff);
121        }
122      }
123    }
124    return result;
125  }
126
127  string unquoteString(const string& str) {
128    string result(str);
129    string::size_type current = result.find('\\');
130    while (current != string::npos) {
131      if (current + 1 < result.size()) {
132        char new_ch;
133        int num_chars;
134        if (isxdigit(result[current+1])) {
135          num_chars = 2;
136          HADOOP_ASSERT(current + num_chars < result.size(),
137                     "escape pattern \\<hex><hex> is missing second digit in '"
138                     + str + "'");
139          char sub_str[3];
140          sub_str[0] = result[current+1];
141          sub_str[1] = result[current+2];
142          sub_str[2] = '\0';
143          char* end_ptr = NULL;
144          long int int_val = strtol(sub_str, &end_ptr, 16);
145          HADOOP_ASSERT(*end_ptr == '\0' && int_val >= 0,
146                     "escape pattern \\<hex><hex> is broken in '" + str + "'");
147          new_ch = static_cast<char>(int_val);
148        } else {
149          num_chars = 1;
150          switch(result[current+1]) {
151          case '\\':
152            new_ch = '\\';
153            break;
154          case 't':
155            new_ch = '\t';
156            break;
157          case 'n':
158            new_ch = '\n';
159            break;
160          case 's':
161            new_ch = ' ';
162            break;
163          default:
164            string msg("unknow n escape character '");
165            msg += result[current+1];
166            HADOOP_ASSERT(false, msg + "' found in '" + str + "'");
167          }
168        }
169        result.replace(current, 1 + num_chars, 1, new_ch);
170        current = result.find('\\', current+1);
171      } else {
172        HADOOP_ASSERT(false, "trailing \\ in '" + str + "'");
173      }
174    }
175    return result;
176  }
177
178}
Note: See TracBrowser for help on using the repository browser.