File: wrap.cc

package info (click to toggle)
aegis 4.22-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 27,952 kB
  • ctags: 11,163
  • sloc: cpp: 170,390; sh: 66,404; makefile: 25,776; yacc: 4,497; perl: 1,106; ansic: 492; awk: 366
file content (135 lines) | stat: -rw-r--r-- 2,794 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
//
//	aegis - project change supervisor
//	Copyright (C) 2004, 2005 Peter Miller;
//	All rights reserved.
//
//	This program is free software; you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation; either version 2 of the License, or
//	(at your option) any later version.
//
//	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., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
//
// MANIFEST: implementation of the printer_wrap class
//

#include <common/ac/string.h>

#include <aemakefile/printer/wrap.h>


printer_wrap::~printer_wrap()
{
    if (column != 0)
	putch('\n');
    delete deeper;
    deeper = 0;
    buflen = 0;
    bufmax = 0;
    delete [] buffer;
    buffer = 0;
}


printer_wrap::printer_wrap(printer *arg) :
    deeper(arg),
    start_of_line(true),
    continuation_line(false),
    column(0),
    buflen(0),
    bufwidth(0),
    bufmax(0),
    buffer(0)
{
}


void
printer_wrap::stash_inner(char c)
{
    if (buflen >= bufmax)
    {
	size_t new_bufmax = bufmax * 2 + 32;
	char *new_buffer = new char[new_bufmax];
	memcpy(new_buffer, buffer, buflen);
	delete [] buffer;
	buffer = new_buffer;
	bufmax = new_bufmax;
    }
    buffer[buflen++] = c;
    if (c == '\t')
	bufwidth += 8;
    else
	bufwidth++;
}


void
printer_wrap::emit_buffer()
{
    if (!buflen)
	return;
    //
    // We have to decide if the word in the buffer makes the line wrap
    // or not.  The current output position in the deeper output in
    // represented by "column", and the length of the current work is
    // represented by "buflen".  If we need to wrap, we add " \\\n\t\t",
    // so we have to wrap at column 78 or earlier.
    //
    if (column != 0)
    {
	if (column + buflen > maximum_line_width)
	{
	    deeper->puts(" \\\n\t\t");
	    column = 16;
	    continuation_line = true;
	}
	else
	{
	    deeper->putch(' ');
	    ++column;
	}
    }
    column += bufwidth;
    deeper->write(buffer, buflen);
    start_of_line = false;
    buflen = 0;
    bufwidth = 0;
}


void
printer_wrap::putch(char c)
{
    switch (c)
    {
    case '\n':
	emit_buffer();

	deeper->putch('\n');
	column = 0;
	start_of_line = true;
	continuation_line = false;
	break;

    case ' ':
    case '\t':
	if (start_of_line && !continuation_line)
	    stash_inner(c);
	else
	    emit_buffer();
	break;

    default:
	stash_inner(c);
	start_of_line = false;
	break;
    }
}