Merge branch 'master' of ssh://git.indexdata.com/home/git/pub/yaz
[yaz-moved-to-github.git] / test / test_file_glob.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2010 Index Data
3  * See the file LICENSE for details.
4  */
5
6 #if HAVE_CONFIG_H
7 #include <config.h>
8 #endif
9
10 #include <errno.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <ctype.h>
14
15 #include <yaz/file_glob.h>
16 #include <yaz/test.h>
17 #include <yaz/log.h>
18 #include <yaz/wrbuf.h>
19
20 void tst_with_path(const char *tpath)
21 {
22     yaz_glob_res_t glob_res;
23     int ret = yaz_file_glob(tpath, &glob_res);
24     if (ret == 0)
25     {
26         size_t n = yaz_file_glob_get_num(glob_res);
27         size_t i;
28         for (i = 0; i < n; i++)
29         {
30             yaz_log(YLOG_LOG, "match %s", yaz_file_glob_get_file(glob_res, i));
31         }
32     }
33     yaz_file_globfree(&glob_res);
34 }
35
36 void tst(void)
37 {
38     yaz_glob_res_t glob_res;
39     int ret;
40     WRBUF tpath = wrbuf_alloc();
41     const char *srcdir = getenv("srcdir");
42     
43     if (srcdir)
44     {
45         wrbuf_puts(tpath, srcdir);
46         wrbuf_puts(tpath, "/");
47     }
48     wrbuf_puts(tpath, "Make*.am");
49     ret = yaz_file_glob(wrbuf_cstr(tpath), &glob_res);
50     YAZ_CHECK_EQ(ret, 0);
51
52     YAZ_CHECK_EQ(1, yaz_file_glob_get_num(glob_res));
53     if (yaz_file_glob_get_num(glob_res) == 1)
54     {
55         const char *f = yaz_file_glob_get_file(glob_res, 0);
56         size_t l_match = strlen("Makefile.am");
57         YAZ_CHECK(f && strlen(f) >= l_match);
58         if (f && strlen(f) >= l_match)
59         {
60             YAZ_CHECK(!strcmp(f + strlen(f) - l_match, "Makefile.am"));
61         }
62     }
63     wrbuf_destroy(tpath);
64     yaz_file_globfree(&glob_res);
65 }
66
67 int main (int argc, char **argv)
68 {
69     YAZ_CHECK_INIT(argc, argv);
70     if (argc >= 2)
71         tst_with_path(argv[1]);
72     else
73         tst();
74     YAZ_CHECK_TERM;
75 }
76 /*
77  * Local variables:
78  * c-basic-offset: 4
79  * c-file-style: "Stroustrup"
80  * indent-tabs-mode: nil
81  * End:
82  * vim: shiftwidth=4 tabstop=8 expandtab
83  */
84