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 return str_copy( name, os_gettemp());
107 }
108
109 /*-----------------------------------------------------------------------------
110 *
111 * ID: gettempfile 23.10.06 0.0.A.
112 *
113 * Summary: Get the filename in the temp dir
114 *
115 -----------------------------------------------------------------------------*/
116
117 pstr STDCALL gettempfile( pstr name, pstr additional )
118 {
119 str tempdir;
120
121 str_init( &tempdir );
122 gettempdir( &tempdir );
123 str_dirfile( &tempdir, additional, name );
124 str_delete( &tempdir );
125
126 return name;
127 }
128
129 /*-----------------------------------------------------------------------------
130 *
131 * ID: getexefile 23.10.06 0.0.A.
132 *
133 * Summary: Get the filename in the exe dir
134 *
135 -----------------------------------------------------------------------------*/
136
137 pstr STDCALL getmodulename( pstr name )
138 {
139 uint i;
140
141 str_reserve( name, 512 );
142 i = GetModuleFileName( 0, str_ptr( name ), 511 );
143 str_setlen( name, i );
144
145 return name;
146 }
147
148 /*-----------------------------------------------------------------------------
149 *
150 * ID: getexedir 23.10.06 0.0.A.
151 *
152 * Summary: Get the filename in the exe dir
153 *
154 -----------------------------------------------------------------------------*/
155
156 pstr STDCALL getmodulepath( pstr name, pstr additional )
157 {
158 str temp;
159
160 str_init( &temp );
161
162 #ifdef LINUX
163 //Only for linux ???
164 // dir[0] = 0;
165 // name[0] = 0;
166 len = readlink( "/proc/self/exe", dir, 512 ) - 1;
167 while ( len && dir[ len ] != '/' ) len--;
168 mem_copyuntilzero( dir + len + 1, name );
169 #else
170 getmodulename( name );
171 str_getdirfile( name, &temp, NULL );
172 str_dirfile( &temp, additional, name );
173 #endif
174 str_delete( &temp );
175 return name;
176 }
177
178 //--------------------------------------------------------------------------
179
Редактировать