File: lengths.c

package info (click to toggle)
latex2rtf 2.3.18a-3
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye
  • size: 9,384 kB
  • sloc: ansic: 20,424; makefile: 660; sh: 478; perl: 22
file content (175 lines) | stat: -rwxr-xr-x 4,552 bytes parent folder | download | duplicates (5)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175

/* lengths.c - commands that access TeX variables that contain TeX lengths

Copyright (C) 2001-2002 The Free Software Foundation

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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

This file is available from http://sourceforge.net/projects/latex2rtf/
 
Authors:
    2001-2002 Scott Prahl

By convention all the values stored should be twips  20 twips = 1 pt
*/

#include <stdlib.h>
#include <string.h>
#include "main.h"
#include "utils.h"
#include "lengths.h"
#include "parser.h"

#define MAX_LENGTHS 50

typedef struct {
    char *name;
    int distance;
} length_type;

length_type Lengths[MAX_LENGTHS];

static int iLengthCount = 0;

static int existsLength(char *s)

/**************************************************************************
     purpose: checks to see if a named TeX dimension exists
     returns: the array index of the named TeX dimension
**************************************************************************/
{
    int i = 0;

    while (i < iLengthCount && strstr(Lengths[i].name, s) == NULL)
        i++;

    if (i == iLengthCount)
        return -1;
    else
        return i;
}

static void newLength(char *s, int d)

/**************************************************************************
     purpose: allocates and initializes a named TeX dimension 
**************************************************************************/
{
    if (iLengthCount == MAX_LENGTHS) {
        diagnostics(WARNING, "Too many lengths, ignoring %s", s);
        return;
    }

    Lengths[iLengthCount].distance = d;
    Lengths[iLengthCount].name = strdup(s);

    if (Lengths[iLengthCount].name == NULL) {
        diagnostics(ERROR, "Cannot allocate name for length \\%s", s);
    }

    iLengthCount++;
}

void setLength(char *s, int d)

/**************************************************************************
     purpose: allocates (if necessary) and sets a named TeX dimension 
**************************************************************************/
{
    int i;

    i = existsLength(s);

    if (i < 0)
        newLength(s, d);
    else
        Lengths[i].distance = d;
}

int getLength(char *s)

/**************************************************************************
     purpose: retrieves a named TeX dimension 
**************************************************************************/
{
    int i;

    i = existsLength(s);

    if (i < 0) {
        diagnostics(WARNING, "No length of type %s", s);
        return 0;
    }

    return Lengths[i].distance;
}

void CmdSetTexLength(int code)
{
    int d;
    char c;

    c = getNonSpace();
    if (c == '=')               /* optional '=' */
        skipSpaces();
    else
        ungetTexChar(c);

    d = getDimension();
    diagnostics(4, "CmdSetTexLength size = %d", d);

    switch (code) {

        case SL_HOFFSET:
            setLength("hoffset", d);
            break;
        case SL_VOFFSET:
            setLength("voffset", d);
            break;
        case SL_PARINDENT:
            setLength("parindent", d);
            break;
        case SL_PARSKIP:
            setLength("parskip", d);
            break;
        case SL_BASELINESKIP:
            setLength("baselineskip", d);
            break;
        case SL_TOPMARGIN:
            setLength("topmargin", d);
            break;
        case SL_TEXTHEIGHT:
            setLength("textheight", d);
            break;
        case SL_HEADHEIGHT:
            setLength("headheight", d);
            break;
        case SL_HEADSEP:
            setLength("headsep", d);
            break;
        case SL_TEXTWIDTH:
            setLength("textwidth", d);
            break;
        case SL_ODDSIDEMARGIN:
            setLength("oddsidemargin", d);
            break;
        case SL_EVENSIDEMARGIN:
            setLength("evensidemargin", d);
            break;
        case SL_LINEWIDTH:
            setLength("linewidth", d);
            break;
    }
}