1
/* fn.c: arbitrarily long filenames (or just strings).
3
Copyright 2001, 2005 Olaf Weber.
4
Copyright 1993 Karl Berry.
6
This library is free software; you can redistribute it and/or
7
modify it under the terms of the GNU Lesser General Public
8
License as published by the Free Software Foundation; either
9
version 2.1 of the License, or (at your option) any later version.
11
This library is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
Lesser General Public License for more details.
16
You should have received a copy of the GNU Lesser General Public
17
License along with this library; if not, write to the Free Software
18
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
#include <kpathsea/config.h>
24
#include <kpathsea/fn.h>
27
/* /usr/local/lib/texmf/fonts/public/cm/pk/ljfour/cmr10.300pk is 58
28
chars, so ASCII `K' seems a good choice. */
37
FN_ALLOCATED (ret) = FN_LENGTH (ret) = 0;
38
FN_STRING (ret) = NULL;
45
fn_copy0 P2C(const_string, s, unsigned, len)
49
FN_ALLOCATED (ret) = CHUNK_SIZE > len ? CHUNK_SIZE : len + 1;
50
FN_STRING (ret) = (string)xmalloc (FN_ALLOCATED (ret));
52
strncpy (FN_STRING (ret), s, len);
53
FN_STRING (ret)[len] = 0;
54
FN_LENGTH (ret) = len + 1;
59
/* Don't think we ever try to free something that might usefully be
60
empty, so give fatal error if nothing allocated. */
63
fn_free P1C(fn_type *, f)
65
assert (FN_STRING (*f) != NULL);
66
free (FN_STRING (*f));
67
FN_STRING (*f) = NULL;
68
FN_ALLOCATED (*f) = 0;
72
/* An arithmetic increase seems more reasonable than geometric. We
73
don't increase the length member since it may be more convenient for
74
the caller to add than subtract when appending the stuff that will
78
grow P2C(fn_type *, f, unsigned, len)
80
while (FN_LENGTH (*f) + len > FN_ALLOCATED (*f))
82
FN_ALLOCATED (*f) += CHUNK_SIZE;
83
XRETALLOC (FN_STRING (*f), FN_ALLOCATED (*f), char);
89
fn_1grow P2C(fn_type *, f, char, c)
92
FN_STRING (*f)[FN_LENGTH (*f)] = c;
98
fn_grow P3C(fn_type *, f, const_string, source, unsigned, len)
101
strncpy (FN_STRING (*f) + FN_LENGTH (*f), source, len);
102
FN_LENGTH (*f) += len;
107
fn_str_grow P2C(fn_type *, f, const_string, s)
109
unsigned more_len = strlen (s);
111
strcat (FN_STRING (*f), s);
112
FN_LENGTH (*f) += more_len;
117
fn_shrink_to P2C(fn_type *, f, unsigned, loc)
119
assert (FN_LENGTH (*f) > loc);
120
FN_STRING (*f)[loc] = 0;
121
FN_LENGTH (*f) = loc + 1;