PageMac
Home
   dot Projects
   dot Pics and Images
   dot WREK
D++
   dot About / FAQ
   dot Download
   dot Code Library
   dot Documentation
   dot VB6 Runtime Files
About

  Source Code - dpp/source/dppdb.dpl

Download link (right click, save as)



1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
'D++ Database Library
'Provides simple text-based database functionality
'By Phunk (http://phunk.pagemac.com)
'Visit the above URL for documentation and examples

function db_isvalid(db_isvalid_file) 
{ 
    newvar db_isvalid_fp; 
    newvar validate; 

    if fileexist(db_isvalid_file) = False then; 
        return "E_NO_SUCH_DB"; 
    endif; 
    db_isvalid_fp = f_open(db_isvalid_file,"r"); 
    validate = f_linein(db_isvalid_fp); 
    f_close(db_isvalid_fp); 
    if validate = "#!DPPDB/VALID_DATABASE" then; 
        return True; 
    else; 
        return False; 
    endif; 
} 

function db_create(db_create_file) 
{ 
    newvar db_create_fp; 

    if fileexist(db_create_file) = True then; 
        return "E_DB_EXISTS"; 
    endif; 
    db_create_fp = f_open(db_create_file,"w"); 
    f_out(db_create_fp,"#!DPPDB/VALID_DATABASE"); 
    f_close(db_create_fp); 
    if fileexist(db_create_file) = True then; 
        return "S_DB_CREATED"; 
    else; 
        return "E_DB_NOT_CREATED"; 
    endif; 
} 

function db_iskey(db_iskey_file, db_iskey_key) 
{ 
    newvar db_iskey_fp; 
    newvar iskey_entry_parts[]; 

    if fileexist(db_iskey_file) = False then; 
        return "E_NO_SUCH_DB"; 
    endif; 
    if db_isvalid(db_iskey_file) = False then; 
        return "E_INVALID_DB"; 
    endif; 
    db_iskey_fp = f_open(db_iskey_file,"r"); 
    f_linein(db_iskey_fp); 
    do until eof(db_iskey_fp); 
        iskey_entry_parts =  split(f_linein(db_iskey_fp),"="); 
        if ubound(iskey_entry_parts) < 1 then; 
            f_close(db_iskey_fp); 
            return "E_INVALID_ENTRY"; 
        endif; 
        if iskey_entry_parts[0] = db_iskey_key then; 
            f_close(db_iskey_fp); 
            return True; 
        endif; 
    loop; 
    f_close(db_iskey_fp); 
    return False; 
} 

function db_fetch(db_fetch_file, db_fetch_key) 
{ 
    newvar db_fetch_fp; 
    newvar fetch_entry_parts[]; 

    if fileexist(db_fetch_file) = False then; 
        return "E_NO_SUCH_DB"; 
    endif; 
    if db_isvalid(db_fetch_file) = False then; 
        return "E_INVALID_DB"; 
    endif; 
    db_fetch_fp = f_open(db_fetch_file,"r"); 
    f_linein(db_fetch_fp); 
    do until eof(db_fetch_fp); 
        fetch_entry_parts =  split(f_linein(db_fetch_fp),"="); 
        if ubound(fetch_entry_parts) < 1 then; 
            f_close(db_fetch_fp); 
            return "E_INVALID_ENTRY"; 
        endif; 
        if fetch_entry_parts[0] = db_fetch_key then; 
            f_close(db_fetch_fp); 
            return fetch_entry_parts[1]; 
        endif; 
    loop; 
    f_close(db_fetch_fp); 
    return "E_NO_SUCH_KEY"; 
} 

function db_insert(db_insert_file, db_insert_key, db_insert_value) 
{ 
    newvar db_insert_fp; 
    newvar insert_entry; 

    if fileexist(db_insert_file) = False then; 
        return "E_NO_SUCH_DB"; 
    endif; 
    if db_isvalid(db_insert_file) = False then; 
        return "E_INVALID_DB"; 
    endif; 
    if db_iskey(db_insert_file,db_insert_key) = True then; 
        return "E_KEY_EXISTS"; 
    endif; 
    db_insert_fp = f_open(db_insert_file,"a"); 
    insert_entry = db_insert_key & "=" & db_insert_value; 
    f_out(db_insert_fp,insert_entry); 
    f_close(db_insert_fp); 
    return "S_ENTRY_INSERTED"; 
} 

function db_delete(db_delete_file, db_delete_key) 
{ 
    newvar db_delete_fp; 
    newvar old_file_line = 0, old_file_parts[]; 
    newvar delete_entry_parts[], entry; 

    if fileexist(db_delete_file) = False then; 
        return "E_NO_SUCH_DB"; 
    endif; 
    if db_isvalid(db_delete_file) = False then; 
        return "E_INVALID_DB"; 
    endif; 
    if db_iskey(db_delete_file,db_delete_key) = False then; 
        return "E_NO_SUCH_KEY"; 
    endif; 
    old_file_parts = split(readfile(db_delete_file),dpp.crlf); 
    if ubound(old_file_parts) < 0 then; 
        return "E_DB_EMPTY"; 
    endif; 
    delete db_delete_file; 
    db_delete_fp = f_open(db_delete_file,"w"); 
    for old_file_line = 0 to ubound(old_file_parts) - 1; 
        entry = old_file_parts[old_file_line]; 
        delete_entry_parts = split(entry,"="); 
        if ubound(delete_entry_parts) > 0 then; 
            if delete_entry_parts[0] <> db_delete_key then; 
                f_out(db_delete_fp,old_file_parts[old_file_line]); 
            endif; 
        else; 
            if old_file_parts[old_file_line] = "#!DPPDB/VALID_DATABASE" then; 
                f_out(db_delete_fp,old_file_parts[old_file_line]); 
            endif; 
        endif; 
    next old_file_line; 
    f_close(db_delete_fp); 
    return "S_KEY_DELETED"; 
} 



This code is Copyright (C) 2000-2012 PageMac Programming. Reproduction of this code must adhere to the GNU General Public License.