1 /******************************************************************************
2 *
3 * Copyright (C) 2006, The Gentee Group. All rights reserved.
4 * This file is part of the Gentee open source project - http://www.gentee.com.
5 *
6 * THIS FILE IS PROVIDED UNDER THE TERMS OF THE GENTEE LICENSE ("AGREEMENT").
7 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE CONSTITUTES RECIPIENTS
8 * ACCEPTANCE OF THE AGREEMENT.
9 *
10 * ID: file 18.10.06 0.0.A.
11 *
12 * Author: Alexey Krivonogov
13 *
14 * Summary: This file provides file system functions.
15 *
16 ******************************************************************************/
17
18 #include "../os/user/defines.h"
19 #include "../genteeapi/gentee.h"
20 #include "../common/arrdata.h"
21
22 /*-----------------------------------------------------------------------------
23 *
24 * ID: file2buf 23.10.06 0.0.A.
25 *
26 * Summary: Read file to buf. Result must be destroy later. name is converted
27 into the full name.
28 *
29 -----------------------------------------------------------------------------*/
30
31 pbuf STDCALL file2buf( pstr name, pbuf ret, uint pos )
32 {
33 str filename;
34 uint search = 0;
35 uint handle, size;
36 parrdata pad;
37
38 str_init( &filename );
39 os_filefullname( name, &filename );
40
41 if ( _compile && str_findch( name, SLASH ) >= str_len( name ))
42 {
43 search = 1;
44 pad = &_compile->libdirs;
45 }
46 again:
47 handle = os_fileopen( &filename, FOP_READONLY );
48 if ( !handle )
49 {
50 if ( search && search <= arr_count( pad ))
51 {
52 str_dirfile( arrdata_get( pad, search++ - 1 ), name, &filename );
53 goto again;
54 }
55 msg( MFileopen | MSG_STR | MSG_EXIT | MSG_POS, &filename, pos );
56 }
57 size = ( uint )os_filesize( handle );
58 buf_reserve( ret, size );
59
60 if ( size && !os_fileread( handle, buf_ptr( ret ), size ))
61 msg( MFileread | MSG_STR | MSG_EXIT | MSG_POS, &filename, pos );
62
63 buf_setlen( ret, size );
64 os_fileclose( handle );
65
66 str_copy( name, &filename );
67
68 str_delete( &filename );
69
70 return ret;
71 }
72
73 uint STDCALL buf2file( pstr name, pbuf out )
74 {
75 str filename;
76 uint handle;
77
78 str_init( &filename );
79 os_filefullname( name, &filename );
80
81 handle = os_fileopen( &filename, FOP_CREATE );
82 if ( !handle )
83 msg( MFileopen | MSG_STR | MSG_EXIT, &filename );
84
85 if ( !os_filewrite( handle, buf_ptr( out ), buf_len( out ) ))
86 msg( MFileread | MSG_STR | MSG_EXIT, &filename );
87
88 os_fileclose( handle );
89
90 str_copy( name, &filename );
91 str_delete( &filename );
92
93 return 1;
94 }
95
96 /*-----------------------------------------------------------------------------
97 *
98 * ID: gettempdir 23.10.06 0.0.A.
99 *
100 * Summary: Get the application's temp dir
101 *
102 -----------------------------------------------------------------------------*/
103
104 pstr STDCALL gettempdir( pstr name )
105 {
106 if ( !str_len( &_gentee.tempdir ))
107 {
108 uint id = 0;
109 str path;
110 pstr ps;
111
112 ps = &_gentee.tempdir;
113
114 os_tempdir( str_init( &path ));
115
116 while ( 1 )
117 {
118 str_printf( ps, "%s%cgentee%02X.tmp", str_ptr( &path ), SLASH, id );
119
120 _gentee.tempfile = os_fileopen( ps, FOP_CREATE | FOP_EXCLUSIVE );
121
122 if ( !_gentee.tempfile )
123 {
124 if ( id++ > 0xFFFF )
125 msg( MFileopen | MSG_STR | MSG_EXIT, ps );
126 }
127 else
128 break;
129 }
130 os_dircreate( str_setlen( ps, str_len( ps ) - 4 ));
131 str_delete( &path );
132 }
133 return str_copy( name, &_gentee.tempdir );
134 }
135
136 /*-----------------------------------------------------------------------------
137 *
138 * ID: gettempfile 23.10.06 0.0.A.
139 *
140 * Summary: Get the filename in the temp dir
141 *
142 -----------------------------------------------------------------------------*/
143
144 pstr STDCALL gettempfile( pstr name, pstr additional )
145 {
146 str tempdir;
147
148 str_init( &tempdir );
149 gettempdir( &tempdir );
150 str_dirfile( &tempdir, additional, name );
151 str_delete( &tempdir );
152
153 return name;
154 }
155
156 /*-----------------------------------------------------------------------------
157 *
158 * ID: getexefile 23.10.06 0.0.A.
159 *
160 * Summary: Get the filename in the exe dir
161 *
162 -----------------------------------------------------------------------------*/
163
164 pstr STDCALL getmodulename( pstr name )
165 {
166 uint i;
167
168 str_reserve( name, 512 );
169 i = GetModuleFileName( 0, str_ptr( name ), 511 );
170 str_setlen( name, i );
171
172 return name;
173 }
174
175 /*-----------------------------------------------------------------------------
176 *
177 * ID: getexedir 23.10.06 0.0.A.
178 *
179 * Summary: Get the filename in the exe dir
180 *
181 -----------------------------------------------------------------------------*/
182
183 pstr STDCALL getmodulepath( pstr name, pstr additional )
184 {
185 str temp;
186
187 str_init( &temp );
188
189 #ifdef LINUX
190 //Only for linux ???
191 // dir[0] = 0;
192 // name[0] = 0;
193 len = readlink( "/proc/self/exe", dir, 512 ) - 1;
194 while ( len && dir[ len ] != '/' ) len--;
195 mem_copyuntilzero( dir + len + 1, name );
196 #else
197 getmodulename( name );
198 str_getdirfile( name, &temp, NULL );
199 str_dirfile( &temp, additional, name );
200 #endif
201 str_delete( &temp );
202 return name;
203 }
204
205 //--------------------------------------------------------------------------