1 /*
2  *  $Id: history.c,v 1.4 2002/05/12 22:51:17 hiroo Exp $
3  */
4 
5 /*
6  * FreeWnn is a network-extensible Kana-to-Kanji conversion system.
7  * This file is part of FreeWnn.
8  *
9  * Copyright Kyoto University Research Institute for Mathematical Sciences
10  *                 1987, 1988, 1989, 1990, 1991, 1992
11  * Copyright OMRON Corporation. 1987, 1988, 1989, 1990, 1991, 1992, 1999
12  * Copyright ASTEC, Inc. 1987, 1988, 1989, 1990, 1991, 1992
13  * Copyright FreeWnn Project 1999, 2000, 2002
14  *
15  * Maintainer:  FreeWnn Project   <freewnn@tomo.gr.jp>
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; either version 2 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
30  */
31 #ifdef HAVE_CONFIG_H
32 #  include <config.h>
33 #endif
34 
35 #include <stdio.h>
36 #if STDC_HEADERS
37 #  include <stdlib.h>
38 #else
39 #  if HAVE_MALLOC_H
40 #    include <malloc.h>
41 #  endif
42 #endif /* STDC_HEADERS */
43 #include "commonhd.h"
44 #include "sdefine.h"
45 #include "sheader.h"
46 #include "jslib.h"
47 
48 struct history
49 {
50   w_char *hbuffer;
51   struct history *previous;
52   struct history *next;
53   int num;
54 };
55 
56 static struct history *beginning_of_history;    /* �ҥ��ȥ����Ƭ */
57 static struct history *end_of_history;  /* �ҥ��ȥ�κǸ� */
58 static struct history *add_history;     /* �ɲð��� */
59 static struct history *current_history = NULL;  /* index */
60 static int wchar_holding = 0;
61 static int history_cunt;
62 
63 static void set_up_history ();
64 
65 int
init_history()66 init_history ()
67 {
68   int size1;
69   char *area_pter;
70 
71   register struct history *hist, *pre, *end;
72 
73   if ((history_cunt = max_history) < 1)
74     {
75       history_cunt = 0;
76       return (0);
77     }
78   size1 = history_cunt * sizeof (struct history);
79   if ((area_pter = malloc (size1)) == NULL)
80     {
81       history_cunt = 0;
82       wnn_errorno = WNN_MALLOC_ERR;
83       return (-1);
84     }
85   hist = beginning_of_history = end_of_history = add_history = (struct history *) area_pter;
86   pre = end = hist + (history_cunt - 1);
87   do
88     {
89       hist->hbuffer = NULL;
90       pre->next = hist;
91       hist->previous = pre;
92       hist->num = 0;
93       pre = hist++;
94     }
95   while (hist <= end);
96   return (0);
97 }
98 
99 
100 int
make_history(wstr,n)101 make_history (wstr, n)
102      register w_char *wstr;
103      register int n;
104 {
105   if (!history_cunt)
106     return (0);
107   if (n <= 0 || n > maxchg || *wstr == NULL)
108     return (-1);
109 
110   if (n == 1 && NORMAL_CHAR (*wstr))
111     {
112       if (!wchar_holding)
113         {
114           wchar_holding = 1;
115           set_up_history ();
116           end_of_history->num = 0;
117         }
118       if (end_of_history->hbuffer == NULL)
119         end_of_history->hbuffer = (w_char *) malloc (sizeof (w_char));
120       else
121         end_of_history->hbuffer = (w_char *) realloc (end_of_history->hbuffer, (end_of_history->num + 1) * sizeof (w_char));
122       Strncpy (end_of_history->hbuffer + end_of_history->num, wstr, 1);
123       if (++(end_of_history->num) >= maxchg)
124         wchar_holding = 0;
125       return (0);
126     }
127   if (wchar_holding)
128     wchar_holding = 0;
129   if (n == 1 && !(KANJI_CHAR (*wstr)))
130     /* ����ȥ��륳���ɤϡ��ҥ��ȥ�ˤ�����ʤ� */
131     return (0);
132   if (end_of_history->num == n && Strncmp (end_of_history->hbuffer, wstr, n) == 0)
133     {
134       /* Ʊ����Τϡ����� */
135       current_history = NULL;
136       return (0);
137     }
138   set_up_history ();
139   if (end_of_history->hbuffer != NULL)
140     free (end_of_history->hbuffer);
141   end_of_history->hbuffer = (w_char *) malloc (n * sizeof (w_char));
142   Strncpy (end_of_history->hbuffer, wstr, n);
143   end_of_history->num = n;
144 
145   return (0);
146 }
147 
148 
149 static void
set_up_history()150 set_up_history ()
151 {
152   if (add_history == beginning_of_history && end_of_history != beginning_of_history)
153     beginning_of_history = beginning_of_history->next;
154   end_of_history = add_history;
155   add_history = add_history->next;
156   current_history = NULL;
157 }
158 
159 
160 int
get_current_history(wbuf)161 get_current_history (wbuf)
162      register w_char *wbuf;
163 {
164   if (!history_cunt)
165     return (0);
166   if (wchar_holding)
167     wchar_holding = 0;
168   if (current_history == NULL)
169     current_history = end_of_history;
170   Strncpy (wbuf, current_history->hbuffer, current_history->num);
171   return (current_history->num);
172 }
173 
174 
175 /* ��������Ͽ�����������Ф� */
176 void
get_end_of_history(wbuf)177 get_end_of_history (wbuf)
178      register w_char *wbuf;
179 {
180   if (!history_cunt)
181     {
182       *wbuf = 0;
183     }
184   else
185     {
186       if (wchar_holding)
187         wchar_holding = 0;
188       Strncpy (wbuf, end_of_history->hbuffer, end_of_history->num);
189       *(wbuf + end_of_history->num) = 0;
190     }
191 }
192 
193 
194 int
previous_history1(buffer)195 previous_history1 (buffer)
196      register w_char *buffer;
197 {
198   if (!history_cunt || current_history == beginning_of_history)
199     return (0);                 /* do nothing */
200 
201   if (current_history != NULL)
202     current_history = current_history->previous;
203   return (get_current_history (buffer));
204 }
205 
206 
207 int
next_history1(buffer)208 next_history1 (buffer)
209      register w_char *buffer;
210 {
211   if (!history_cunt || current_history == end_of_history || current_history == NULL)
212     return (0);
213 
214   current_history = current_history->next;
215   return (get_current_history (buffer));
216 }
217