]> mj.ucw.cz Git - libucw.git/blob - lib/fb-param.c
6fc5f27ac2043da3f95c193554ac03f87c0b9698
[libucw.git] / lib / fb-param.c
1 /*
2  *      UCW Library -- FastIO on files with run-time parametrization
3  *
4  *      (c) 2007 Pavel Charvat <pchar@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include "lib/lib.h"
11 #include "lib/conf.h"
12 #include "lib/fastbuf.h"
13
14 struct fb_params fbpar_defaults = {
15   .buffer_size = 65536,
16 }; 
17
18 struct cf_section fbpar_cf = {
19 # define F(x) PTR_TO(struct fb_params, x)
20   CF_TYPE(struct fb_params),
21   CF_ITEMS {
22     // FIXME
23     CF_LOOKUP("Type", (int *)F(type), ((byte *[]){"std", "direct", "mmap", NULL})),
24     CF_UNS("BufSize", F(buffer_size)),
25     CF_END
26   }
27 # undef F
28 };
29
30 static struct cf_section fbpar_global_cf = {
31   CF_ITEMS {
32     CF_SECTION("Defaults", &fbpar_defaults, &fbpar_cf),
33     CF_END
34   }
35 };
36
37 static void CONSTRUCTOR
38 fbpar_global_init(void)
39 {
40   cf_declare_section("FBParam", &fbpar_global_cf, 0);
41 }
42
43 struct fastbuf *
44 bopen_file(byte *name, int mode, struct fb_params *params)
45 {
46   params = params ? : &fbpar_defaults;
47   switch (params->type)
48     {
49       case FB_STD:
50         return bopen(name, mode, params->buffer_size);
51       case FB_DIRECT:
52         return fbdir_open(name, mode, NULL);
53       default:
54         ASSERT(0);
55     }
56 }
57
58 struct fastbuf *
59 bopen_file_try(byte *name, int mode, struct fb_params *params)
60 {
61   params = params ? : &fbpar_defaults;
62   switch (params->type)
63     {
64       case FB_STD:
65         return bopen_try(name, mode, params->buffer_size);
66       case FB_DIRECT:
67         return fbdir_open_try(name, mode, NULL);
68       default:
69         ASSERT(0);
70     }
71 }
72
73 struct fastbuf *
74 bopen_fd(int fd, struct fb_params *params)
75 {
76   params = params ? : &fbpar_defaults;
77   switch (params->type)
78     {
79       case FB_STD:
80         return bfdopen(fd, params->buffer_size);
81       case FB_DIRECT:
82         return fbdir_open_fd(fd, NULL);
83       default:
84         ASSERT(0);
85     }
86 }
87
88 struct fastbuf *
89 bopen_tmp_file(struct fb_params *params)
90 {
91   params = params ? : &fbpar_defaults;
92   switch (params->type)
93     {
94       case FB_STD:
95         return bopen_tmp(params->buffer_size);
96       case FB_DIRECT:
97         return fbdir_open_tmp(NULL);
98       default:
99         ASSERT(0);
100     }
101 }