Back to index

tetex-bin  3.0
path-elt.c
Go to the documentation of this file.
00001 /* path-elt.c: Return the stuff between colons.
00002 
00003 Copyright (C) 1993, 96 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/c-pathch.h>
00022 #include <kpathsea/pathsearch.h>
00023 
00024 
00025 /* The static (but dynamically allocated) area we return the answer in,
00026    and how much we've currently allocated for it.  */
00027 static string elt = NULL;
00028 static unsigned elt_alloc = 0;
00029 
00030 /* The path we're currently working on.  */
00031 static const_string path = NULL;
00032 
00033 
00034 /* Upon entry, the static `path' is at the first (and perhaps last)
00035    character of the return value, or else NULL if we're at the end (or
00036    haven't been called).  I make no provision for caching the results;
00037    thus, we parse the same path over and over, on every lookup.  If that
00038    turns out to be a significant lose, it can be fixed, but I'm guessing
00039    disk accesses overwhelm everything else.  If ENV_P is true, use
00040    IS_ENV_SEP; else use IS_DIR_SEP.  */
00041 
00042 static string
00043 element P2C(const_string, passed_path,  boolean, env_p)
00044 {
00045   const_string p;
00046   string ret;
00047   int brace_level;
00048   unsigned len;
00049   
00050   if (passed_path)
00051     path = passed_path;
00052   /* Check if called with NULL, and no previous path (perhaps we reached
00053      the end).  */
00054   else if (!path)
00055     return NULL;
00056   
00057   /* OK, we have a non-null `path' if we get here.  */
00058   assert (path);
00059   p = path;
00060   
00061   /* Find the next colon not enclosed by braces (or the end of the path).  */
00062   brace_level = 0;
00063   while (*p != 0  && !(brace_level == 0
00064                        && (env_p ? IS_ENV_SEP (*p) : IS_DIR_SEP (*p)))) {
00065     if (*p == '{') ++brace_level;
00066     else if (*p == '}') --brace_level;
00067     ++p;
00068   }
00069    
00070   /* Return the substring starting at `path'.  */
00071   len = p - path;
00072 
00073   /* Make sure we have enough space (including the null byte).  */
00074   if (len + 1 > elt_alloc)
00075     {
00076       elt_alloc = len + 1;
00077       elt = (string)xrealloc (elt, elt_alloc);
00078     }
00079 
00080   strncpy (elt, path, len);
00081   elt[len] = 0;
00082   ret = elt;
00083 
00084   /* If we are at the end, return NULL next time.  */
00085   if (path[len] == 0)
00086     path = NULL;
00087   else
00088     path += len + 1;
00089 
00090   return ret;
00091 }
00092 
00093 string
00094 kpse_path_element P1C(const_string, p)
00095 {
00096   return element (p, true);
00097 }
00098 
00099 string
00100 kpse_filename_component P1C(const_string, p)
00101 {
00102   return element (p, false);
00103 }
00104 
00105 #ifdef TEST
00106 
00107 void
00108 print_path_elements (const_string path)
00109 {
00110   string elt;
00111   printf ("Elements of `%s':", path ? path : "(null)");
00112   
00113   for (elt = kpse_path_element (path); elt != NULL;
00114        elt = kpse_path_element (NULL))
00115     {
00116       printf (" %s", *elt ? elt : "`'");
00117     }
00118   
00119   puts (".");
00120 }
00121 
00122 int
00123 main ()
00124 {
00125   /* All lists end with NULL.  */
00126   print_path_elements (NULL);      /* */
00127   print_path_elements (""); /* "" */
00128   print_path_elements ("a");       /* a */
00129   print_path_elements (ENV_SEP_STRING);   /* "", "" */
00130   print_path_elements (ENV_SEP_STRING ENV_SEP_STRING);  /* "", "", "" */
00131   print_path_elements ("a" ENV_SEP_STRING);      /* a, "" */ 
00132   print_path_elements (ENV_SEP_STRING "b");      /* "", b */ 
00133   print_path_elements ("a" ENV_SEP_STRING "b");  /* a, b */ 
00134   
00135   return 0;
00136 }
00137 
00138 #endif /* TEST */
00139 
00140 
00141 /*
00142 Local variables:
00143 standalone-compile-command: "gcc -g -I. -I.. -DTEST path-elt.c kpathsea.a"
00144 End:
00145 */