Back to index

tetex-bin  3.0
fn.c
Go to the documentation of this file.
00001 /* fn.c: arbitrarily long filenames (or just strings).
00002 
00003 Copyright (C) 1993 Karl Berry.
00004 
00005 This library is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU Library General Public
00007 License as published by the Free Software Foundation; either
00008 version 2 of the License, or (at your option) any later version.
00009 
00010 This library is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 Library General Public License for more details.
00014 
00015 You should have received a copy of the GNU Library General Public
00016 License along with this library; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
00018 
00019 #include <kpathsea/config.h>
00020 
00021 #include <kpathsea/fn.h>
00022 
00023 
00024 /* /usr/local/lib/texmf/fonts/public/cm/pk/ljfour/cmr10.300pk is 58
00025    chars, so ASCII `K' seems a good choice. */
00026 #define CHUNK_SIZE 75
00027 
00028 
00029 fn_type
00030 fn_init P1H(void)
00031 {
00032   fn_type ret;
00033   
00034   FN_ALLOCATED (ret) = FN_LENGTH (ret) = 0;
00035   FN_STRING (ret) = NULL;
00036   
00037   return ret;
00038 }
00039 
00040 
00041 fn_type
00042 fn_copy0 P2C(const_string, s,  unsigned, len)
00043 {
00044   fn_type ret;
00045   
00046   FN_ALLOCATED (ret) = CHUNK_SIZE > len ? CHUNK_SIZE : len + 1;
00047   FN_STRING (ret) = (string)xmalloc (FN_ALLOCATED (ret));
00048   
00049   strncpy (FN_STRING (ret), s, len);
00050   FN_STRING (ret)[len] = 0;
00051   FN_LENGTH (ret) = len + 1;
00052   
00053   return ret;
00054 }
00055 
00056 /* Don't think we ever try to free something that might usefully be
00057    empty, so give fatal error if nothing allocated.  */
00058 
00059 void
00060 fn_free P1C(fn_type *, f)
00061 {
00062   assert (FN_STRING (*f) != NULL);
00063   free (FN_STRING (*f));
00064   FN_STRING (*f) = NULL;
00065   FN_ALLOCATED (*f) = 0;
00066   FN_LENGTH (*f) = 0;
00067 }
00068 
00069 /* An arithmetic increase seems more reasonable than geometric.  We
00070    don't increase the length member since it may be more convenient for
00071    the caller to add than subtract when appending the stuff that will
00072    presumably follow.  */
00073 
00074 static void
00075 grow P2C(fn_type *, f,  unsigned, len)
00076 {
00077   while (FN_LENGTH (*f) + len > FN_ALLOCATED (*f))
00078     {
00079       FN_ALLOCATED (*f) += CHUNK_SIZE;
00080       XRETALLOC (FN_STRING (*f), FN_ALLOCATED (*f), char);
00081     }
00082 }
00083 
00084 
00085 void
00086 fn_1grow P2C(fn_type *, f,  char, c)
00087 {
00088   grow (f, 1);
00089   FN_STRING (*f)[FN_LENGTH (*f)] = c;
00090   FN_LENGTH (*f)++;
00091 }
00092 
00093 
00094 void
00095 fn_grow P3C(fn_type *, f,  const_string, source,  unsigned, len)
00096 {
00097   grow (f, len);
00098   strncpy (FN_STRING (*f) + FN_LENGTH (*f), source, len);
00099   FN_LENGTH (*f) += len;
00100 }
00101 
00102 
00103 void
00104 fn_str_grow P2C(fn_type *, f,  const_string, s)
00105 {
00106   unsigned more_len = strlen (s);
00107   grow (f, more_len);
00108   strcat (FN_STRING (*f), s);
00109   FN_LENGTH (*f) += more_len;
00110 }
00111 
00112 
00113 void
00114 fn_shrink_to P2C(fn_type *, f,  unsigned, loc)
00115 {
00116   assert (FN_LENGTH (*f) > loc);
00117   FN_STRING (*f)[loc] = 0;
00118   FN_LENGTH (*f) = loc + 1;
00119 }