Mercurial > kinput2.yaz
comparison lib/imlib/imbuf.c @ 0:92745d501b9a
initial import from kinput2-v3.1
author | Yoshiki Yazawa <yaz@honeyplanet.jp> |
---|---|
date | Mon, 08 Mar 2010 04:44:30 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:92745d501b9a |
---|---|
1 #ifndef lint | |
2 static char *rcsid = "$Id: imbuf.c,v 1.7 1994/05/30 10:26:28 ishisone Exp $"; | |
3 #endif | |
4 /* | |
5 * Copyright (c) 1994 Software Research Associates, Inc. | |
6 * | |
7 * Permission to use, copy, modify, and distribute this software and its | |
8 * documentation for any purpose and without fee is hereby granted, provided | |
9 * that the above copyright notice appear in all copies and that both that | |
10 * copyright notice and this permission notice appear in supporting | |
11 * documentation, and that the name of Software Research Associates not be | |
12 * used in advertising or publicity pertaining to distribution of the | |
13 * software without specific, written prior permission. Software Research | |
14 * Associates makes no representations about the suitability of this software | |
15 * for any purpose. It is provided "as is" without express or implied | |
16 * warranty. | |
17 * | |
18 * Author: Makoto Ishisone, Software Research Associates, Inc., Japan | |
19 */ | |
20 | |
21 #include <X11/Intrinsic.h> | |
22 #include <X11/Xfuncs.h> | |
23 #include "imbuf.h" | |
24 | |
25 static void | |
26 allocIMBuf(ibp, len) | |
27 IMBuffer *ibp; | |
28 int len; | |
29 { | |
30 int newsize; | |
31 | |
32 if (ibp->size >= len) return; | |
33 newsize = ibp->size * 2; | |
34 if (newsize < len) newsize = len; | |
35 if (ibp->buf == ibp->internal) { | |
36 ibp->buf = XtMalloc(newsize); | |
37 bcopy(ibp->internal, ibp->buf, ibp->size); | |
38 } else { | |
39 ibp->buf = XtRealloc(ibp->buf, newsize); | |
40 } | |
41 ibp->size = newsize; | |
42 } | |
43 | |
44 void | |
45 IMBufInit(ibp) | |
46 IMBuffer *ibp; | |
47 { | |
48 ibp->buf = ibp->internal; | |
49 IMBufClear(ibp); | |
50 } | |
51 | |
52 void | |
53 IMBufClear(ibp) | |
54 IMBuffer *ibp; | |
55 { | |
56 if (ibp->buf != NULL && ibp->buf != ibp->internal) XtFree(ibp->buf); | |
57 ibp->buf = ibp->internal; | |
58 ibp->size = sizeof(ibp->internal); | |
59 ibp->start = ibp->end = 0; | |
60 } | |
61 | |
62 void | |
63 IMBufAdd(ibp, data, len) | |
64 IMBuffer *ibp; | |
65 char *data; | |
66 int len; | |
67 { | |
68 allocIMBuf(ibp, ibp->end + len); | |
69 (void)bcopy(data, ibp->buf + ibp->end, len); | |
70 ibp->end += len; | |
71 } | |
72 | |
73 void | |
74 IMBufOverwrite(ibp, offset, data, len) | |
75 IMBuffer *ibp; | |
76 int offset; | |
77 char *data; | |
78 int len; | |
79 { | |
80 int dend; | |
81 | |
82 dend = ibp->start + offset + len; | |
83 allocIMBuf(ibp, dend); | |
84 (void)bcopy(data, ibp->buf + ibp->start + offset, len); | |
85 if (ibp->end < dend) ibp->end = dend; | |
86 } | |
87 | |
88 char * | |
89 IMBufAlloc(ibp, len) | |
90 IMBuffer *ibp; | |
91 int len; | |
92 { | |
93 char *p; | |
94 | |
95 allocIMBuf(ibp, ibp->end + len); | |
96 p = ibp->buf + ibp->end; | |
97 ibp->end += len; | |
98 return p; | |
99 } | |
100 | |
101 void | |
102 IMBufDiscard(ibp, len) | |
103 IMBuffer *ibp; | |
104 int len; | |
105 { | |
106 if (len > 0) { | |
107 /* discard top of the data */ | |
108 ibp->start += len; | |
109 } else { | |
110 /* discard end of the data */ | |
111 ibp->end += len; | |
112 } | |
113 if (ibp->start >= ibp->end) IMBufClear(ibp); | |
114 } | |
115 | |
116 void | |
117 IMBufDiscardNUL(ibp) | |
118 IMBuffer *ibp; | |
119 { | |
120 while (ibp->start < ibp->end) { | |
121 if ((ibp->buf)[ibp->start] != 0) break; | |
122 ibp->start++; | |
123 } | |
124 if (ibp->start >= ibp->end) IMBufClear(ibp); | |
125 } | |
126 | |
127 void | |
128 IMBufCompact(ibp) | |
129 IMBuffer *ibp; | |
130 { | |
131 | |
132 if (ibp->buf != ibp->internal) { | |
133 int length = IMBUFLEN(ibp); | |
134 | |
135 if (length <= sizeof(ibp->internal)) { | |
136 bcopy(ibp->buf + ibp->start, ibp->internal, length); | |
137 XtFree(ibp->buf); | |
138 ibp->buf = ibp->internal; | |
139 ibp->size = sizeof(ibp->internal); | |
140 } else { | |
141 bcopy(ibp->buf + ibp->start, ibp->buf, length); | |
142 } | |
143 ibp->start = 0; | |
144 ibp->end = length; | |
145 } else if (ibp->start != 0) { | |
146 int length = IMBUFLEN(ibp); | |
147 | |
148 bcopy(ibp->buf + ibp->start, ibp->buf, length); | |
149 ibp->start = 0; | |
150 ibp->end = length; | |
151 } | |
152 } |