api9f.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * The contents of this file are subject to the Interbase Public
  3. * License Version 1.0 (the "License"); you may not use this file
  4. * except in compliance with the License. You may obtain a copy
  5. * of the License at http://www.Inprise.com/IPL.html
  6. *
  7. * Software distributed under the License is distributed on an
  8. * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  9. * or implied. See the License for the specific language governing
  10. * rights and limitations under the License.
  11. *
  12. * The Original Code was created by Inprise Corporation
  13. * and its predecessors. Portions created by Inprise Corporation are
  14. * Copyright (C) Inprise Corporation.
  15. *
  16. * All Rights Reserved.
  17. * Contributor(s): ______________________________________.
  18. */
  19. #include <stdio.h>
  20. #include <ibase.h>
  21. #include "example.h"
  22. #ifndef CHAR
  23. #define CHAR unsigned char
  24. #endif
  25. #ifndef SHORT
  26. #define SHORT unsigned short
  27. #endif
  28. static void set_statistics (char*, ISC_BLOB_CTL);
  29. static int read_text (SHORT, ISC_BLOB_CTL);
  30. static int dump_text (SHORT, ISC_BLOB_CTL);
  31. static int caller (SHORT, ISC_BLOB_CTL, SHORT, CHAR*, SHORT*);
  32. #define ACTION_open 0
  33. #define ACTION_get_segment 1
  34. #define ACTION_close 2
  35. #define ACTION_put_segment 3
  36. #define ACTION_create 4
  37. #define FB_SUCCESS 0
  38. #define FB_FAILURE 1
  39. #define BUFFER_LENGTH 512
  40. #ifdef SHLIB_DEFS
  41. #define system (*_libfun_system)
  42. #define fopen (*_libfun_fopen)
  43. #define unlink (*_libfun_unlink)
  44. #define fprintf (*_libfun_fprintf)
  45. #define fclose (*_libfun_fclose)
  46. #define fgetc (*_libfun_fgetc)
  47. extern int system();
  48. extern FILE *fopen();
  49. extern int unlink();
  50. extern int fprintf();
  51. extern int fclose();
  52. extern int fgetc();
  53. #endif
  54. static char prevbuf[BUFFER_LENGTH + 1];
  55. static int width = 40;
  56. int EXPORT desc_filter (SHORT action, ISC_BLOB_CTL control)
  57. {
  58. /**************************************
  59. *
  60. * d e s c _ f i l t e r
  61. *
  62. **************************************
  63. *
  64. * Functional description
  65. * Format a blob into 40-character-wide
  66. * paragraphs.
  67. * Read the blob into a file and process
  68. * it on open, then read it back line by
  69. * line in the get_segment loop.
  70. *
  71. **************************************/
  72. int status;
  73. FILE *text_file;
  74. char *out_buffer;
  75. switch (action)
  76. {
  77. case ACTION_open:
  78. prevbuf[0] = '\0';
  79. if (status = dump_text (action, control))
  80. return status;
  81. set_statistics("desc.txt", control); /* set up stats in ctl struct */
  82. break;
  83. case ACTION_get_segment:
  84. /* open the file first time through and save the file pointer */
  85. if (!control->ctl_data [0])
  86. {
  87. text_file = fopen ("desc.txt", "r");
  88. control->ctl_data[0] = (long) text_file;
  89. }
  90. if (status = read_text (action, control))
  91. return status;
  92. break;
  93. case ACTION_close:
  94. /* don't need the temp files any more, so clean them up */
  95. /*unlink("desc.txt");*/
  96. break;
  97. case ACTION_create:
  98. case ACTION_put_segment:
  99. return isc_uns_ext;
  100. }
  101. return FB_SUCCESS;
  102. }
  103. static int caller (SHORT action, ISC_BLOB_CTL control, SHORT buffer_length,
  104. CHAR *buffer, SHORT *return_length)
  105. {
  106. /**************************************
  107. *
  108. * c a l l e r
  109. *
  110. **************************************
  111. *
  112. * Functional description
  113. * Call next source filter. This
  114. * is a useful service routine for
  115. * all blob filters.
  116. *
  117. **************************************/
  118. int status;
  119. ISC_BLOB_CTL source;
  120. source = control->ctl_source_handle;
  121. source->ctl_status = control->ctl_status;
  122. source->ctl_buffer = buffer;
  123. source->ctl_buffer_length = buffer_length;
  124. status = (*source->ctl_source) (action, source);
  125. if (return_length)
  126. *return_length = source->ctl_segment_length;
  127. return status;
  128. }
  129. static int dump_text (SHORT action, ISC_BLOB_CTL control)
  130. {
  131. /**************************************
  132. *
  133. * d u m p _ t e x t
  134. *
  135. **************************************
  136. *
  137. * Functional description
  138. * Open a blob and write the
  139. * contents to a file
  140. *
  141. **************************************/
  142. FILE *text_file;
  143. CHAR buffer [BUFFER_LENGTH + 1];
  144. SHORT length;
  145. int status;
  146. int i, j;
  147. CHAR tbuf [BUFFER_LENGTH + 1];
  148. if (!(text_file = fopen ("desc.txt", "w")))
  149. return FB_FAILURE;
  150. while (!(status = caller (ACTION_get_segment, control, sizeof(buffer) - 1,
  151. buffer, &length)))
  152. {
  153. buffer[length] = 0;
  154. sprintf(tbuf, "%s%s", prevbuf, buffer);
  155. length = strlen(tbuf);
  156. /* replace any new-lines with space */
  157. for (i = 0; i < length; i++) {
  158. if (tbuf[i] == '\n')
  159. tbuf[i] = ' ';
  160. }
  161. i = 0;
  162. /* break the line up into width-length pieces */
  163. for (; i < length; i++)
  164. {
  165. /* save the remainder */
  166. if (strlen(&tbuf[i]) <= 40) {
  167. sprintf(prevbuf, "%s ", &tbuf[i]);
  168. break;
  169. }
  170. /* find end-of-word to break the line at */
  171. for (j = width - 1; j >= 0; j--) {
  172. if (isspace(tbuf[i + j]))
  173. break;
  174. }
  175. if (j < 0)
  176. j = width;
  177. fprintf (text_file, "%*.*s\n", j, j, &tbuf[i]);
  178. i = i + j;
  179. }
  180. }
  181. /* print the remainder */
  182. fprintf(text_file, "%s\n", prevbuf);
  183. fclose (text_file);
  184. if (status != isc_segstr_eof)
  185. return status;
  186. return FB_SUCCESS;
  187. }
  188. static void set_statistics (char* filename, ISC_BLOB_CTL control)
  189. {
  190. /*************************************
  191. *
  192. * s e t _ s t a t i s t i c s
  193. *
  194. *************************************
  195. *
  196. * Functional description
  197. * Sets up the statistical fields
  198. * in the passed in ctl structure.
  199. * These fields are:
  200. * ctl_max_segment - length of
  201. * longest seg in blob (in
  202. * bytes)
  203. * ctl_number_segments - # of
  204. * segments in blob
  205. * ctl_total_length - total length
  206. * of blob in bytes.
  207. * we should reset the
  208. * ctl structure, so that
  209. * blob_info calls get the right
  210. * values.
  211. *
  212. *************************************/
  213. int max_seg_length;
  214. int length;
  215. int num_segs;
  216. int cur_length;
  217. FILE *file;
  218. char *p;
  219. char c;
  220. num_segs = 0;
  221. length = 0;
  222. max_seg_length = 0;
  223. cur_length = 0;
  224. file = fopen ("desc.txt", "r");
  225. while (1)
  226. {
  227. c = fgetc (file);
  228. if (feof (file))
  229. break;
  230. length++;
  231. cur_length++;
  232. if (c == '\n') /* that means we are at end of seg */
  233. {
  234. if (cur_length > max_seg_length)
  235. max_seg_length = cur_length;
  236. num_segs++;
  237. cur_length = 0;
  238. }
  239. }
  240. control->ctl_max_segment = max_seg_length;
  241. control->ctl_number_segments = num_segs;
  242. control->ctl_total_length = length;
  243. }
  244. static int read_text (SHORT action, ISC_BLOB_CTL control)
  245. {
  246. /**************************************
  247. *
  248. * r e a d _ t e x t
  249. *
  250. **************************************
  251. *
  252. * Functional description
  253. * Reads a file one line at a time
  254. * and puts the data out as if it
  255. * were coming from a blob.
  256. *
  257. **************************************/
  258. CHAR *p;
  259. FILE *file;
  260. SHORT length;
  261. int c, status;
  262. p = control->ctl_buffer;
  263. length = control->ctl_buffer_length;
  264. file = (FILE *)control->ctl_data [0];
  265. for (;;)
  266. {
  267. c = fgetc (file);
  268. if (feof (file))
  269. break;
  270. *p++ = c;
  271. if ((c == '\n') || p >= control->ctl_buffer + length)
  272. {
  273. control->ctl_segment_length = p - control->ctl_buffer;
  274. if (c == '\n')
  275. return FB_SUCCESS;
  276. else
  277. return isc_segment;
  278. }
  279. }
  280. return isc_segstr_eof;
  281. }