beta-0.89.2
[luatex.git] / source / texk / kpathsea / fn.c
blob3464ced772228e1e9c9ce66a394ad9fd2e34e8d8
1 /* fn.c: arbitrarily long filenames or strings.
3 Copyright 1993, 2008 Karl Berry.
4 Copyright 2001, 2005 Olaf Weber.
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 License
17 along with this library; if not, see <http://www.gnu.org/licenses/>. */
19 #include <kpathsea/config.h>
21 #include <kpathsea/fn.h>
24 /* /usr/local/lib/texmf/fonts/public/cm/pk/ljfour/cmr10.300pk is 58
25 chars, so ASCII `K' seems a good choice. */
26 #define CHUNK_SIZE 75
29 fn_type
30 fn_init (void)
32 fn_type ret;
34 FN_ALLOCATED (ret) = FN_LENGTH (ret) = 0;
35 FN_STRING (ret) = NULL;
37 return ret;
41 fn_type
42 fn_copy0 (const_string s, unsigned len)
44 fn_type ret;
46 FN_ALLOCATED (ret) = CHUNK_SIZE > len ? CHUNK_SIZE : len + 1;
47 FN_STRING (ret) = (string)xmalloc (FN_ALLOCATED (ret));
49 strncpy (FN_STRING (ret), s, len);
50 FN_STRING (ret)[len] = 0;
51 FN_LENGTH (ret) = len + 1;
53 return ret;
56 /* Don't think we ever try to free something that might usefully be
57 empty, so give fatal error if nothing allocated. */
59 void
60 fn_free (fn_type *f)
62 assert (FN_STRING (*f) != NULL);
63 free (FN_STRING (*f));
64 FN_STRING (*f) = NULL;
65 FN_ALLOCATED (*f) = 0;
66 FN_LENGTH (*f) = 0;
69 /* An arithmetic increase seems more reasonable than geometric. We
70 don't increase the length member since it may be more convenient for
71 the caller to add than subtract when appending the stuff that will
72 presumably follow. */
74 static void
75 grow (fn_type *f, unsigned len)
77 while (FN_LENGTH (*f) + len > FN_ALLOCATED (*f))
79 FN_ALLOCATED (*f) += CHUNK_SIZE;
80 XRETALLOC (FN_STRING (*f), FN_ALLOCATED (*f), char);
85 void
86 fn_1grow (fn_type *f, char c)
88 grow (f, 1);
89 FN_STRING (*f)[FN_LENGTH (*f)] = c;
90 FN_LENGTH (*f)++;
94 void
95 fn_grow (fn_type *f, const_string source, unsigned len)
97 grow (f, len);
98 strncpy (FN_STRING (*f) + FN_LENGTH (*f), source, len);
99 FN_LENGTH (*f) += len;
103 void
104 fn_str_grow (fn_type *f, const_string s)
106 unsigned more_len = strlen (s);
107 grow (f, more_len);
108 strcat (FN_STRING (*f), s);
109 FN_LENGTH (*f) += more_len;
113 void
114 fn_shrink_to (fn_type *f, unsigned loc)
116 assert (FN_LENGTH (*f) > loc);
117 FN_STRING (*f)[loc] = 0;
118 FN_LENGTH (*f) = loc + 1;