summaryrefslogtreecommitdiffstats
path: root/private/utils/mep/help/enginlib/hline.c
blob: a9376f36108b23217fadda79f75061839b924c1f (plain) (blame)
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
/**************************************************************************
 *HelpGetLine - Return a line of ascii text
 *
 *       Copyright <C> 1988, Microsoft Corporation
 *
 * Revision History:
 *
 *       25-Jan-1990     ln      LOCATE -> HLP_LOCATE
 *       22-Feb-1989     ln      Check correctly for end of topic while copying
 *                               line.
 *       22-Dec-1988     LN      Removed MASM High Level Lang support (Need
 *                               to control segments better than that will
 *                               let me)
 *       08-Dec-1988     LN      CSEG
 *       11-Nov-1988     ln      Adjust cbMax on entry
 *       03-Nov-1988     ln      Added GQ sensitivity
 *  []   22-Sep-1988     LN      Created
 *
 * Notes:
 *
 * Sensitive to the following switches:
 *
 *       HOFFSET - If defined, handle/offset version
 *       OS2     - If defined, OS/2 protect mode version
 *       DSLOAD  - If defined, causes DS to be reloaded, if needed
 *       ASCII   - If TRUE, includes ASCII support code
 *       GQ      - If defined, INC BP before saving & DEC before restore
 *
 **************************************************************************/

#include <stdio.h>
#if defined (OS2)
#define INCL_BASE
#include <os2.h>
#else
#include <windows.h>
#endif

#include <help.h>
#include <helpfile.h>
#include <helpsys.h>



/**** HelpGetLine - Return a line of ascii text
 *
 *  Interpret the help files stored format and return a line at a time of
 *  ascii text.
 *
 * ushort far pascal LOADDS HelpGetLine (
 * ushort  ln,           = 1 based line number to return
 * ushort  cbMax,        = Max number of bytes to transfer
 * uchar far *pszDst,    = pointer to destination
 * PB      pbTopic       = PB pointer to topic text
 *
 * Output:
 *  Returns number of characters transfered, or 0 if that line does not exist.
 *
 *************************************************************************/
USHORT pascal
HelpGetLine (
    USHORT ln,
    USHORT cbMax,
    PUCHAR pszDst,
    PB     pbTopic
    ) {

    struct topichdr *pT;
    USHORT          cbTransfered = 0;
    PUCHAR          pszDstOrg    = pszDst;

    cbMax--; //adjust to account for terminating zero

    pT = PBLOCK(pbTopic);

    if (pT) {

        PCHAR pLine = hlp_locate(ln, (PCHAR)pT);

        if (pLine) {

            *pszDst      = ' ';
            *(pszDst +1) = '\00';   // initialize dest.


            if (pT->ftype & FTCOMPRESSED) {

                //  For compressed files, get the length of the line from the
                //  first byte, and of course skip that byte. Form the
                //  maximum byte count ot be transfered as the lesser of the
                //  actual length of the line or caller cbMax.

                USHORT Len = (USHORT)*pLine++ - 1;

                if (Len) {
                    ULONG   LongLen;
                    Len = (Len > cbMax) ? cbMax : Len;

                    LongLen = Len/sizeof(ULONG);
                    Len     = (USHORT)(Len % sizeof(ULONG));


                    while (LongLen--) {
                        *((ULONG *UNALIGNED)pszDst)++ = *((ULONG UNALIGNED *)pLine)++;
                    }
                    while (Len--) {
                        *pszDst++ = *pLine++;
                    }
                    *pszDst++ = '\00';       // Null terminate it
                    cbTransfered = (USHORT)(pszDst - pszDstOrg);
                } else {
                    cbTransfered = 2;
                }

            } else {

                //  For non-compressed files, copy one line

                PCHAR pSrc = pLine;
                CHAR  c    = *pLine;

                if (c == '\n') {
                    cbTransfered = 2;
                } else {
                    while (c != '\00' && c != '\n') {
                        c = *pszDst++ = *pLine++;
                    }
                    *(pszDst-1) = '\00';    // null terminate it

                    cbTransfered = (USHORT)(pszDst - pszDstOrg);
                }
            }
        }

        PBUNLOCK(pbTopic);
    }

    return cbTransfered;
}