-
Notifications
You must be signed in to change notification settings - Fork 0
/
nomos_gap.c
111 lines (102 loc) · 2.87 KB
/
nomos_gap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
Copyright (C) 2013-2014, Siemens AG
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "nomos_gap.h"
#include <stdio.h> /* printf, scanf, NULL */
#include <stdlib.h> /* malloc, free, rand */
/**
* \file
* Compile using `gcc nomos_gap.c $(pkg-config glib-2.0 --cflags --libs) -std=c99`
*/
/**
* collapse text by elimination of invisible char, returns position+offset pairs
*/
GArray* collapseInvisible(char* text, char invisible)
{
int offset = 0;
char* readPointer = text;
char* writePointer = text;
int iAmVisible;
GArray* pairs = g_array_new(FALSE, FALSE, sizeof(pairPosOff));
for( iAmVisible = FALSE; *readPointer; readPointer++)
{
if(*readPointer == invisible){
offset++;
iAmVisible = FALSE;
continue;
}
// now: *readPointer != invisible
if (!iAmVisible){
pairPosOff pair;
pair.pos = writePointer-text;
pair.off = offset;
g_array_append_val(pairs, pair);
iAmVisible = TRUE;
}
*writePointer++ = *readPointer;
}
*writePointer = '\0';
return pairs;
}
/**
* Collapse spaces in text, returns position+offset pairs
* \todo delete me
*/
GArray* collapseSpaces(char* text)
{
int start = 0;
int cutOff; /* -1,0,1,... */
char* readPointer = text;
char* writePointer = text;
GArray* pairs = g_array_new(FALSE, FALSE, sizeof(pairPosOff));
for( cutOff=1; *readPointer; readPointer++)
{
if((*readPointer!=' ') && (cutOff>0)){
pairPosOff pair;
pair.pos = start;
pair.off = readPointer-writePointer;
g_array_append_val(pairs, pair);
}
if(*readPointer==' '){
cutOff++;
}
else{ // far away from cutting
cutOff = -1;
}
if (cutOff==0){
start = writePointer+1-text;
}
if (cutOff < 1){
*writePointer++ = *readPointer;
}
}
*writePointer = '\0';
return pairs;
}
inline pairPosOff* getPairPosOff(GArray* in, int index)
{
return &g_array_index(in, pairPosOff, index);
}
int uncollapsePosition(int collapsedPos, GArray* shifter)
{
int shifterIndex;
pairPosOff* thePoA;
for (shifterIndex = 1; shifterIndex < shifter->len; ++shifterIndex) {
thePoA = getPairPosOff(shifter, shifterIndex);
if (thePoA->pos > collapsedPos) {
break;
}
}
thePoA = getPairPosOff(shifter, shifterIndex - 1);
return collapsedPos + thePoA->off;
}