EnglishРусский  

   ..

   defines.c

   defines.h

The project is closed! You can look at a new scripting language. It is available on GitHub.
Also, try our open source cross-platform automation software.

Ads

Installer and installation software
Commercial and Freeware installers.

source\src\os\user\defines.c
  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: defines 18.10.06 0.0.A.
 11 *
 12 * Author: Alexey Krivonogov
 13 *
 14 * Summary: This file provides Windows basic types and some constants.
 15 *
 16 ******************************************************************************/
 17 
 18 #include "defines.h"
 19 #include "../../genteeapi/gentee.h"
 20 
 21 pvoid  _stdout = INVALID_HANDLE_VALUE;
 22 pvoid  _stdin = INVALID_HANDLE_VALUE;
 23 
 24 /*-----------------------------------------------------------------------------
 25 *
 26 * ID: os_dircreate 23.10.06 0.0.A.
 27 * 
 28 * Summary: Create the directory
 29 *
 30 -----------------------------------------------------------------------------*/
 31 
 32 uint     STDCALL os_dircreate( pstr name )
 33 {
 34    return CreateDirectory( str_ptr( name ), NULL );
 35 }
 36 
 37 /*-----------------------------------------------------------------------------
 38 *
 39 * ID: os_dirdelete 23.10.06 0.0.A.
 40 * 
 41 * Summary: Delete the empty directory
 42 *
 43 -----------------------------------------------------------------------------*/
 44 
 45 uint     STDCALL os_dirdelete( pstr name )
 46 {
 47    return RemoveDirectory( str_ptr( name ));
 48 }
 49 
 50 /*-----------------------------------------------------------------------------
 51 *
 52 * ID: os_dirgetcur 23.10.06 0.0.A.
 53 * 
 54 * Summary: Get the current directory
 55 *
 56 -----------------------------------------------------------------------------*/
 57 
 58 pstr   STDCALL os_dirgetcur( pstr name ) 
 59 {
 60    return str_setlen( name, GetCurrentDirectory( 512, 
 61                       str_ptr( str_reserve( name, 512 ))));
 62 }
 63 
 64 /*-----------------------------------------------------------------------------
 65 *
 66 * ID: os_dirsetcur 23.10.06 0.0.A.
 67 * 
 68 * Summary: Set the current directory
 69 *
 70 -----------------------------------------------------------------------------*/
 71 
 72 uint  STDCALL os_dirsetcur( pstr name ) 
 73 {
 74    return SetCurrentDirectory( str_ptr( name ));
 75 }
 76 
 77 /*-----------------------------------------------------------------------------
 78 *
 79 * ID: os_dirdeletefull 23.10.06 0.0.A.
 80 * 
 81 * Summary: Delete the directory with subfolders and files
 82 *
 83 -----------------------------------------------------------------------------*/
 84 
 85 uint     STDCALL os_dirdeletefull( pstr name )
 86 {
 87    str  stemp;
 88    WIN32_FIND_DATA  data;
 89    pvoid            find;
 90 
 91    str_init( &stemp );
 92    str_reserve( &stemp, 512 );
 93    str_printf( &stemp, "%s%c*.*", str_ptr( name ), SLASH );
 94    find = FindFirstFile( str_ptr( &stemp ), &data );
 95    if ( find != INVALID_HANDLE_VALUE )
 96    {
 97       do { 
 98          if ( data.cFileName[0] == '.' && ( !data.cFileName[1] ||
 99             ( data.cFileName[1] == '.' && !data.cFileName[2] )))
100             continue;
101          str_clear( &stemp );        
102          str_printf( &stemp, "%s%c%s", str_ptr( name ), SLASH, data.cFileName );
103 
104          if ( data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
105             os_dirdeletefull( &stemp );
106          else
107             os_filedelete( &stemp );
108 //         print( "%s\n", str_ptr( &stemp ));
109       } while ( FindNextFile( find, &data ));
110 
111       FindClose( find );
112    }
113 //   _getch();
114    str_delete( &stemp );
115    return os_dirdelete( name );
116 }
117 
118 /*-----------------------------------------------------------------------------
119 *
120 * ID: os_fileclose 23.10.06 0.0.A.
121 * 
122 * Summary: Close the file
123 *
124 -----------------------------------------------------------------------------*/
125 
126 uint     STDCALL os_fileclose( uint handle )
127 {
128    return CloseHandle(( pvoid )handle );
129 }
130 
131 /*-----------------------------------------------------------------------------
132 *
133 * ID: os_filefullname 23.10.06 0.0.A.
134 * 
135 * Summary: Get the full name of the file
136 *
137 -----------------------------------------------------------------------------*/
138 
139 pstr  STDCALL os_filefullname( pstr filename, pstr result )
140 {
141    uint   len;
142    pubyte ptr;
143 
144    str_reserve( result, 512 );
145    len = GetFullPathName( str_ptr( filename ), 512, str_ptr( result ), &ptr );
146    str_setlen( result, len );
147    return result;
148 }
149 
150 /*-----------------------------------------------------------------------------
151 *
152 * ID: os_filedelete 23.10.06 0.0.A.
153 * 
154 * Summary: Delete the file
155 *
156 -----------------------------------------------------------------------------*/
157 
158 uint     STDCALL os_filedelete( pstr name )
159 {
160    return DeleteFile( str_ptr( name ));
161 }
162 
163 uint  STDCALL os_fileopen( pstr name, uint flag )
164 {
165    uint ret;
166 
167    ret = ( uint )CreateFile( str_ptr( name ), ( flag & FOP_READONLY ? GENERIC_READ : 
168             GENERIC_READ | GENERIC_WRITE ), ( flag & FOP_EXCLUSIVE ? 0 : 
169 // ( flag & FOP_READONLY ? FILE_SHARE_READ : FILE_SHARE_READ | FILE_SHARE_WRITE )
170             FILE_SHARE_READ | FILE_SHARE_WRITE ), NULL, 
171            ( flag & FOP_CREATE ? CREATE_ALWAYS :
172               ( flag & FOP_IFCREATE ? OPEN_ALWAYS : OPEN_EXISTING )),
173            /*FILE_FLAG_WRITE_THROUGH*/ 0, NULL ); 
174 //   printf("Name=%s %i\n", str_ptr( name ), ret );
175    return ret == ( uint )INVALID_HANDLE_VALUE ? 0 : ret ;
176 }
177 
178 ulong64  STDCALL os_filepos( uint handle, long64 offset, uint mode )
179 {
180    LARGE_INTEGER  li;
181 
182    li.QuadPart = offset;
183 
184    li.LowPart = SetFilePointer( ( pvoid )handle, li.LowPart, &li.HighPart, 
185        ( mode == FSET_BEGIN ?
186       FILE_BEGIN : ( mode == FSET_CURRENT ? FILE_CURRENT : FILE_END )));
187    if ( li.LowPart == MAX_UINT && GetLastError() != NO_ERROR )
188       return -1L;
189    return li.QuadPart;
190 }
191 
192 uint   STDCALL os_fileread( uint handle, pubyte data, uint size )
193 {
194    uint  read;
195 
196    if ( !ReadFile( (pvoid)handle, data, size, &read, NULL ) || read != size ) 
197       return FALSE;
198    return read;
199 }
200 
201 ulong64  STDCALL os_filesize( uint handle )
202 {
203    LARGE_INTEGER  li;
204 
205    li.LowPart = GetFileSize( ( pvoid )handle, &li.HighPart ); 
206  
207    if ( li.LowPart == INVALID_FILE_SIZE && GetLastError() != NO_ERROR )
208       return -1L;
209 
210    return li.QuadPart;
211 }
212 
213 //--------------------------------------------------------------------------
214 
215 uint   STDCALL os_filewrite( uint handle, pubyte data, uint size )
216 {
217    uint  write;
218 
219    if ( !WriteFile( ( pvoid )handle, data, size, &write, NULL ) || write != size ) 
220       return FALSE;
221    return write;
222 }
223 
224 /*-----------------------------------------------------------------------------
225 *
226 * ID: os_fileexist 23.10.06 0.0.A.
227 * 
228 * Summary: If the file or directory exists
229 *
230 -----------------------------------------------------------------------------*/
231 
232 uint STDCALL os_fileexist( pstr name )
233 {
234    return os_getattrib( name ) != 0xFFFFFFFF ? 1 : 0;
235 }
236 
237 /*-----------------------------------------------------------------------------
238 *
239 * ID: os_getattrib 23.10.06 0.0.A.
240 * 
241 * Summary: Get the file or directory attrbutes
242 *
243 -----------------------------------------------------------------------------*/
244 
245 uint  STDCALL os_getattrib( pstr name )
246 {
247    return GetFileAttributes( str_ptr( name ));
248 }
249 
250 /*-----------------------------------------------------------------------------
251 *
252 * ID: os_tempdir 23.10.06 0.0.A.
253 * 
254 * Summary: Get the temp dir
255 *
256 -----------------------------------------------------------------------------*/
257 
258 pstr  STDCALL os_tempdir( pstr name )
259 {
260    str_setlen( name, GetTempPath( 1024, str_reserve( name, 1024 )->data ));
261    return str_trim( name, SLASH, TRIM_ONE | TRIM_RIGHT );
262 }
263 
264 #ifndef NOGENTEE
265 
266 pstr  STDCALL os_gettemp( void )
267 {
268 //   ubyte temp[ 512 ];
269 //   ubyte stemp[ 512 ];
270 
271 //   str_lenset( dir, 0 );
272 //   str_isfree( dir, 512 );
273 /*#ifdef LINUX
274    if ( !ggentee.tempfile )
275    {
276       while( 1 )
277       {
278          wsprintf( stemp, "/temp/gentee%02X.tmp", ggentee.tempid );
279          ggentee.tempfile = file_open( stemp, FOP_CREATE | FOP_EXCLUSIVE );
280          if ( ggentee.tempfile = -1 )
281             ggentee.tempid++;
282          else
283             break;
284       }
285       stemp[ mem_len( stemp ) - 4 ] = 0;
286 //      wsprintf( stemp, "%s\\gentee%02X", temp, ggentee.tempid );
287       mkdir( stemp, 700 );
288       ggentee.tempdir = str_new( 0, stemp );
289    }
290 //   str_appendp( dir, str_ptr( ggentee.tempdir ));
291 #else*/
292    if ( !str_len( &_gentee.tempdir ))
293    {
294       str    path;
295       pstr   ps;
296       uint   diskc = 0;
297 //      byte qq[512];
298 
299       ps = &_gentee.tempdir;
300 
301       os_tempdir( str_init( &path ));
302       
303       _gentee.tempid = ( _gentee.flags & G_TMPRAND ? GetTickCount() & 0xFF : 0 );
304       while ( 1 )
305       {
306          str_clear( ps );
307          str_printf( ps, "%s%cgentee%02X.tmp", str_ptr( &path ), SLASH, _gentee.tempid );
308          _gentee.tempfile = os_fileopen( ps, FOP_CREATE | FOP_EXCLUSIVE );
309 //         sprintf(qq, "0x02%x %i = %s\n", _gentee.flags, _gentee.tempid, str_ptr(ps));
310 //         MessageBox( NULL, qq, qq, MB_OK );
311          if ( !_gentee.tempfile )
312          {
313             if ( os_getattrib( ps ) == 0xFFFFFFFF && !diskc )
314             {
315                str_copyzero( &path, "c:\\temp" );
316                os_dircreate( &path );
317                diskc = 1;
318             }
319             if ( _gentee.tempid++ > 0xFFFF )
320                msg( MFileopen | MSG_STR | MSG_EXIT, ps );
321          }
322          else
323          {
324             str_setlen( ps, str_len( ps ) - 4 );
325             os_dirdeletefull( ps );
326             break;
327          }
328       }
329 //      os_dircreate( str_setlen( ps, str_len( ps ) - 4 ));
330       os_dircreate( ps );
331       str_delete( &path );
332    }
333 //#endif
334    return &_gentee.tempdir;
335 }
336 
337 /*-----------------------------------------------------------------------------
338 *
339 * ID: os_init 23.10.06 0.0.A.
340 * 
341 * Summary: Initializing input and output.
342 *
343 -----------------------------------------------------------------------------*/
344 
345 void   STDCALL os_init( uint param )
346 {
347    if ( param )  // if ( !GetConsoleWindow( ))
348       AllocConsole();
349    else
350    {
351       CPINFO cpinfo;
352 
353       GetCPInfo( CP_ACP, &cpinfo );
354       _gentee.multib = cpinfo.MaxCharSize > 1 ? 1 : 0;
355    }
356    if ( _gentee.flags & G_CONSOLE || param )
357    {
358       _stdout = GetStdHandle( STD_OUTPUT_HANDLE );
359       _stdin = GetStdHandle( STD_INPUT_HANDLE );
360    }
361 }
362 
363 /*-----------------------------------------------------------------------------
364 *
365 * ID: os_print 23.10.06 0.0.A.
366 * 
367 * Summary: Print a text to the console.
368 *
369 -----------------------------------------------------------------------------*/
370 
371 void   STDCALL os_print( pubyte ptr, uint len )
372 {
373    uint    write;
374    pubyte  charprn;
375 
376    if ( _gentee.flags & G_CHARPRN )
377    {
378       charprn = ( pubyte )mem_alloc( len + 1 );
379       CharToOem( ptr, charprn );
380       ptr = charprn;
381    }
382    if ( _gentee.flags & G_CONSOLE )
383       WriteFile( _stdout, ptr, len, &write, 0 );
384    else
385    {
386       if ( _stdout == INVALID_HANDLE_VALUE )
387           os_init( 1 );
388       WriteConsole( _stdout, ptr, len, &write, NULL );
389    }
390    
391    if ( _gentee.flags & G_CHARPRN )
392       mem_free( charprn );
393 }
394 
395 /*-----------------------------------------------------------------------------
396 *
397 * ID: os_getch 23.10.06 0.0.A.
398 * 
399 * Summary: Get a character form the console.
400 *
401 -----------------------------------------------------------------------------*/
402 
403 uint    STDCALL os_getchar( void )
404 {
405    uint  mode, get;
406    ubyte input[8];
407 
408    if ( _gentee.getch )
409       return _gentee.getch( 0, 1 );
410 
411    if ( _stdin == INVALID_HANDLE_VALUE )
412       os_init( 1 );
413 
414    GetConsoleMode( _stdin, &mode );
415    SetConsoleMode( _stdin, 0 );
416    ReadConsole( _stdin, input, 1, &get, NULL );
417    SetConsoleMode( _stdin, mode );
418 //   return _getch();
419    return input[0];
420 }
421 
422 /*-----------------------------------------------------------------------------
423 *
424 * ID: os_scan 23.10.06 0.0.A.
425 * 
426 * Summary: Get characters form the console.
427 *
428 -----------------------------------------------------------------------------*/
429 
430 uint  STDCALL os_scan( pubyte input, uint len ) 
431 {
432    uint   read;
433 
434    if ( _gentee.getch )
435       return _gentee.getch( input, len );
436 
437    if ( _stdin == INVALID_HANDLE_VALUE )
438       os_init( 1 );
439    ReadConsole( _stdin, input, len, &read, NULL );
440 
441    return read;
442 }
443 
444 #endif // NOGENTEE
445 
446 /*-----------------------------------------------------------------------------
447 *
448 * ID: os_strcmplen 23.10.06 0.0.A.
449 * 
450 * Summary: Compare strings
451 *
452 -----------------------------------------------------------------------------*/
453 
454 int   STDCALL os_strcmplen( pubyte one, pubyte two, uint len )
455 {
456    int cmp = CompareString( LOCALE_USER_DEFAULT, 0, one, len, two, len );
457    if ( cmp == CSTR_LESS_THAN )
458       return -1;
459    if ( cmp == CSTR_GREATER_THAN )
460       return 1;
461    return 0;
462 }
463 
464 /*-----------------------------------------------------------------------------
465 *
466 * ID: os_strcmpignlen 23.10.06 0.0.A.
467 * 
468 * Summary: Compare strings
469 *
470 -----------------------------------------------------------------------------*/
471 
472 int   STDCALL os_strcmpignlen( pubyte one, pubyte two, uint len )
473 {
474    int cmp = CompareString( LOCALE_USER_DEFAULT, NORM_IGNORECASE, one, len, two, len );
475 
476    if ( cmp == CSTR_LESS_THAN )
477       return -1;
478    if ( cmp == CSTR_GREATER_THAN )
479       return 1;
480 
481    return 0;
482 }
483 
484 pvoid    STDCALL os_thread( pvoid pfunc, pvoid param )
485 {
486    uint   id;
487 
488    return CreateThread( 0, 0, pfunc, param, 0, &id );
489 }
490 
491 /*-----------------------------------------------------------------------------
492 *
493 * ID: os_ustrcmplen 23.10.06 0.0.A.
494 * 
495 * Summary: Compare strings
496 *
497 -----------------------------------------------------------------------------*/
498 
499 int   STDCALL os_ustrcmplen( pushort one, pushort two, uint len )
500 {
501    int cmp = CompareStringW( LOCALE_USER_DEFAULT, 0, one, len, two, len );
502    if ( cmp == CSTR_LESS_THAN )
503       return -1;
504    if ( cmp == CSTR_GREATER_THAN )
505       return 1;
506    return 0;
507 }
508 
509 /*-----------------------------------------------------------------------------
510 *
511 * ID: os_strcmpignlen 23.10.06 0.0.A.
512 * 
513 * Summary: Compare strings
514 *
515 -----------------------------------------------------------------------------*/
516 
517 int   STDCALL os_ustrcmpignlen( pushort one, pushort two, uint len )
518 {
519    int cmp = CompareStringW( LOCALE_USER_DEFAULT, NORM_IGNORECASE, one, len, two, len );
520 
521    if ( cmp == CSTR_LESS_THAN )
522       return -1;
523    if ( cmp == CSTR_GREATER_THAN )
524       return 1;
525 
526    return 0;
527 }
528 
529 /*-----------------------------------------------------------------------------
530 *
531 * ID: os_strcmp 23.10.06 0.0.A.
532 * 
533 * Summary: Compare strings
534 *
535 -----------------------------------------------------------------------------*/
536 
537 int   STDCALL os_strcmp( pubyte one, pubyte two )
538 {
539    return os_strcmplen( one, two, -1 );
540 }
541 
542 /*-----------------------------------------------------------------------------
543 *
544 * ID: os_strcmpign 23.10.06 0.0.A.
545 * 
546 * Summary: Compare strings
547 *
548 -----------------------------------------------------------------------------*/
549 
550 int   STDCALL os_strcmpign( pubyte one, pubyte two )
551 {
552    return os_strcmpignlen( one, two, -1 );
553 }
554 
555 /*-----------------------------------------------------------------------------
556 *
557 * ID: os_strcmp 23.10.06 0.0.A.
558 * 
559 * Summary: Compare strings
560 *
561 -----------------------------------------------------------------------------*/
562 
563 int   STDCALL os_ustrcmp( pushort one, pushort two )
564 {
565    return os_ustrcmplen( one, two, -1 );
566 }
567 
568 /*-----------------------------------------------------------------------------
569 *
570 * ID: os_strcmpign 23.10.06 0.0.A.
571 * 
572 * Summary: Compare strings
573 *
574 -----------------------------------------------------------------------------*/
575 
576 int   STDCALL os_ustrcmpign( pushort one, pushort two )
577 {
578    return os_ustrcmpignlen( one, two, -1 );
579 }
580 
581 //--------------------------------------------------------------------------
582 
583 /*
584 pvoid STDCALL os_alloc( uint size )
585 {
586    return VirtualAlloc( NULL, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE );
587 }
588 
589 void STDCALL os_free( pvoid ptr )
590 {
591    VirtualFree( ptr, 0, MEM_RELEASE );
592 }
593 */ 
594 //--------------------------------------------------------------------------
595