event.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2005, Herve Drolon, FreeImage Team
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  15. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  18. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. * POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "opj_includes.h"
  27. /* ==========================================================
  28. Utility functions
  29. ==========================================================*/
  30. #ifdef OPJ_CODE_NOT_USED
  31. #ifndef _WIN32
  32. static char*
  33. i2a(unsigned i, char *a, unsigned r) {
  34. if (i/r > 0) a = i2a(i/r,a,r);
  35. *a = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i%r];
  36. return a+1;
  37. }
  38. /**
  39. Transforms integer i into an ascii string and stores the result in a;
  40. string is encoded in the base indicated by r.
  41. @param i Number to be converted
  42. @param a String result
  43. @param r Base of value; must be in the range 2 - 36
  44. @return Returns a
  45. */
  46. static char *
  47. _itoa(int i, char *a, int r) {
  48. r = ((r < 2) || (r > 36)) ? 10 : r;
  49. if(i < 0) {
  50. *a = '-';
  51. *i2a(-i, a+1, r) = 0;
  52. }
  53. else *i2a(i, a, r) = 0;
  54. return a;
  55. }
  56. #endif /* !_WIN32 */
  57. #endif
  58. /* ----------------------------------------------------------------------- */
  59. opj_event_mgr_t* OPJ_CALLCONV opj_set_event_mgr(opj_common_ptr cinfo, opj_event_mgr_t *event_mgr, void *context) {
  60. if(cinfo) {
  61. opj_event_mgr_t *previous = cinfo->event_mgr;
  62. cinfo->event_mgr = event_mgr;
  63. cinfo->client_data = context;
  64. return previous;
  65. }
  66. return NULL;
  67. }
  68. opj_bool opj_event_msg(opj_common_ptr cinfo, int event_type, const char *fmt, ...) {
  69. #define MSG_SIZE 512 /* 512 bytes should be more than enough for a short message */
  70. opj_msg_callback msg_handler = NULL;
  71. opj_event_mgr_t *event_mgr = cinfo->event_mgr;
  72. if(event_mgr != NULL) {
  73. switch(event_type) {
  74. case EVT_ERROR:
  75. msg_handler = event_mgr->error_handler;
  76. break;
  77. case EVT_WARNING:
  78. msg_handler = event_mgr->warning_handler;
  79. break;
  80. case EVT_INFO:
  81. msg_handler = event_mgr->info_handler;
  82. break;
  83. default:
  84. break;
  85. }
  86. if(msg_handler == NULL) {
  87. return OPJ_FALSE;
  88. }
  89. } else {
  90. return OPJ_FALSE;
  91. }
  92. if ((fmt != NULL) && (event_mgr != NULL)) {
  93. va_list arg;
  94. int str_length/*, i, j*/; /* UniPG */
  95. char message[MSG_SIZE];
  96. /* initialize the optional parameter list */
  97. va_start(arg, fmt);
  98. /* parse the format string and put the result in 'message' */
  99. str_length = vsnprintf(message, MSG_SIZE, fmt, arg); /* UniPG */
  100. /* deinitialize the optional parameter list */
  101. va_end(arg);
  102. /* output the message to the user program */
  103. if( str_length > -1 && str_length < MSG_SIZE )
  104. msg_handler(message, cinfo->client_data);
  105. else return OPJ_FALSE;
  106. }
  107. return OPJ_TRUE;
  108. }