1*946379e7Schristos /* GNU gettext - internationalization aids
2*946379e7Schristos Copyright (C) 1996, 1998, 2000-2002 Free Software Foundation, Inc.
3*946379e7Schristos
4*946379e7Schristos This file was written by Peter Miller <millerp@canb.auug.org.au>
5*946379e7Schristos
6*946379e7Schristos This program is free software; you can redistribute it and/or modify
7*946379e7Schristos it under the terms of the GNU General Public License as published by
8*946379e7Schristos the Free Software Foundation; either version 2, or (at your option)
9*946379e7Schristos any later version.
10*946379e7Schristos
11*946379e7Schristos This program is distributed in the hope that it will be useful,
12*946379e7Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
13*946379e7Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*946379e7Schristos GNU General Public License for more details.
15*946379e7Schristos
16*946379e7Schristos You should have received a copy of the GNU General Public License
17*946379e7Schristos along with this program; if not, write to the Free Software Foundation,
18*946379e7Schristos Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19*946379e7Schristos
20*946379e7Schristos
21*946379e7Schristos #ifdef HAVE_CONFIG_H
22*946379e7Schristos #include "config.h"
23*946379e7Schristos #endif
24*946379e7Schristos
25*946379e7Schristos /* Specification. */
26*946379e7Schristos #include "dir-list.h"
27*946379e7Schristos
28*946379e7Schristos #include <stddef.h>
29*946379e7Schristos #include <stdlib.h>
30*946379e7Schristos
31*946379e7Schristos #include "str-list.h"
32*946379e7Schristos
33*946379e7Schristos static string_list_ty *directory /* = NULL */;
34*946379e7Schristos
35*946379e7Schristos
36*946379e7Schristos /* Append a directory to the end of the list of directories. */
37*946379e7Schristos void
dir_list_append(const char * s)38*946379e7Schristos dir_list_append (const char *s)
39*946379e7Schristos {
40*946379e7Schristos if (directory == NULL)
41*946379e7Schristos directory = string_list_alloc ();
42*946379e7Schristos string_list_append_unique (directory, s);
43*946379e7Schristos }
44*946379e7Schristos
45*946379e7Schristos
46*946379e7Schristos /* Return the nth directory, or NULL of n is out of range. */
47*946379e7Schristos const char *
dir_list_nth(int n)48*946379e7Schristos dir_list_nth (int n)
49*946379e7Schristos {
50*946379e7Schristos /* The default value of the list consists of the single directory ".". */
51*946379e7Schristos if (directory == NULL)
52*946379e7Schristos dir_list_append (".");
53*946379e7Schristos
54*946379e7Schristos if (n < 0 || n >= directory->nitems)
55*946379e7Schristos return NULL;
56*946379e7Schristos return directory->item[n];
57*946379e7Schristos }
58*946379e7Schristos
59*946379e7Schristos
60*946379e7Schristos /* Return the current list of directories, for later use with dir_list_restore.
61*946379e7Schristos Reset the list to empty. */
62*946379e7Schristos void *
dir_list_save_reset()63*946379e7Schristos dir_list_save_reset ()
64*946379e7Schristos {
65*946379e7Schristos void *saved_value = directory;
66*946379e7Schristos
67*946379e7Schristos directory = NULL;
68*946379e7Schristos return saved_value;
69*946379e7Schristos }
70*946379e7Schristos
71*946379e7Schristos
72*946379e7Schristos /* Restore a previously saved list of directories. */
73*946379e7Schristos void
dir_list_restore(void * saved_value)74*946379e7Schristos dir_list_restore (void *saved_value)
75*946379e7Schristos {
76*946379e7Schristos /* Don't free the contained strings, because they may have been returned
77*946379e7Schristos by dir_list_nth and may still be in use. */
78*946379e7Schristos if (directory != NULL)
79*946379e7Schristos {
80*946379e7Schristos if (directory->item != NULL)
81*946379e7Schristos free (directory->item);
82*946379e7Schristos free (directory);
83*946379e7Schristos }
84*946379e7Schristos
85*946379e7Schristos directory = saved_value;
86*946379e7Schristos }
87