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. */
34 FN_ALLOCATED (ret
) = FN_LENGTH (ret
) = 0;
35 FN_STRING (ret
) = NULL
;
42 fn_copy0 (const_string s
, unsigned len
)
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;
56 /* Don't think we ever try to free something that might usefully be
57 empty, so give fatal error if nothing allocated. */
62 assert (FN_STRING (*f
) != NULL
);
63 free (FN_STRING (*f
));
64 FN_STRING (*f
) = NULL
;
65 FN_ALLOCATED (*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
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);
86 fn_1grow (fn_type
*f
, char c
)
89 FN_STRING (*f
)[FN_LENGTH (*f
)] = c
;
95 fn_grow (fn_type
*f
, const_string source
, unsigned len
)
98 strncpy (FN_STRING (*f
) + FN_LENGTH (*f
), source
, len
);
99 FN_LENGTH (*f
) += len
;
104 fn_str_grow (fn_type
*f
, const_string s
)
106 unsigned more_len
= strlen (s
);
108 strcat (FN_STRING (*f
), s
);
109 FN_LENGTH (*f
) += more_len
;
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;