File: docParaString.c

package info (click to toggle)
ted 2.11-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 11,064 kB
  • ctags: 13,935
  • sloc: ansic: 120,446; makefile: 7,469; sh: 253
file content (87 lines) | stat: -rw-r--r-- 2,408 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
/************************************************************************/
/*									*/
/*  Manage the string of text bytes in a paragraph.			*/
/*  (Try not to be too clever, this will be changed to unicode once.)	*/
/*									*/
/************************************************************************/

#   include	"tedConfig.h"

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

#   include	<appDebugon.h>

#   include	"docBuf.h"

/************************************************************************/
/*									*/
/*  Make sure there is enough space in the string of a paragraph.	*/
/*									*/
/*  NOTE that bi->biParaStrlen is not updated. We just make sure that	*/
/*	there is enough memory.						*/
/*									*/
/************************************************************************/

int docInflateTextString(	BufferItem *    bi,
				int		by	)
    {
    unsigned char *	freshString;
    int			newSize;

    if  ( bi->biLevel != DOClevPARA )
	{ LLDEB(bi->biLevel,DOClevPARA); return -1;	}

    if  ( bi->biParaStrlen % 50 )
	{ newSize= bi->biParaStrlen+ by+  1; }
    else{ newSize= bi->biParaStrlen+ by+ 51; }

    freshString= (unsigned char *)realloc( bi->biParaString, newSize );
    if  ( ! freshString )
	{ LXDEB(bi->biParaStrlen,freshString); return -1; }

    bi->biParaString= freshString;

    return 0;
    }

/************************************************************************/
/*									*/
/*  Replace part of the string of a paragraph with a different string	*/
/*									*/
/************************************************************************/

int docParaStringReplace(		int *			pStroffShift,
					BufferItem *		bi,
					int			stroffBegin,
					int			stroffEnd,
					const unsigned char *	addedString,
					int			addedStrlen )
    {
    int		stroffShift;

    stroffShift= addedStrlen- stroffEnd+ stroffBegin;

    if  ( stroffShift > 0				&&
	  docInflateTextString( bi, stroffShift )	)
	{ LLDEB(bi->biParaStrlen,stroffShift); return -1;	}

    if  ( bi->biParaStrlen > 0 )
	{
	memmove( bi->biParaString+ stroffEnd+ stroffShift,
		bi->biParaString+ stroffEnd, bi->biParaStrlen- stroffEnd+ 1 );
	}

    memcpy( bi->biParaString+ stroffBegin, addedString, addedStrlen );

    if  ( stroffShift != 0 )
	{
	bi->biParaStrlen += stroffShift;
	bi->biParaString[bi->biParaStrlen]= '\0';
	}

    *pStroffShift= stroffShift;
    return 0;
    }