event.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * The copyright in this software is being made available under the 2-clauses
  3. * BSD License, included below. This software may be subject to other third
  4. * party and contributor rights, including patent rights, and no such rights
  5. * are granted under this license.
  6. *
  7. * Copyright (c) 2005, Herve Drolon, FreeImage Team
  8. * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
  9. * Copyright (c) 2012, CS Systemes d'Information, France
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  25. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. * POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include "opj_includes.h"
  34. /* ==========================================================
  35. Utility functions
  36. ==========================================================*/
  37. #ifdef OPJ_CODE_NOT_USED
  38. #ifndef _WIN32
  39. static char*
  40. i2a(unsigned i, char *a, unsigned r)
  41. {
  42. if (i / r > 0) {
  43. a = i2a(i / r, a, r);
  44. }
  45. *a = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i % r];
  46. return a + 1;
  47. }
  48. /**
  49. Transforms integer i into an ascii string and stores the result in a;
  50. string is encoded in the base indicated by r.
  51. @param i Number to be converted
  52. @param a String result
  53. @param r Base of value; must be in the range 2 - 36
  54. @return Returns a
  55. */
  56. static char *
  57. _itoa(int i, char *a, int r)
  58. {
  59. r = ((r < 2) || (r > 36)) ? 10 : r;
  60. if (i < 0) {
  61. *a = '-';
  62. *i2a(-i, a + 1, r) = 0;
  63. } else {
  64. *i2a(i, a, r) = 0;
  65. }
  66. return a;
  67. }
  68. #endif /* !_WIN32 */
  69. #endif
  70. /* ----------------------------------------------------------------------- */
  71. /**
  72. * Default callback function.
  73. * Do nothing.
  74. */
  75. static void opj_default_callback(const char *msg, void *client_data)
  76. {
  77. OPJ_ARG_NOT_USED(msg);
  78. OPJ_ARG_NOT_USED(client_data);
  79. }
  80. /* ----------------------------------------------------------------------- */
  81. /* ----------------------------------------------------------------------- */
  82. OPJ_BOOL opj_event_msg(opj_event_mgr_t* p_event_mgr, OPJ_INT32 event_type,
  83. const char *fmt, ...)
  84. {
  85. #define OPJ_MSG_SIZE 512 /* 512 bytes should be more than enough for a short message */
  86. opj_msg_callback msg_handler = 00;
  87. void * l_data = 00;
  88. if (p_event_mgr != 00) {
  89. switch (event_type) {
  90. case EVT_ERROR:
  91. msg_handler = p_event_mgr->error_handler;
  92. l_data = p_event_mgr->m_error_data;
  93. break;
  94. case EVT_WARNING:
  95. msg_handler = p_event_mgr->warning_handler;
  96. l_data = p_event_mgr->m_warning_data;
  97. break;
  98. case EVT_INFO:
  99. msg_handler = p_event_mgr->info_handler;
  100. l_data = p_event_mgr->m_info_data;
  101. break;
  102. default:
  103. break;
  104. }
  105. if (msg_handler == 00) {
  106. return OPJ_FALSE;
  107. }
  108. } else {
  109. return OPJ_FALSE;
  110. }
  111. if ((fmt != 00) && (p_event_mgr != 00)) {
  112. va_list arg;
  113. char message[OPJ_MSG_SIZE];
  114. memset(message, 0, OPJ_MSG_SIZE);
  115. /* initialize the optional parameter list */
  116. va_start(arg, fmt);
  117. /* parse the format string and put the result in 'message' */
  118. vsnprintf(message, OPJ_MSG_SIZE, fmt, arg);
  119. /* force zero termination for Windows _vsnprintf() of old MSVC */
  120. message[OPJ_MSG_SIZE - 1] = '\0';
  121. /* deinitialize the optional parameter list */
  122. va_end(arg);
  123. /* output the message to the user program */
  124. msg_handler(message, l_data);
  125. }
  126. return OPJ_TRUE;
  127. }
  128. void opj_set_default_event_handler(opj_event_mgr_t * p_manager)
  129. {
  130. p_manager->m_error_data = 00;
  131. p_manager->m_warning_data = 00;
  132. p_manager->m_info_data = 00;
  133. p_manager->error_handler = opj_default_callback;
  134. p_manager->info_handler = opj_default_callback;
  135. p_manager->warning_handler = opj_default_callback;
  136. }