Test base64 system more
[yaz-moved-to-github.git] / test / test_file_glob.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2012 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
14 #include <yaz/file_glob.h>
15 #include <yaz/test.h>
16 #include <yaz/log.h>
17 #include <yaz/wrbuf.h>
18
19 void tst_with_path(const char *tpath)
20 {
21     yaz_glob_res_t glob_res;
22     int ret = yaz_file_glob(tpath, &glob_res);
23     if (ret == 0)
24     {
25         size_t n = yaz_file_glob_get_num(glob_res);
26         size_t i;
27         for (i = 0; i < n; i++)
28         {
29             yaz_log(YLOG_LOG, "match %s", yaz_file_glob_get_file(glob_res, i));
30         }
31     }
32     yaz_file_globfree(&glob_res);
33 }
34
35 static int check_file(const char *got, const char *expect)
36 {
37     const char *f = got;
38     size_t l_match = strlen(expect);
39     YAZ_CHECK(f && strlen(f) >= l_match);
40     if (f && strlen(f) >= l_match && !strcmp(f + strlen(f) - l_match, expect))
41         return 1;
42     return 0;
43 }
44
45 void tst(void)
46 {
47     yaz_glob_res_t glob_res;
48     int ret;
49     WRBUF tpath = wrbuf_alloc();
50     const char *srcdir = getenv("srcdir");
51     
52     if (srcdir)
53     {
54         wrbuf_puts(tpath, srcdir);
55         wrbuf_puts(tpath, "/");
56     }
57     wrbuf_puts(tpath, "test_file*.c");
58     ret = yaz_file_glob(wrbuf_cstr(tpath), &glob_res);
59     YAZ_CHECK_EQ(ret, 0);
60
61     YAZ_CHECK_EQ(2, yaz_file_glob_get_num(glob_res));
62     if (yaz_file_glob_get_num(glob_res) == 2)
63     {
64         YAZ_CHECK(check_file(yaz_file_glob_get_file(glob_res, 0),
65                              "test_file_glob.c"));
66         YAZ_CHECK(check_file(yaz_file_glob_get_file(glob_res, 1),
67                              "test_filepath.c"));
68     }
69     wrbuf_destroy(tpath);
70     yaz_file_globfree(&glob_res);
71 }
72
73 int main (int argc, char **argv)
74 {
75     YAZ_CHECK_INIT(argc, argv);
76     if (argc >= 2)
77         tst_with_path(argv[1]);
78     else
79         tst();
80     YAZ_CHECK_TERM;
81 }
82 /*
83  * Local variables:
84  * c-basic-offset: 4
85  * c-file-style: "Stroustrup"
86  * indent-tabs-mode: nil
87  * End:
88  * vim: shiftwidth=4 tabstop=8 expandtab
89  */
90