Introduction
Introduction Statistics Contact Development Disclaimer Help
string.c - sam - An updated version of the sam text editor.
git clone git://vernunftzentrum.de/sam.git
Log
Files
Refs
LICENSE
---
string.c (2526B)
---
1 /* Copyright (c) 1998 Lucent Technologies - All rights reserved. */
2 #include "sam.h"
3
4 #define MINSIZE 16 /* minimum number of chars allocated */
5 #define MAXSIZE 256 /* maximum number of chars for an empty string */
6
7
8 void
9 Strinit(String *p)
10 {
11 p->s = emalloc(MINSIZE*RUNESIZE);
12 p->n = 0;
13 p->size = MINSIZE;
14 }
15
16 void
17 Strinit0(String *p)
18 {
19 p->s = emalloc(MINSIZE*RUNESIZE);
20 p->s[0] = 0;
21 p->n = 1;
22 p->size = MINSIZE;
23 }
24
25 void
26 Strclose(String *p)
27 {
28 free(p->s);
29 }
30
31 void
32 Strzero(String *p)
33 {
34 if(p->size > MAXSIZE){
35 p->s = erealloc(p->s, RUNESIZE*MAXSIZE); /* throw away the garba…
36 p->size = MAXSIZE;
37 }
38 p->n = 0;
39 }
40
41 void
42 Strdupl(String *p, wchar_t *s) /* copies the null */
43 {
44 p->n = wcslen(s);
45 Strinsure(p, p->n + 1);
46 wmemmove(p->s, s, p->n);
47 }
48
49 void
50 Strduplstr(String *p, String *q) /* will copy the null if there's one…
51 {
52 Strinsure(p, q->n);
53 p->n = q->n;
54 wmemmove(p->s, q->s, q->n);
55 }
56
57 void
58 Straddc(String *p, wchar_t c)
59 {
60 Strinsure(p, p->n + 1);
61 p->s[p->n++] = c;
62 }
63
64 void
65 Strinsure(String *p, uint64_t n)
66 {
67 if(n > STRSIZE)
68 error(Etoolong);
69
70 if(p->size < n){ /* p needs to grow */
71 n += 100;
72 p->s = erealloc(p->s, n * RUNESIZE);
73 p->size = n;
74 }
75 }
76
77 void
78 Strinsert(String *p, String *q, Posn p0)
79 {
80 Strinsure(p, p->n+q->n);
81 wmemmove(p->s + p0 + q->n, p->s + p0, p->n - p0);
82 wmemmove(p->s + p0, q->s, q->n);
83 p->n += q->n;
84 }
85
86 void
87 Strdelete(String *p, Posn p1, Posn p2)
88 {
89 wmemmove(p->s + p1, p->s + p2, p->n - p2);
90 p->n -= p2-p1;
91 }
92
93 int
94 Strcmp(String *a, String *b)
95 {
96 return wcscmp(a->s, b->s);
97 }
98
99 char*
100 Strtoc(String *s)
101 {
102 size_t l = s->n * MB_LEN_MAX;
103 char *c = emalloc(l + 1);
104 wchar_t ws[s->n + 1];
105
106 memset(ws, 0, sizeof(ws));
107 memset(c, 0, l + 1);
108 wmemcpy(ws, s->s, s->n);
109 ws[s->n] = 0;
110
111 if (wcstombs(c, ws, l) == (size_t)-1)
112 panic("encoding 1");
113
114 return c;
115 }
116
117 /*
118 * Build very temporary String from wchar_t*
119 */
120 String*
121 tmprstr(wchar_t *r, int n)
122 {
123 static String p = {0};
124
125 p.s = r;
126 p.n = n;
127 p.size = n;
128 return &p;
129 }
130
131 /*
132 * Convert null-terminated char* into String
133 */
134 String*
135 tmpcstr(char *s)
136 {
137 String *p = emalloc(sizeof(String));
138 p->n = utflen(s);
139 p->size = p->n + 1;
140 p->s = calloc(p->size, sizeof(wchar_t));
141 if (mbstowcs(p->s, s, p->n) == (size_t)-1)
142 panic("encoding 2");
143
144 return p;
145 }
146
147 void
148 freetmpstr(String *s)
149 {
150 free(s->s);
151 free(s);
152 }
You are viewing proxied material from vernunftzentrum.de. The copyright of proxied material belongs to its original authors. Any comments or complaints in relation to proxied material should be directed to the original authors of the content concerned. Please see the disclaimer for more details.