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\win32\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    uint  diskc = 0;
269    pstr  temp;
270    pstr  ret;
271 //   ubyte temp[ 512 ];
272 //   ubyte stemp[ 512 ];
273 
274 //   str_lenset( dir, 0 );
275 //   str_isfree( dir, 512 );
276 /*#ifdef LINUX
277    if ( !ggentee.tempfile )
278    {
279       while( 1 )
280       {
281          wsprintf( stemp, "/temp/gentee%02X.tmp", ggentee.tempid );
282          ggentee.tempfile = file_open( stemp, FOP_CREATE | FOP_EXCLUSIVE );
283          if ( ggentee.tempfile = -1 )
284             ggentee.tempid++;
285          else
286             break;
287       }
288       stemp[ mem_len( stemp ) - 4 ] = 0;
289 //      wsprintf( stemp, "%s\\gentee%02X", temp, ggentee.tempid );
290       mkdir( stemp, 700 );
291       ggentee.tempdir = str_new( 0, stemp );
292    }
293 //   str_appendp( dir, str_ptr( ggentee.tempdir ));
294 #else*/
295    if ( !_gentee.tempfile )
296    {
297       temp = str_new( NULL );
298       ret = str_new( NULL );
299       os_tempdir( temp );
300 
301       while ( 1 )
302       {
303          str_clear( ret );
304          str_printf( ret, "%s\\gentee%02X.tmp", str_ptr( temp ), _gentee.tempid );
305    
306          _gentee.tempfile = os_fileopen( ret, FOP_CREATE | FOP_EXCLUSIVE );
307             
308          if ( !_gentee.tempfile )
309          {
310             if ( os_getattrib( ret ) == 0xFFFFFFFF )
311                if ( !diskc )
312                {
313                   str_copyzero( temp, "c:\\temp" );
314                   os_dircreate( temp );
315                   diskc = 1;
316                }
317                else
318                   msg( MFileopen | MSG_STR, ret );
319             _gentee.tempid++;
320          }
321          else
322             break;
323       }
324       str_setlen( ret, str_len( ret ) - 4 );
325       os_dircreate( ret );
326       str_copy( &_gentee.tempdir, ret );
327 
328       str_destroy( temp );
329       str_destroy( ret );
330    }
331 //#endif
332    return &_gentee.tempdir;
333 }
334 
335 /*-----------------------------------------------------------------------------
336 *
337 * ID: os_init 23.10.06 0.0.A.
338 * 
339 * Summary: Initializing input and output.
340 *
341 -----------------------------------------------------------------------------*/
342 
343 void   STDCALL os_init( uint param )
344 {
345    if ( param )  // if ( !GetConsoleWindow( ))
346       AllocConsole();
347    else
348    {
349       CPINFO cpinfo;
350 
351       GetCPInfo( CP_ACP, &cpinfo );
352       _gentee.multib = cpinfo.MaxCharSize > 1 ? 1 : 0;
353    }
354    if ( _gentee.flags & G_CONSOLE || param )
355    {
356       _stdout = GetStdHandle( STD_OUTPUT_HANDLE );
357       _stdin = GetStdHandle( STD_INPUT_HANDLE );
358    }
359 }
360 
361 /*-----------------------------------------------------------------------------
362 *
363 * ID: os_print 23.10.06 0.0.A.
364 * 
365 * Summary: Print a text to the console.
366 *
367 -----------------------------------------------------------------------------*/
368 
369 void   STDCALL os_print( pubyte ptr, uint len )
370 {
371    uint    write;
372    pubyte  charprn;
373 
374    if ( _gentee.flags & G_CHARPRN )
375    {
376       charprn = ( pubyte )mem_alloc( len + 1 );
377       CharToOem( ptr, charprn );
378       ptr = charprn;
379    }
380    if ( _gentee.flags & G_CONSOLE )
381       WriteFile( _stdout, ptr, len, &write, 0 );
382    else
383    {
384       if ( _stdout == INVALID_HANDLE_VALUE )
385           os_init( 1 );
386       WriteConsole( _stdout, ptr, len, &write, NULL );
387    }
388    
389    if ( _gentee.flags & G_CHARPRN )
390       mem_free( charprn );
391 }
392 
393 /*-----------------------------------------------------------------------------
394 *
395 * ID: os_getch 23.10.06 0.0.A.
396 * 
397 * Summary: Get a character form the console.
398 *
399 -----------------------------------------------------------------------------*/
400 
401 uint    STDCALL os_getchar( void )
402 {
403    uint  mode, get;
404    ubyte input[8];
405 
406    if ( _gentee.getch )
407       return _gentee.getch( 0, 1 );
408 
409    if ( _stdin == INVALID_HANDLE_VALUE )
410       os_init( 1 );
411 
412    GetConsoleMode( _stdin, &mode );
413    SetConsoleMode( _stdin, 0 );
414    ReadConsole( _stdin, input, 1, &get, NULL );
415    SetConsoleMode( _stdin, mode );
416 //   return _getch();
417    return input[0];
418 }
419 
420 /*-----------------------------------------------------------------------------
421 *
422 * ID: os_scan 23.10.06 0.0.A.
423 * 
424 * Summary: Get characters form the console.
425 *
426 -----------------------------------------------------------------------------*/
427 
428 uint  STDCALL os_scan( pubyte input, uint len ) 
429 {
430    uint   read;
431 
432    if ( _gentee.getch )
433       return _gentee.getch( input, len );
434 
435    if ( _stdin == INVALID_HANDLE_VALUE )
436       os_init( 1 );
437    ReadConsole( _stdin, input, len, &read, NULL );
438 
439    return read;
440 }
441 
442 #endif // NOGENTEE
443 
444 /*-----------------------------------------------------------------------------
445 *
446 * ID: os_strcmplen 23.10.06 0.0.A.
447 * 
448 * Summary: Compare strings
449 *
450 -----------------------------------------------------------------------------*/
451 
452 int   STDCALL os_strcmplen( pubyte one, pubyte two, uint len )
453 {
454    int cmp = CompareString( LOCALE_USER_DEFAULT, 0, one, len, two, len );
455    if ( cmp == CSTR_LESS_THAN )
456       return -1;
457    if ( cmp == CSTR_GREATER_THAN )
458       return 1;
459    return 0;
460 }
461 
462 /*-----------------------------------------------------------------------------
463 *
464 * ID: os_strcmpignlen 23.10.06 0.0.A.
465 * 
466 * Summary: Compare strings
467 *
468 -----------------------------------------------------------------------------*/
469 
470 int   STDCALL os_strcmpignlen( pubyte one, pubyte two, uint len )
471 {
472    int cmp = CompareString( LOCALE_USER_DEFAULT, NORM_IGNORECASE, one, len, two, len );
473 
474    if ( cmp == CSTR_LESS_THAN )
475       return -1;
476    if ( cmp == CSTR_GREATER_THAN )
477       return 1;
478 
479    return 0;
480 }
481 
482 pvoid    STDCALL os_thread( pvoid pfunc, pvoid param )
483 {
484    uint   id;
485 
486    return CreateThread( 0, 0, pfunc, param, 0, &id );
487 }
488 
489 /*-----------------------------------------------------------------------------
490 *
491 * ID: os_ustrcmplen 23.10.06 0.0.A.
492 * 
493 * Summary: Compare strings
494 *
495 -----------------------------------------------------------------------------*/
496 
497 int   STDCALL os_ustrcmplen( pushort one, pushort two, uint len )
498 {
499    int cmp = CompareStringW( LOCALE_USER_DEFAULT, 0, one, len, two, len );
500    if ( cmp == CSTR_LESS_THAN )
501       return -1;
502    if ( cmp == CSTR_GREATER_THAN )
503       return 1;
504    return 0;
505 }
506 
507 /*-----------------------------------------------------------------------------
508 *
509 * ID: os_strcmpignlen 23.10.06 0.0.A.
510 * 
511 * Summary: Compare strings
512 *
513 -----------------------------------------------------------------------------*/
514 
515 int   STDCALL os_ustrcmpignlen( pushort one, pushort two, uint len )
516 {
517    int cmp = CompareStringW( LOCALE_USER_DEFAULT, NORM_IGNORECASE, one, len, two, len );
518 
519    if ( cmp == CSTR_LESS_THAN )
520       return -1;
521    if ( cmp == CSTR_GREATER_THAN )
522       return 1;
523 
524    return 0;
525 }
526 
527 /*-----------------------------------------------------------------------------
528 *
529 * ID: os_strcmp 23.10.06 0.0.A.
530 * 
531 * Summary: Compare strings
532 *
533 -----------------------------------------------------------------------------*/
534 
535 int   STDCALL os_strcmp( pubyte one, pubyte two )
536 {
537    return os_strcmplen( one, two, -1 );
538 }
539 
540 /*-----------------------------------------------------------------------------
541 *
542 * ID: os_strcmpign 23.10.06 0.0.A.
543 * 
544 * Summary: Compare strings
545 *
546 -----------------------------------------------------------------------------*/
547 
548 int   STDCALL os_strcmpign( pubyte one, pubyte two )
549 {
550    return os_strcmpignlen( one, two, -1 );
551 }
552 
553 /*-----------------------------------------------------------------------------
554 *
555 * ID: os_strcmp 23.10.06 0.0.A.
556 * 
557 * Summary: Compare strings
558 *
559 -----------------------------------------------------------------------------*/
560 
561 int   STDCALL os_ustrcmp( pushort one, pushort two )
562 {
563    return os_ustrcmplen( one, two, -1 );
564 }
565 
566 /*-----------------------------------------------------------------------------
567 *
568 * ID: os_strcmpign 23.10.06 0.0.A.
569 * 
570 * Summary: Compare strings
571 *
572 -----------------------------------------------------------------------------*/
573 
574 int   STDCALL os_ustrcmpign( pushort one, pushort two )
575 {
576    return os_ustrcmpignlen( one, two, -1 );
577 }
578 
579 //--------------------------------------------------------------------------
580 
581 /*
582 pvoid STDCALL os_alloc( uint size )
583 {
584    return VirtualAlloc( NULL, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE );
585 }
586 
587 void STDCALL os_free( pvoid ptr )
588 {
589    VirtualFree( ptr, 0, MEM_RELEASE );
590 }
591 */ 
592 //--------------------------------------------------------------------------
593