File: splitmsg.cc

package info (click to toggle)
estic 1.61-5
  • links: PTS
  • area: main
  • in suites: potato
  • size: 3,968 kB
  • ctags: 6,407
  • sloc: cpp: 41,916; asm: 1,620; makefile: 436; ansic: 402; sh: 40
file content (148 lines) | stat: -rw-r--r-- 3,920 bytes parent folder | download | duplicates (6)
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
139
140
141
142
143
144
145
146
147
148
/*****************************************************************************/
/*                                                                           */
/*                                SPLITMSG.CC                                */
/*                                                                           */
/* (C) 1993-95  Ullrich von Bassewitz                                        */
/*              Zwehrenbuehlstrasse 33                                       */
/*              D-72070 Tuebingen                                            */
/* EMail:       uz@ibb.schwaben.com                                          */
/*                                                                           */
/*****************************************************************************/



// $Id$
//
// $Log$
//
//



#include "splitmsg.h"



/*****************************************************************************/
/*                                                                           */
/*****************************************************************************/



ListNode<String>* SplitLine (String Text, Rect &Bounds, unsigned MinLen)
// Split the given string into a list of lines. MinLen is used if any of the
// lines is requested to be centered. In this case, MinLen is the minimum
// length of the centered line (used in windows with header strings).
{
    String* S;

    // Check the parameters
    unsigned Len = Text.Len ();
    PRECONDITION (Len > 0);

    // Initialize bounds
    Bounds.A.X = Bounds.A.Y = 0;

    // Split the text into lots of lines remembering the longest line
    int Longest = MinLen;
    ListNode<String>* Node = NULL;
    while (Len) {

        // Get the end of the line
        int Pos = Text.Pos ('\n');

        //
        int L;
        if (Pos < 0) {
            // Last line
            S = new String (Text);
            L = Text.Len ("@~");
            if (L > Longest) {
                Longest = L;
            }
            Len = 0;
        } else {
            // Cut the piece from the string, deleting the newline
            S = new String (Text.Cut (0, Pos));
            Text.Del (0, Pos + 1);
            Len -= Pos + 1;

            // Get the length and compare to Longest
            L = S->Len ("@~\x01");      // Do not count caret/bar/center
            if (L > Longest) {
                Longest = L;
            }
        }

        // Create a new listnode
        ListNode<String>* N = new ListNode<String> (S);

        // Insert this node into the list
        if (Node) {
            N->InsertBefore (Node);
        } else {
            Node = N;
        }

    }

    // Now the length of the longest line is in Longest. Walk through
    // the line list, delete the center char and center lines if requested.
    // Count the nodes
    ListNode<String>* N = Node;
    int Count = 0;
    do {
        // Get pointer to line
        String& S = *(N->Contents ());
        Count++;

        // Check for center flag if string is not empty
        if (S.IsEmpty () == 0 && S [0] == '\x01') {
            // Delete the center flag
            S.Del (0, 1);

            // Center the string
            S.Pad (String::Center, Longest);
        }

        // Set next node
        N = N->Next ();

    } while (N != Node);

    // Set up the surrounding rectangle
    Bounds.B.X = (i16) Longest;
    Bounds.B.Y = (i16) Count;

    // Return the list
    return Node;
}



void ReleaseLines (ListNode<String> *Node)
// Release a line list build from SplitLine
{
    ListNode<String> *N = Node;

    while (!Node->IsEmpty ()) {

        // Get the next node and unlink it
        N = Node->Next ();
        N->Unlink ();

        // Delete the contents of the node and the node
        delete N->Contents ();
        delete N;

    }

    // Now delete the root node
    delete Node->Contents ();
    delete Node;
}