File: docFindBookmark.c

package info (click to toggle)
ted 2.16-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 13,944 kB
  • ctags: 20,273
  • sloc: ansic: 167,980; makefile: 12,518; sh: 263
file content (138 lines) | stat: -rw-r--r-- 3,190 bytes parent folder | download
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/************************************************************************/
/*									*/
/*  Find a bookmark in a document.					*/
/*									*/
/************************************************************************/

#   include	"tedConfig.h"

#   include	<stdlib.h>
#   include	<string.h>
#   include	<stdio.h>
#   include	<ctype.h>

#   include	<appDebugon.h>

#   include	"docBuf.h"
#   include	"docFind.h"

typedef struct FindBookmark
    {
    const char *	fbMarkName;
    int			fbMarkSize;
    int			fbObjectNumber;
    } FindBookmark;

/************************************************************************/
/*									*/
/*  Find the occurrence of a bookmark in a paragraph.			*/
/*									*/
/************************************************************************/

static int docBookmarkParaFindNext(
				DocumentSelection *		ds,
				BufferItem *			bi,
				const BufferDocument *		bd,
				const DocumentPosition *	dpFrom,
				void *				through )
    {
    FindBookmark *	fb= (FindBookmark *)through;

    int			partFrom;

    int			part;
    TextParticule *	tp;

    const int		lastOne= 1;

    partFrom= 0;
    if  ( dpFrom->dpStroff != 0						&&
	  docFindParticule( &partFrom, bi, dpFrom->dpStroff, lastOne )	)
	{ LDEB(dpFrom->dpStroff); return -1;	}

    if  ( partFrom < 0 || partFrom >= bi->biParaParticuleCount )
	{ LLDEB(partFrom,bi->biParaParticuleCount); return -1;	}

    tp= bi->biParaParticules+ partFrom;
    for ( part= partFrom; part < bi->biParaParticuleCount; tp++, part++ )
	{
	const DocumentField *	df;
	const char *		foundName;
	int			foundSize;

	if  ( tp->tpKind == DOCkindFIELDSTART )
	    {
	    df= bd->bdFieldList.dflFields+ tp->tpObjectNumber;

	    if  ( df->dfKind == DOCfkBOOKMARK				&&
		  ! docFieldGetBookmark( df, &foundName, &foundSize )	&&
		  foundSize == fb->fbMarkSize				&&
		  ! memcmp( foundName, fb->fbMarkName, fb->fbMarkSize )	)
		{
		if  ( fb->fbObjectNumber >= 0 )
		    { LDEB(fb->fbObjectNumber);	}

		fb->fbObjectNumber= tp->tpObjectNumber;

		ds->dsBegin.dpBi= bi;
		ds->dsBegin.dpStroff= tp->tpStroff;
		}
	    }

	if  ( tp->tpKind == DOCkindFIELDEND		&&
	      tp->tpObjectNumber == fb->fbObjectNumber	)
	    {
	    ds->dsEnd.dpBi= bi;
	    ds->dsEnd.dpStroff= tp->tpStroff;

	    return 0;
	    }
	}

    return 1;
    }

int docFindBookmarkInDocument(	DocumentSelection *	ds,
				BufferDocument *	bd,
				const char *		markName,
				int			markSize )
    {
    int				ret;

    DocumentPosition		dpFrom;

    DocumentSelection		dsNew;

    FindBookmark		fb;

    docInitDocumentPosition( &dpFrom );
    docInitDocumentSelection( &dsNew );

    fb.fbMarkName= markName;
    fb.fbMarkSize= markSize;
    fb.fbObjectNumber= -1;

    ret= docFindFindNextInDocument( &dsNew, &dpFrom, bd,
				    docBookmarkParaFindNext, (void *)&fb );

    if  ( ret < 0 )
	{ LDEB(ret); return ret;	}

    if  ( ret == 0 )
	{
	const int	direction= 1;
	const int	col0= -1;
	const int	col1= -1;

	if  ( dsNew.dsBegin.dpBi != dsNew.dsEnd.dpBi )
	    { XXDEB(dsNew.dsBegin.dpBi,dsNew.dsEnd.dpBi); return 1; }

	docSetRangeSelection( ds, &(dsNew.dsBegin), &(dsNew.dsEnd),
							direction, col0, col1 );

	return 0;
	}

    return 1;
    }