Annotation of parser3/src/classes/file.C, revision 1.8
1.1 paf 1: /*
2: Parser
3: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
4: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
5:
1.8 ! paf 6: $Id: file.C,v 1.7 2001/03/26 10:36:52 paf Exp $
1.1 paf 7: */
8:
9: #include "pa_request.h"
10: #include "_file.h"
11: #include "pa_vfile.h"
12:
13: // global var
14:
1.3 paf 15: VStateless_class *file_class;
1.1 paf 16:
17: // methods
18:
1.7 paf 19: /// @test mkdirs
1.1 paf 20: static void _save(Request& r, const String& method_name, Array *params) {
1.4 paf 21: Pool& pool=r.pool();
1.8 ! paf 22: Value& vfile_name=*static_cast<Value *>(params->get(0));
1.1 paf 23: // forcing
1.4 paf 24: // ^save[this body type]
1.8 ! paf 25: r.fail_if_junction_(true, vfile_name,
1.4 paf 26: method_name, "file name must not be junction");
27:
28: // forcing untaint language
29: String lfile_name(pool);
1.8 ! paf 30: lfile_name.append(vfile_name.as_string(),
1.5 paf 31: String::UL_FILE_NAME, true);
1.7 paf 32:
33: // save
34: static_cast<VFile *>(r.self)->save(r.absolute(lfile_name));
35: }
36:
37: static void _delete(Request& r, const String& method_name, Array *params) {
38: Pool& pool=r.pool();
1.8 ! paf 39: Value& vfile_name=*static_cast<Value *>(params->get(0));
1.7 paf 40: // forcing
41: // ^delete[this body type]
1.8 ! paf 42: r.fail_if_junction_(true, vfile_name,
1.7 paf 43: method_name, "file name must not be junction");
44:
45: // forcing untaint language
46: String lfile_name(pool);
1.8 ! paf 47: lfile_name.append(vfile_name.as_string(),
1.7 paf 48: String::UL_FILE_NAME, true);
1.4 paf 49:
1.7 paf 50: // unlink
51: file_delete(pool, r.absolute(lfile_name));
1.1 paf 52: }
53:
1.8 ! paf 54: static void _find(Request& r, const String& method_name, Array *params) {
! 55: Pool& pool=r.pool();
! 56: Value& vfile_name=*static_cast<Value *>(params->get(0));
! 57: // forcing
! 58: // ^delete[this body type]
! 59: r.fail_if_junction_(true, vfile_name,
! 60: method_name, "file name must not be junction");
! 61:
! 62: // forcing untaint language
! 63: String lfile_name(pool);
! 64: lfile_name.append(vfile_name.as_string(),
! 65: String::UL_FILE_NAME, true);
! 66:
! 67: // passed file name simply exists in current dir
! 68: if(file_readable(r.absolute(lfile_name))) {
! 69: r.write_no_lang(*new(pool) VString(lfile_name));
! 70: return;
! 71: }
! 72:
! 73: // scan .. dirs for result
! 74: for(int i=0; i<10; i++) {
! 75: String test_name(pool);
! 76: for(int j=0; j<i; j++)
! 77: test_name.APPEND_CONST("../");
! 78: test_name.append(lfile_name, String::UL_NO);
! 79: if(file_readable(r.absolute(test_name))) {
! 80: r.write_no_lang(*new(pool) VString(*new(pool) String(test_name)));
! 81: return;
! 82: }
! 83: }
! 84:
! 85: // not found
! 86: if(params->size()==2) {
! 87: // forcing ..{this body type}
! 88: Value& not_found_code=*static_cast<Value *>(params->get(1));
! 89: r.fail_if_junction_(false, not_found_code,
! 90: method_name, "not-found param must be junction");
! 91: r.write_pass_lang(r.process(not_found_code));
! 92: }
! 93: }
! 94:
1.1 paf 95: // initialize
96:
1.3 paf 97: void initialize_file_class(Pool& pool, VStateless_class& vclass) {
1.1 paf 98: // ^save[file-name]
99: vclass.add_native_method("save", _save, 1, 1);
1.7 paf 100:
101: // ^delete[file-name]
102: vclass.add_native_method("delete", _delete, 1, 1);
1.8 ! paf 103:
! 104: // ^find[file-name]
! 105: // ^find[file-name]{when-not-found}
! 106: vclass.add_native_method("find", _find, 1, 2);
1.1 paf 107: }
E-mail: