PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 0ms 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
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  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. #ifndef __hive_client_helper_h__
  26. #define __hive_client_helper_h__
  27. #include <iostream>
  28. #include "hiveconstants.h"
  29. // TODO: add architecture specific macro definitions here if needed
  30. #ifdef ARCH32
  31. #elif defined(ARCH64)
  32. #else
  33. #endif
  34. /*****************************************************************
  35. * Macro Functions
  36. *****************************************************************/
  37. /**
  38. * @brief A macro that converts a string to a signed 64 bit integer.
  39. *
  40. * Macro will work for both 32 and 64 bit architectures
  41. */
  42. #define ATOI64(val) int64_t(strtoll(val, NULL, 10))
  43. /**
  44. * @brief A macro that converts a string to an unsigned 64 bit integer.
  45. *
  46. * Macro will work for both 32 and 64 bit architectures
  47. */
  48. #define ATOI64U(val) uint64_t(strtoull(val, NULL, 10))
  49. /**
  50. * @brief Convert a Macro'ed value to a string.
  51. *
  52. * Callers should only call STRINGIFY(x) and
  53. * should never use XSTRINGIFY(x)
  54. */
  55. #define STRINGIFY(x) XSTRINGIFY(x)
  56. #define XSTRINGIFY(x) #x
  57. /**
  58. * @brief Finds the number of elements in an array
  59. */
  60. #define LENGTH(arr) (sizeof(arr)/sizeof(arr[0]))
  61. /**
  62. * Checks an error condition, and if true:
  63. * 1. prints the error
  64. * 2. saves the message to err_buf
  65. * 3. returns the specified ret_val
  66. */
  67. #define RETURN_ON_ASSERT(condition, funct_name, error_msg, err_buf, err_buf_len, ret_val) { \
  68. if (condition) { \
  69. cerr << funct_name << ": " << error_msg << endl << flush; \
  70. safe_strncpy(err_buf, error_msg, err_buf_len); \
  71. return ret_val; \
  72. } \
  73. }
  74. /**
  75. * Always performs the following:
  76. * 1. prints the error
  77. * 2. saves the message to err_buf
  78. * 3. returns the specified ret_val
  79. */
  80. #define RETURN_FAILURE(funct_name, error_msg, err_buf, err_buf_len, ret_val) { \
  81. RETURN_ON_ASSERT(true, funct_name, error_msg, err_buf, err_buf_len, ret_val); \
  82. }
  83. /*****************************************************************
  84. * Global Helper Functions
  85. *****************************************************************/
  86. /**
  87. * @brief Convert the name of a HiveType to its actual value.
  88. *
  89. * Returns the corresponding HiveType enum given the name of a Hive data type.
  90. * This function is case sensitive.
  91. * For example: hiveTypeLookup("string") => HIVE_STRING_TYPE
  92. *
  93. * @param hive_type_name Name of a HiveType
  94. * @return The corresponding HiveType
  95. */
  96. HiveType hiveTypeLookup(const char* hive_type_name);
  97. /**
  98. * @brief Safe version of strncpy.
  99. *
  100. * A version of strncpy that guarantees the existance of '\0' at the end of the supplied buffer
  101. * to prevent buffer overruns. Instead of returning dest_buffer like strncpy, safe_strncpy
  102. * returns the number of bytes written to dest_buffer (excluding the null terminator).
  103. *
  104. * @param dest_buffer Buffer to write into.
  105. * @param src_buffer Buffer to copy from.
  106. * @param num The size of the destination buffer in bytes.
  107. *
  108. * @return Number of bytes copied into the destination buffer (excluding the null terminator).
  109. */
  110. size_t safe_strncpy(char* dest_buffer, const char* src_buffer, size_t num);
  111. #endif // __hive_client_helper_h__