root/fetch.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. fetchword
  2. getmaterial
  3. materialalloc

   1 /*
   2  *   Compound Text word statistics and search report
   3  *
   4  *   fetch.c : multilingal word server
   5  *
   6  *                                           Copyright(c) isao yasuda, 1998 
   7  */
   8 static char *rcs_id = "$Id: fetch.c,v 1.3 2008/05/04 13:08:46 isao Exp $";
   9 
  10 #include <stdio.h>
  11 #include <stdlib.h>
  12 #include <string.h>
  13 #include "staslova.h"
  14 #include "table.h"
  15 
  16 unsigned char *
  17 fetchword(void)
  18 {
  19   unsigned char *cur = NULL;
  20   unsigned char *obj = NULL;
  21 
  22   if (cap != NULL) 
  23     if (*++cap != NULL) /* usually return one word from stock */
  24       return *cap; 
  25   erc = NOWORDS;
  26   while (erc == NOWORDS) {
  27     /* first action or stock sold out */
  28     if ((cur = getmaterial(CURRENT)) == NULL) /* EOF or IO error         */
  29       return NULL;
  30     if ((obj = materialalloc(NULL, cur, NULL)) == NULL)
  31       return NULL;
  32     G0 = ASCII; G1 = LATIN1;
  33     if ((wdpa = getwdarray(obj)) == NULL) {  /* memory error or no words */
  34       if (erc != NOWORDS)
  35         return NULL;
  36     } else
  37       erc = NORMAL;
  38     free((unsigned char *) obj);
  39   }
  40   cap = wdpa;
  41   return *cap;
  42 }
  43   
  44 unsigned char *
  45 getmaterial(int kind)
  46 {
  47   unsigned char *cur;
  48   
  49   if (lno == 0) { /* initialize */
  50     mtp[0] = no0;
  51     mtp[0] = fgets(mtp[0], MAXLINE+1, stdin);
  52     lno = 0;
  53     clp = 0;
  54   }
  55   switch (kind) {
  56   case CONTEXT : /* material text fragment service */ 
  57     return materialalloc(mtp[(clp + 1) % 3], mtp[(clp + 2) % 3], mtp[clp]); 
  58   case CURRENT : /* current line service */
  59   default :
  60     cur = mtp[clp];
  61     clp = ++lno % 3;
  62     if (cur != NULL) {
  63       switch (clp) {
  64       case 0 : mtp[clp] = no0; break;
  65       case 1 : mtp[clp] = no1; break;
  66       case 2 : mtp[clp] = no2; break;
  67       }
  68       mtp[clp] = fgets(mtp[clp], MAXLINE+1, stdin);
  69     }
  70     return cur;
  71   }
  72 }
  73 
  74 unsigned char *
  75 materialalloc(unsigned char *pre, unsigned char *cur, unsigned char *nxt)
  76 {
  77   int len = 0;
  78   unsigned char *p;
  79 
  80   if (pre != NULL) {
  81     len = (strlen(pre) + strlen(cur));
  82     if (nxt != NULL) {
  83       len += strlen(nxt);
  84       if ((p = (unsigned char *) malloc(len + 1)) == NULL) {
  85         fprintf(stderr, "Material allocation failed.(materialalloc)\n");
  86         return p;
  87       }
  88       strcpy(p, pre);
  89       strcat(p, cur);
  90       strcat(p, nxt);
  91     } else {
  92       if ((p = (unsigned char *) malloc(len + 1)) == NULL) {
  93         fprintf(stderr, "Material allocation failed.(materialalloc)\n");
  94         return p;
  95       }
  96       strcpy(p, pre);
  97       strcat(p, cur);
  98     }    
  99   } else {
 100     if (nxt != NULL) {
 101       len = (strlen(cur) + strlen(nxt));
 102       if ((p = (unsigned char *) malloc(len + 1)) == NULL) {
 103         fprintf(stderr, "Material allocation failed.(materialalloc)\n");
 104         return p;
 105       }
 106       strcpy(p, cur);
 107       strcat(p, nxt);
 108     } else {
 109       len = strlen(cur);
 110       if ((p = (unsigned char *) malloc(len + 1)) == NULL) {
 111         fprintf(stderr, "Material allocation failed.(materialalloc)\n");
 112         return p;
 113       }
 114       strcpy(p, cur);
 115     }
 116   }
 117   return p;
 118 }

/* [previous][next][first][last][top][bottom][index][help] */