• R/O
  • SSH
  • HTTPS

bchan: 提交


Commit MetaInfo

修訂562 (tree)
時間2013-05-08 01:34:24
作者ornse01

Log Message

implement textcluster_t.
- collection structure only.

Change Summary

差異

--- bchank/trunk/src/textcluster.c (nonexistent)
+++ bchank/trunk/src/textcluster.c (revision 562)
@@ -0,0 +1,163 @@
1+/*
2+ * textcluster.c
3+ *
4+ * Copyright (c) 2013 project bchan
5+ *
6+ * This software is provided 'as-is', without any express or implied
7+ * warranty. In no event will the authors be held liable for any damages
8+ * arising from the use of this software.
9+ *
10+ * Permission is granted to anyone to use this software for any purpose,
11+ * including commercial applications, and to alter it and redistribute it
12+ * freely, subject to the following restrictions:
13+ *
14+ * 1. The origin of this software must not be misrepresented; you must not
15+ * claim that you wrote the original software. If you use this software
16+ * in a product, an acknowledgment in the product documentation would be
17+ * appreciated but is not required.
18+ *
19+ * 2. Altered source versions must be plainly marked as such, and must not be
20+ * misrepresented as being the original software.
21+ *
22+ * 3. This notice may not be removed or altered from any source
23+ * distribution.
24+ *
25+ */
26+
27+#include "textcluster.h"
28+
29+#include <basic.h>
30+#include <bstdlib.h>
31+#include <bstdio.h>
32+#include <bsys/queue.h>
33+
34+LOCAL VOID textcluster_line_insert(textcluster_line_t *line, textcluster_line_t *next)
35+{
36+ QueInsert(&line->que, &next->que);
37+}
38+
39+LOCAL textcluster_line_t* textcluster_line_getnext(textcluster_line_t *line)
40+{
41+ return (textcluster_line_t*)line->que.next;
42+}
43+
44+LOCAL textcluster_line_t* textcluster_line_new()
45+{
46+ textcluster_line_t *line;
47+
48+ line = (textcluster_line_t*)malloc(sizeof(textcluster_line_t));
49+ if (line == NULL) {
50+ return line;
51+ }
52+ QueInit(&line->que);
53+ line->str = NULL;
54+ line->pos = NULL;
55+ line->len = 0;
56+
57+ return line;
58+}
59+
60+LOCAL VOID textcluster_line_delete(textcluster_line_t *line)
61+{
62+ QueRemove(&line->que);
63+ if (line->str != NULL) {
64+ free(line->str);
65+ }
66+ if (line->pos != NULL) {
67+ free(line->pos);
68+ }
69+ free(line);
70+}
71+
72+EXPORT textcluster_line_t* textcluster_getsentinel(textcluster_t *cluster)
73+{
74+ return (textcluster_line_t*)&cluster->sentinel;
75+}
76+
77+EXPORT textcluster_line_t* textcluster_get(textcluster_t *cluster, W at)
78+{
79+ textcluster_line_t *sentinel, *node;
80+ W i;
81+
82+ if (cluster->len == 0) {
83+ return NULL;
84+ }
85+ if (cluster->len >= at) {
86+ return NULL;
87+ }
88+
89+ sentinel = textcluster_getsentinel(cluster);
90+ node = textcluster_line_getnext(sentinel);
91+ for (i = 0; i < at; i++) {
92+ node = textcluster_line_getnext(node);
93+ }
94+
95+ return node;
96+}
97+
98+EXPORT textcluster_line_t* textcluster_insert(textcluster_t *cluster, W at)
99+{
100+ textcluster_line_t *line, *line_new;
101+
102+ if (at != cluster->len) {
103+ line = textcluster_get(cluster, at);
104+ if (line == NULL) {
105+ return NULL;
106+ }
107+ } else {
108+ line = textcluster_getsentinel(cluster);
109+ }
110+
111+ line_new = textcluster_line_new();
112+ if (line_new == NULL) {
113+ return NULL;
114+ }
115+
116+ textcluster_line_insert(line_new, line);
117+ cluster->line++;
118+
119+ return line_new;
120+}
121+
122+EXPORT W textcluster_remove(textcluster_t *cluster, W at)
123+{
124+ textcluster_line_t *line;
125+
126+ line = textcluster_get(cluster, at);
127+ if (line == NULL) {
128+ return -1; // TODO
129+ }
130+
131+ textcluster_line_delete(line);
132+ cluster->line--;
133+
134+ return 0;
135+}
136+
137+EXPORT textcluster_t* textcluster_new()
138+{
139+ textcluster_t *cluster;
140+
141+ cluster = (textcluster_t*)malloc(sizeof(textcluster_t));
142+ if (cluster == NULL) {
143+ return NULL;
144+ }
145+ QueInit(&cluster->sentinel);
146+ cluster->len = 0;
147+
148+ return cluster;
149+}
150+
151+EXPORT VOID textcluster_delete(textcluster_t *cluster)
152+{
153+ textcluster_line_t *sentinel, *node;
154+ sentinel = textcluster_getsentinel(cluster);
155+ for (;;) {
156+ node = textcluster_line_getnext(sentinel);
157+ if (node == sentinel) {
158+ break;
159+ }
160+ textcluster_line_delete(sentinel);
161+ }
162+ free(cluster);
163+}
--- bchank/trunk/src/textcluster.h (nonexistent)
+++ bchank/trunk/src/textcluster.h (revision 562)
@@ -0,0 +1,53 @@
1+/*
2+ * textcluster.c
3+ *
4+ * Copyright (c) 2013 project bchan
5+ *
6+ * This software is provided 'as-is', without any express or implied
7+ * warranty. In no event will the authors be held liable for any damages
8+ * arising from the use of this software.
9+ *
10+ * Permission is granted to anyone to use this software for any purpose,
11+ * including commercial applications, and to alter it and redistribute it
12+ * freely, subject to the following restrictions:
13+ *
14+ * 1. The origin of this software must not be misrepresented; you must not
15+ * claim that you wrote the original software. If you use this software
16+ * in a product, an acknowledgment in the product documentation would be
17+ * appreciated but is not required.
18+ *
19+ * 2. Altered source versions must be plainly marked as such, and must not be
20+ * misrepresented as being the original software.
21+ *
22+ * 3. This notice may not be removed or altered from any source
23+ * distribution.
24+ *
25+ */
26+
27+#include <basic.h>
28+#include <bsys/queue.h>
29+
30+#ifndef __TEXTCLUSTER_H__
31+#define __TEXTCLUSTER_H__
32+
33+struct textcluster_line_t_ {
34+ QUEUE que;
35+ TC *str;
36+ W *pos;
37+ W len;
38+};
39+typedef struct textcluster_line_t_ textcluster_line_t;
40+
41+struct textcluster_t_ {
42+ QUEUE sentinel;
43+ W len;
44+};
45+typedef struct textcluster_t_ textcluster_t;
46+
47+IMPORT textcluster_t* textcluster_new();
48+IMPORT VOID textcluster_delete(textcluster_t *cluster);
49+IMPORT textcluster_line_t* textcluster_get(textcluster_t *cluster, W at);
50+IMPORT textcluster_line_t* textcluster_insert(textcluster_t *cluster, W at);
51+IMPORT W textcluster_remove(textcluster_t *cluster, W at);
52+
53+#endif
Show on old repository browser