1 /*
2  * sactstring.c: SACT��ʸ��������Ϣ
3  *
4  * Copyright (C) 1997-1998 Masaki Chikama (Wren) <chikama@kasumi.ipl.mech.nagoya-u.ac.jp>
5  *               1998-                           <masaki-c@is.aist-nara.ac.jp>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21 */
22 /* $Id: sactstring.c,v 1.1 2003/11/09 15:06:12 chikama Exp $ */
23 
24 #include "config.h"
25 #include <stdio.h>
26 #include <glib.h>
27 #include <string.h>
28 #include "portab.h"
29 #include "system.h"
30 #include "variable.h"
31 
32 // ʸ�����ִ���
33 typedef struct {
34 	char *src; // �֤�������ʸ����
35 	char *dst; // �֤�����ʸ����
36 } strexchange_t;
37 
38 #define DEFSTACKSIZE 100
39 static char **stack;   // stack����
40 static int idx;        // stack pointer
41 static int idxmax;     // stack pointer�κ���
42 
43 // ʸ�����֤������� (ɽ������on-the-fly���Ѵ�����ɽ��)
44 #define REPLACEBUFSIZE 3000
45 static char repbuf[2][REPLACEBUFSIZE];
46 static GSList *strreplace;
47 static char *replacesrc;
48 static char *replacedst;
49 
50 /**
51  * ʸ�����ѿ������å��ν����
52  */
sstr_init()53 int sstr_init() {
54 	stack = g_new(char *, DEFSTACKSIZE);
55 	idx = 0;
56 	idxmax = DEFSTACKSIZE;
57 	return OK;
58 }
59 
60 /**
61  * ʸ�����ѿ������å���ʸ������Ѥ�
62  * @param strno: ���ʥꥪ��Ǥ�ʸ�����ѿ��ֹ�
63  */
sstr_push(char * str)64 int sstr_push(char *str) {
65 	if (idx >= idxmax) {
66 		stack = g_renew(char *, stack, idx*2);
67 		idxmax = idx*2;
68 	}
69 
70 	stack[idx++] = g_strdup(str);
71 
72 	return OK;
73 }
74 
75 /**
76  * ʸ�����ѿ������å�����ʸ�������Ф�
77  * @param strno: �����å������ᤷ��ʸ������Ǽ����ʸ�����ѿ��ֹ�
78  */
sstr_pop(char * str,int maxlen)79 int sstr_pop(char *str, int maxlen) {
80 	if (idx == 0) return NG;
81 
82 	strncpy(str, stack[--idx], maxlen);
83 	g_free(stack[idx]);
84 
85 	return OK;
86 }
87 
88 /**
89  * ʸ������֤�����
90  * @param sstrno: �Ѵ���ʸ�����ѿ��ֹ�
91  * @param dstrno: �Ѵ���ʸ�����ѿ��ֹ�
92  */
sstr_regist_replace(char * sstr,char * dstr)93 int sstr_regist_replace(char *sstr, char *dstr) {
94 	strexchange_t *ex;
95 
96 	if (sstr == dstr) return NG;
97 
98 	ex = g_new(strexchange_t, 1);
99 	ex->src = strdup(sstr);
100 	ex->dst = strdup(dstr);
101 	strreplace = g_slist_append(strreplace, ex);
102 	return OK;
103 }
104 
105 /**
106  * ���� -> ʸ����
107  */
sstr_num2str(int strno,int fig,int nzeropad,int num)108 int sstr_num2str(int strno, int fig, int nzeropad, int num) {
109 	char s[256], ss[256];
110 
111 	if (nzeropad) {
112 		char *sss = "%%0%dd";
113 		sprintf(ss, sss, fig);
114 	} else {
115 		char *sss = "%%%dd";
116 		sprintf(ss, sss, fig);
117 	}
118 
119 	sprintf(s, ss, num);
120 	v_strcpy(strno -1, s);
121 
122 	return OK;
123 }
124 
125 // ʸ������֤���������
replacestr_cb(gpointer data,gpointer userdata)126 static void replacestr_cb(gpointer data, gpointer userdata) {
127 	strexchange_t *ex = (strexchange_t *)data;
128 	char *start, *next, *out;
129 
130 	if (ex == NULL) return;
131 
132 	start = replacesrc;
133 	out   = replacedst;
134 
135 	while (TRUE) {
136 		next = strstr(start, ex->src);
137 		if (next == NULL) break;
138 		strncat(out, start, (size_t)(next - start));
139 		strncat(out, ex->dst, max(0, (REPLACEBUFSIZE - (int)strlen(out))));
140 		start = next + strlen(ex->src);
141 	}
142 	strncat(out, start, max(0, REPLACEBUFSIZE - (int)strlen(out)));
143 
144 	replacedst = replacesrc;
145 	replacesrc = out;
146 	replacedst[0] = '\0';
147 }
148 
149 // ʸ������֤�����
sstr_replacestr(char * msg)150 char *sstr_replacestr(char *msg) {
151 	if (strreplace == NULL) return msg;
152 
153 	repbuf[0][0] = '\0';
154 	repbuf[1][0] = '\0';
155 	strncpy(repbuf[0], msg, REPLACEBUFSIZE);
156 	replacesrc = repbuf[0];
157 	replacedst = repbuf[1];
158 	g_slist_foreach(strreplace, replacestr_cb, NULL);
159 
160 	return (repbuf[0][0] == '\0') ? repbuf[1] : repbuf[0];
161 }
162