PageRenderTime 18ms CodeModel.GetById 10ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.1-rc2/hive/external/odbc/src/cpp/hiveclienthelper.h

#
C Header | 132 lines | 26 code | 21 blank | 85 comment | 1 complexity | 041e509c7cefcaf7f79af0073474a524 MD5 | raw file
  1/**************************************************************************//**
  2 *
  3 * Licensed to the Apache Software Foundation (ASF) under one
  4 * or more contributor license agreements.  See the NOTICE file
  5 * distributed with this work for additional information
  6 * regarding copyright ownership.  The ASF licenses this file
  7 * to you under the Apache License, Version 2.0 (the
  8 * "License"); you may not use this file except in compliance
  9 * with the License.  You may obtain a copy of the License at
 10 *
 11 *   http://www.apache.org/licenses/LICENSE-2.0
 12 *
 13 * Unless required by applicable law or agreed to in writing, software
 14 * distributed under the License is distributed on an "AS IS" BASIS,
 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 16 * See the License for the specific language governing permissions and
 17 * limitations under the License.
 18 *
 19 ******************************************************************************
 20 *
 21 * @file hiveclienthelper.h
 22 * @brief Provides some commonly used functions and macros.
 23 *
 24 *****************************************************************************/
 25
 26
 27#ifndef __hive_client_helper_h__
 28#define __hive_client_helper_h__
 29
 30#include <iostream>
 31
 32#include "hiveconstants.h"
 33
 34
 35// TODO: add architecture specific macro definitions here if needed
 36#ifdef  ARCH32
 37
 38#elif defined(ARCH64)
 39
 40#else
 41
 42#endif
 43
 44/*****************************************************************
 45 * Macro Functions
 46 *****************************************************************/
 47
 48/**
 49 * @brief A macro that converts a string to a signed 64 bit integer.
 50 *
 51 * Macro will work for both 32 and 64 bit architectures
 52 */
 53#define ATOI64(val)  int64_t(strtoll(val, NULL, 10))
 54
 55/**
 56 * @brief A macro that converts a string to an unsigned 64 bit integer.
 57 *
 58 * Macro will work for both 32 and 64 bit architectures
 59 */
 60#define ATOI64U(val) uint64_t(strtoull(val, NULL, 10))
 61
 62/**
 63 * @brief Convert a Macro'ed value to a string.
 64 *
 65 * Callers should only call STRINGIFY(x) and
 66 * should never use XSTRINGIFY(x)
 67 */
 68#define STRINGIFY(x) XSTRINGIFY(x)
 69#define XSTRINGIFY(x) #x
 70
 71/**
 72 * @brief Finds the number of elements in an array
 73 */
 74#define LENGTH(arr) (sizeof(arr)/sizeof(arr[0]))
 75
 76/**
 77 * Checks an error condition, and if true:
 78 * 1. prints the error
 79 * 2. saves the message to err_buf
 80 * 3. returns the specified ret_val
 81 */
 82#define RETURN_ON_ASSERT(condition, funct_name, error_msg, err_buf, err_buf_len, ret_val) {     \
 83  if (condition) {                                                                              \
 84      cerr << funct_name << ": " << error_msg << endl << flush;                                 \
 85      safe_strncpy(err_buf, error_msg, err_buf_len);                                            \
 86      return ret_val;                                                                           \
 87  }                                                                                             \
 88}
 89
 90/**
 91 * Always performs the following:
 92 * 1. prints the error
 93 * 2. saves the message to err_buf
 94 * 3. returns the specified ret_val
 95 */
 96#define RETURN_FAILURE(funct_name, error_msg, err_buf, err_buf_len, ret_val) {  \
 97  RETURN_ON_ASSERT(true, funct_name, error_msg, err_buf, err_buf_len, ret_val); \
 98}
 99
100/*****************************************************************
101 * Global Helper Functions
102 *****************************************************************/
103
104/**
105 * @brief Convert the name of a HiveType to its actual value.
106 *
107 * Returns the corresponding HiveType enum given the name of a Hive data type.
108 * This function is case sensitive.
109 * For example: hiveTypeLookup("string") => HIVE_STRING_TYPE
110 *
111 * @param hive_type_name Name of a HiveType
112 * @return The corresponding HiveType
113 */
114HiveType hiveTypeLookup(const char* hive_type_name);
115
116/**
117 * @brief Safe version of strncpy.
118 *
119 * A version of strncpy that guarantees the existance of '\0' at the end of the supplied buffer
120 * to prevent buffer overruns. Instead of returning dest_buffer like strncpy, safe_strncpy
121 * returns the number of bytes written to dest_buffer (excluding the null terminator).
122 *
123 * @param dest_buffer Buffer to write into.
124 * @param src_buffer  Buffer to copy from.
125 * @param num         The size of the destination buffer in bytes.
126 *
127 * @return Number of bytes copied into the destination buffer (excluding the null terminator).
128 */
129size_t safe_strncpy(char* dest_buffer, const char* src_buffer, size_t num);
130
131
132#endif // __hive_client_helper_h__