Annotation of parser3/src/classes/file.C, revision 1.19
1.17 paf 1: /** @file
2: Parser: @b file parser class.
3:
1.1 paf 4: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.17 paf 5:
1.1 paf 6: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
7:
1.19 ! paf 8: $Id: file.C,v 1.18 2001/04/03 17:01:01 paf Exp $
1.1 paf 9: */
10:
11: #include "pa_request.h"
12: #include "_file.h"
13: #include "pa_vfile.h"
1.11 paf 14: #include "pa_table.h"
1.1 paf 15:
1.9 paf 16: // consts
17:
18: const int FIND_MONKEY_MAX_HOPS=10;
19:
1.1 paf 20: // global var
21:
1.3 paf 22: VStateless_class *file_class;
1.1 paf 23:
24: // methods
25:
26: static void _save(Request& r, const String& method_name, Array *params) {
1.4 paf 27: Pool& pool=r.pool();
1.8 paf 28: Value& vfile_name=*static_cast<Value *>(params->get(0));
1.1 paf 29: // forcing
1.4 paf 30: // ^save[this body type]
1.8 paf 31: r.fail_if_junction_(true, vfile_name,
1.19 ! paf 32: method_name, "file name must not be code");
1.4 paf 33:
1.7 paf 34: // save
1.18 paf 35: static_cast<VFile *>(r.self)->save(r.absolute(vfile_name.as_string()));
1.7 paf 36: }
37:
38: static void _delete(Request& r, const String& method_name, Array *params) {
39: Pool& pool=r.pool();
1.8 paf 40: Value& vfile_name=*static_cast<Value *>(params->get(0));
1.7 paf 41: // forcing
42: // ^delete[this body type]
1.8 paf 43: r.fail_if_junction_(true, vfile_name,
1.19 ! paf 44: method_name, "file name must not be code");
1.7 paf 45:
46: // unlink
1.18 paf 47: file_delete(pool, r.absolute(vfile_name.as_string()));
1.1 paf 48: }
49:
1.8 paf 50: static void _find(Request& r, const String& method_name, Array *params) {
51: Pool& pool=r.pool();
52: Value& vfile_name=*static_cast<Value *>(params->get(0));
53: // forcing
54: // ^delete[this body type]
55: r.fail_if_junction_(true, vfile_name,
1.19 ! paf 56: method_name, "file name must not be code");
1.8 paf 57:
1.18 paf 58: const String &lfile_name=vfile_name.as_string();
1.8 paf 59:
60: // passed file name simply exists in current dir
61: if(file_readable(r.absolute(lfile_name))) {
62: r.write_no_lang(*new(pool) VString(lfile_name));
63: return;
64: }
65:
66: // scan .. dirs for result
1.9 paf 67: for(int i=0; i<FIND_MONKEY_MAX_HOPS; i++) {
1.8 paf 68: String test_name(pool);
69: for(int j=0; j<i; j++)
70: test_name.APPEND_CONST("../");
1.15 paf 71: test_name.append(lfile_name, String::UL_CLEAN);
1.8 paf 72: if(file_readable(r.absolute(test_name))) {
73: r.write_no_lang(*new(pool) VString(*new(pool) String(test_name)));
74: return;
75: }
76: }
77:
78: // not found
79: if(params->size()==2) {
80: // forcing ..{this body type}
81: Value& not_found_code=*static_cast<Value *>(params->get(1));
82: r.fail_if_junction_(false, not_found_code,
1.19 ! paf 83: method_name, "not-found param must be code");
1.8 paf 84: r.write_pass_lang(r.process(not_found_code));
85: }
86: }
87:
1.9 paf 88: static void _load(Request& r, const String& method_name, Array *params) {
89: Pool& pool=r.pool();
90: Value& vfile_name=*static_cast<Value *>(params->get(0));
91:
1.10 paf 92: // forcing ^load[this body type]
1.9 paf 93: r.fail_if_junction_(true, vfile_name,
1.19 ! paf 94: method_name, "file name must not be code");
1.9 paf 95:
1.18 paf 96: const String& lfile_name=vfile_name.as_string();
1.9 paf 97:
1.12 paf 98: void *data; size_t size;
1.9 paf 99: file_read(pool, r.absolute(lfile_name), data, size, false/*binary*/);
100:
1.18 paf 101: char *user_file_name=params->size()==1?lfile_name.cstr(String::UL_FILE_NAME)
1.9 paf 102: :static_cast<Value *>(params->get(1))->as_string().cstr();
1.10 paf 103:
1.11 paf 104: const String *mime_type=0;
105: if(params->size()==3)
106: mime_type=&static_cast<Value *>(params->get(2))->as_string();
107: else {
108: if(r.mime_types) {
109: if(char *cext=strrchr(user_file_name, '.')) {
110: cext++;
111: String sext(pool, cext);
112: if(r.mime_types->locate(0, sext))
113: if(!(mime_type=r.mime_types->item(1)))
114: PTHROW(0, 0,
1.12 paf 115: r.mime_types->origin_string(),
116: "MIME-TYPE table column elements must not be empty");
1.11 paf 117: }
118: }
119: }
1.12 paf 120:
1.11 paf 121: if(!mime_type)
122: mime_type=new(pool) String(pool, "application/octet-stream");
123:
124: static_cast<VFile *>(r.self)->set(data, size, user_file_name, mime_type);
1.9 paf 125: }
126:
1.1 paf 127: // initialize
128:
1.3 paf 129: void initialize_file_class(Pool& pool, VStateless_class& vclass) {
1.1 paf 130: // ^save[file-name]
1.14 paf 131: vclass.add_native_method("save", Method::CT_DYNAMIC, _save, 1, 1);
1.7 paf 132:
133: // ^delete[file-name]
1.14 paf 134: vclass.add_native_method("delete", Method::CT_STATIC, _delete, 1, 1);
1.8 paf 135:
136: // ^find[file-name]
137: // ^find[file-name]{when-not-found}
1.14 paf 138: vclass.add_native_method("find", Method::CT_STATIC, _find, 1, 2);
1.9 paf 139:
140: // ^load[disk-name]
141: // ^load[disk-name;user-name]
1.11 paf 142: // ^load[disk-name;user-name;mime-type]
1.14 paf 143: vclass.add_native_method("load", Method::CT_DYNAMIC, _load, 1, 3);
1.1 paf 144: }
E-mail: