ODPrntf.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* OpenDoors Online Software Programming Toolkit
  2. * (C) Copyright 1991 - 1999 by Brian Pirie.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. *
  19. * File: ODPrntf.c
  20. *
  21. * Description: Implements the od_printf() function.
  22. *
  23. * Revisions: Date Ver Who Change
  24. * ---------------------------------------------------------------
  25. * Oct 13, 1994 6.00 BP New file header format.
  26. * Dec 09, 1994 6.00 BP Standardized coding style.
  27. * Nov 11, 1995 6.00 BP Removed register keyword.
  28. * Nov 14, 1995 6.00 BP 32-bit portability.
  29. * Nov 16, 1995 6.00 BP Removed oddoor.h, added odcore.h.
  30. * Dec 12, 1995 6.00 BP Added entry, exit and kernel macros.
  31. * Dec 30, 1995 6.00 BP Added ODCALL for calling convention.
  32. * Jan 03, 1996 6.00 BP Use ODVCALL instead of ODCALL.
  33. * Jan 04, 1996 6.00 BP Add missing OD_API_EXIT() at end.
  34. * Feb 19, 1996 6.00 BP Changed version number to 6.00.
  35. * Mar 03, 1996 6.10 BP Begin version 6.10.
  36. * Aug 10, 2003 6.23 SH *nix support
  37. */
  38. #define BUILDING_OPENDOORS
  39. #include <stdio.h>
  40. #include <stdarg.h>
  41. #include <string.h>
  42. #include <stdlib.h>
  43. #include "OpenDoor.h"
  44. #include "ODCore.h"
  45. #include "ODGen.h"
  46. #include "ODKrnl.h"
  47. /* Size of od_printf() working buffer. Adjust this upwards if you are */
  48. /* encountering difficulties when calling od_printf() with long strings. */
  49. #define WORK_BUFFER_SIZE 2048
  50. /* ----------------------------------------------------------------------------
  51. * od_printf()
  52. *
  53. * The OpenDoors equivalent of the C printf() function, this function performs
  54. * formatted string output to both the local and remote screens.
  55. *
  56. * Parameters: pszFormat - Format string, in the same format as the printf()
  57. * format string.
  58. *
  59. * The semantics of any further parameters are dicated by the
  60. * contents of the format string.
  61. *
  62. * Return: void
  63. */
  64. ODAPIDEF void ODVCALL od_printf(const char *pszFormat,...)
  65. {
  66. va_list pArgumentList;
  67. static char pszWorkBuffer[WORK_BUFFER_SIZE];
  68. char *pchCurrent;
  69. char *pchStart;
  70. BOOL bNotFound;
  71. INT nCharCount;
  72. /* Log function entry if running in trace mode. */
  73. TRACE(TRACE_API, "od_printf()");
  74. /* Initialize OpenDoors if it hasn't already been done. */
  75. if(!bODInitialized) od_init();
  76. OD_API_ENTRY();
  77. /* Use static work buffer. */
  78. /* Copy the arguments after the format string. */
  79. va_start(pArgumentList, pszFormat);
  80. /* Perform a string printf to the working buffer. */
  81. vsnprintf(pszWorkBuffer, WORK_BUFFER_SIZE, pszFormat, pArgumentList);
  82. va_end(pArgumentList);
  83. /* If no color characters are defined, then just display the entire */
  84. /* buffer in one shot. */
  85. if(!od_control.od_color_char && !od_control.od_color_delimiter)
  86. goto quick_print;
  87. chColorCheck = od_control.od_color_delimiter;
  88. bNotFound = TRUE;
  89. pchCurrent = (char *)pszWorkBuffer;
  90. pchStart = (char *)pszWorkBuffer;
  91. nCharCount = 0;
  92. while(*pchCurrent)
  93. {
  94. if(*pchCurrent == od_control.od_color_delimiter)
  95. {
  96. bNotFound = FALSE;
  97. if(nCharCount != 0)
  98. {
  99. od_disp(pchStart, nCharCount, TRUE);
  100. }
  101. if(!*(++pchCurrent))
  102. {
  103. chColorCheck = 0;
  104. OD_API_EXIT();
  105. return;
  106. }
  107. od_set_attrib(od_color_config(pchCurrent));
  108. if(!*(pchCurrent = (char *)pchColorEndPos))
  109. {
  110. chColorCheck = 0;
  111. OD_API_EXIT();
  112. return;
  113. }
  114. if(!*(++pchCurrent))
  115. {
  116. OD_API_EXIT();
  117. return;
  118. }
  119. pchStart = (char *)pchCurrent;
  120. nCharCount = 0;
  121. }
  122. else if(*pchCurrent == od_control.od_color_char)
  123. {
  124. bNotFound = FALSE;
  125. if(nCharCount != 0)
  126. {
  127. od_disp(pchStart, nCharCount, TRUE);
  128. }
  129. if(!*(++pchCurrent))
  130. {
  131. OD_API_EXIT();
  132. return;
  133. }
  134. od_set_attrib(*pchCurrent);
  135. if(!*(++pchCurrent))
  136. {
  137. OD_API_EXIT();
  138. return;
  139. }
  140. pchStart = (char *)pchCurrent;
  141. nCharCount = 0;
  142. }
  143. else
  144. {
  145. ++nCharCount;
  146. ++pchCurrent;
  147. }
  148. }
  149. chColorCheck = 0;
  150. if(bNotFound)
  151. {
  152. quick_print:
  153. /* Display the entire string in one shot. */
  154. od_disp_str(pszWorkBuffer);
  155. }
  156. else if(nCharCount != 0)
  157. {
  158. /* If there are remaining characters in the string, then display them. */
  159. od_disp(pchStart, nCharCount, TRUE);
  160. }
  161. OD_API_EXIT();
  162. }