home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / cprog / actlib12.zip / TOOLS.H < prev    next >
C/C++ Source or Header  |  1993-03-31  |  18KB  |  713 lines

  1. /*  Copyright (C) 1993   Marc Stern  (internet: stern@mble.philips.be)  */
  2.  
  3. #ifndef __Tools_H
  4. #define __Tools_H
  5.  
  6. #include "c2cpp.h"
  7. #include <stdio.h>
  8. #include <io.h>
  9. #include <dos.h>
  10.  
  11.  
  12. /* for Microsoft (or other than Borland) */
  13.  
  14. #ifndef MK_FP
  15. #define MK_FP( seg, ofs )   (void far *)( (unsigned long)((void far *)(seg)) + (unsigned long)((void near *)(ofs)) )
  16.  
  17. struct  fcb {
  18.     char    fcb_drive;      /* 0 = default, 1 = A, 2 = B */
  19.     char    fcb_name[8];    /* File name */
  20.     char    fcb_ext[3];     /* File extension */
  21.     short   fcb_curblk;     /* Current block number */
  22.     short   fcb_recsize;    /* Logical record size in bytes */
  23.     long    fcb_filsize;    /* File size in bytes */
  24.     short   fcb_date;       /* Date file was last written */
  25.     char    fcb_resv[10];   /* Reserved for DOS */
  26.     char    fcb_currec;     /* Current record in block */
  27.     long    fcb_random;     /* Random record number */
  28. };
  29.  
  30. struct  xfcb    {
  31.     char        xfcb_flag;    /* Contains 0xff to indicate xfcb */
  32.     char        xfcb_resv[5];  /* Reserved for DOS */
  33.     char        xfcb_attr;     /* Search attribute */
  34.     struct  fcb xfcb_fcb;      /* The standard fcb */
  35. };
  36.  
  37. #endif
  38.  
  39.  
  40.             
  41. typedef unsigned char BYTE;
  42.  
  43.  
  44. /***
  45.  *  Function    :  HIBYTE/LOWBYTE
  46.  *
  47.  *  Description :  Extract high/low byte of an integer
  48.  *
  49.  *  Parameters  :  in   int x
  50.  *
  51.  *  Side-effects:  Macro, so be careful.
  52.  *
  53.  *  Return      :  none
  54.  ***/
  55.  
  56. #define HIBYTE( x ) ( (unsigned int)(x) >> 8 )
  57. #define LOWBYTE( x) ( (unsigned char)(x) )
  58.  
  59.  
  60. /***
  61.  *  Function    :  CAST
  62.  *
  63.  *  Description :  Cast any object to any type.
  64.  *
  65.  *  Parameters  :  in     new_type      any type
  66.  *                 in     object        any variable
  67.  *
  68.  *  Side-effects:  Macro, so be careful.
  69.  *
  70.  *  Return      :  none
  71.  ***/
  72.  
  73. #define CAST( new_type, object ) ( *((new_type *)&object) )
  74.      
  75.  
  76. /***
  77.  *  Function    :  SWAP
  78.  *
  79.  *  Description :  Swap two arguments of any type.
  80.  *
  81.  *  Parameters  :  in/out   x      any type argument
  82.  *                 in/out   y      any type argument
  83.  *
  84.  *  Side-effects:  Macro, so be careful.
  85.  *
  86.  *  Return      :  none
  87.  ***/
  88.  
  89. #define SWAP( x, y )        ( (x) ^= (y) ^= (x) ^= (y) )
  90.  
  91.  
  92. /***
  93.  *  Function    :  fileexist
  94.  *
  95.  *  Description :  Test if a filename exist
  96.  *
  97.  *  Parameters  :  in   char *filename
  98.  *
  99.  *  Return      :  0 or 1
  100.  ***/
  101.  
  102. #define fileexist( filename )   ( ! access(filename, 0) )
  103.  
  104.  
  105. /***
  106.  *  Function    :  ICA_get/put
  107.  *
  108.  *  Description :  Intra-application Communications Area get/put
  109.  *                 a 16 bytes data
  110.  *
  111.  *  Parameters  :  in   void *ptr   pointer to a 16 bytes data
  112.  *
  113.  *  Return      :  none
  114.  ***/
  115.  
  116. #define ICA_get( ptr )   ( memcpy(ptr, MK_FP(0x0040, 0x00F0), 16) )
  117. #define ICA_put( ptr )   ( memcpy(MK_FP(0x0040, 0x00F0), ptr, 16) )
  118.  
  119.  
  120.  
  121. /***
  122.  *  Function    :  getkey
  123.  *
  124.  *  Description :  Return a 2-bytes key pressed.
  125.  *
  126.  *  Parameters  :  none
  127.  *
  128.  *  Decisions   :  extended characters are added to 256.
  129.  *
  130.  *  Return code :  code of key pressed.
  131.  *
  132.  *  OS/Compiler :  MS-DOS
  133.  ***/
  134.  
  135. EXTERN int getkey( void );
  136.  
  137.  
  138. /***
  139.  *  Function    :  ungetkey
  140.  *
  141.  *  Description :  Puts a 2-bytes key into the keyboard buffer
  142.  *
  143.  *  Parameters  :  in    int key           key to be buffered
  144.  *
  145.  *  Decisions   :  extended characters are added to 256.
  146.  *
  147.  *  Return code :  none
  148.  *
  149.  *  Warning     :  May not work if a keyboard buffer extender is used.
  150.  *                 Does not handle the case of buffer full.
  151.  *
  152.  *  OS/Compiler :  MS-DOS
  153.  ***/
  154.  
  155. EXTERN void ungetkey( int key );
  156.  
  157.  
  158.  
  159. /***
  160.  *  Function    :  fnreduce
  161.  *
  162.  *  Description :  Transforms a filename into a full reduced filename.
  163.  *
  164.  *  Parameters  :  in/out   char *filename    input/reduced filename
  165.  *
  166.  *  Decisions   :  Translate into uppercase
  167.  *                  '/' are tranlated into '\\'
  168.  *
  169.  *  Return      :  pointer to result
  170.  *
  171.  *  OS/Compiler :  MS-DOS
  172.  ***/
  173.  
  174. EXTERN char *fnreduce( char *filename );
  175.  
  176.  
  177. /***
  178.  *  Function    :  getpgmpath
  179.  *
  180.  *  Description :  Transforms a filename into a full pathname
  181.  *                 relative to program directory.
  182.  *
  183.  *  Parameters  :  in/out   char *filename    input filename
  184.  *
  185.  *  Return      :  pointer to filename    
  186.  *
  187.  *  Precond     :  Global variable extern char **_argv must be defined
  188.  *                 and assign to argv in main( int argc, char *argv[] )
  189.  *                 Built-in in Borland
  190.  *                 Built-in in Microsoft (uses _pgmptr)
  191.  *
  192.  *  OS/Compiler :  MS-DOS version >= 3.0
  193.  ***/
  194.  
  195. EXTERN char *getpgmpath( char *filename );
  196.  
  197.  
  198. /***
  199.  *  Function    :  gettmppath
  200.  *
  201.  *  Description :  Transforms a filename into a full pathname
  202.  *                 relative to temporary directory.
  203.  *
  204.  *  Decisions   :  Temporary directory is first searched
  205.  *                 into environment variable 'TEMP',
  206.  *                 after into environment variable 'TMP'.
  207.  *                 If none are defined, program directory
  208.  *                 is assumed.
  209.  *
  210.  *  Parameters  :  in/out  char *filename    filename
  211.  *
  212.  *  Return      :  pointer to filename
  213.  *
  214.  *  Precond     :  Global variable extern char **_argv must be defined
  215.  *                 and assign to argv in main( int argc, char *argv[] )
  216.  *                 Built-in in Borland
  217.  *
  218.  *  OS/Compiler :  MS-DOS version >= 3.0
  219.  ***/
  220.  
  221. EXTERN char *gettmppath( char *filename );
  222.  
  223.  
  224. /***
  225.  *  Function    :  truename
  226.  *
  227.  *  Description :  Transforms a filename into a full reduced filename.
  228.  *                 Resolves all SUBST's, JOIN's, network names,...
  229.  *
  230.  *  Parameters  :  in/out   char *filename    input/reduced filename
  231.  *
  232.  *  Warning     :   - Valid only for DOS >= 2.0
  233.  *                  - Trailing '\' is not removed
  234.  *
  235.  *  Return      :  0      OK
  236.  *                 2  Invalid syntax
  237.  *                 3  Invalid drive or path
  238.  *
  239.  *  OS/Compiler :  MS-DOS version >= 2.0
  240.  ***/
  241.  
  242. EXTERN int truename( char *filename );
  243.  
  244.  
  245. /***
  246.  *  Function    :  exthandles
  247.  *
  248.  *  Description :  Allows MS-DOS program to open 255 handles
  249.  *
  250.  *  Parameters  :  none
  251.  *
  252.  *  Return      :  none
  253.  *
  254.  *  OS/Compiler :  MS-DOS version >= 3.30
  255.  ***/
  256.  
  257. EXTERN void exthandles( void );
  258.  
  259.  
  260.  
  261. /***
  262.  *  Function    :  getdensity
  263.  *
  264.  *  Description :  Return the density of a floppy drive.
  265.  *
  266.  *  Parameters  :  in    int drive         0 = A:, 1 = B:
  267.  *
  268.  *  Return      :  density in Kb (360, 720, 1200, 1440)
  269.  *                 0 if invalid drive or drive not ready
  270.  *
  271.  *  OS/Compiler :  MS-DOS
  272.  ***/
  273.  
  274. EXTERN int getdensity( int drive );
  275.  
  276.  
  277. /***
  278.  *
  279.  *  Function    :  is_drive_ready
  280.  *
  281.  *  Description :  Return if a floppy drive is ready or not.
  282.  *
  283.  *  Parameters  :  in    int drive         0 = A:, 1 = B:, ...
  284.  *
  285.  *  Return      :  0 if not ready (or invalid)
  286.  *                 1 if ready
  287.  *
  288.  *  OS/Compiler :  MS-DOS
  289.  ***/
  290.  
  291. EXTERN int is_drive_ready( int drive );
  292.  
  293.  
  294.  
  295. /***
  296.  *
  297.  *  Function    :  geterrorlevel
  298.  *
  299.  *  Description :  Return errorlevel code from previous command executed
  300.  *                 by top COMMAND.COM
  301.  *
  302.  *  Parameters  :  none
  303.  *
  304.  *  Return      :  errorlevel
  305.  *                 -1 If MS-DOS version not supported
  306.  *
  307.  *  Warning     :  This function is only valid with COMMAND.COM from
  308.  *                 MS-DOS 3.20, 3.21, 3.30, 4.0, 4.01, 5.0 & NDOS 6.0.
  309.  */
  310.  
  311. EXTERN int geterrorlevel( void );
  312.  
  313.  
  314.  
  315. /***
  316.  *  Function    :  getdiskfree
  317.  *
  318.  *  Description :  Return the number of bytes free of a disk.
  319.  *
  320.  *  Parameters  :  in    int disk         0 = A:, 1 = B:, ...
  321.  *
  322.  *  Return      :  number of bytes free
  323.  *                 -1 if invalid drive
  324.  *
  325.  *  OS/Compiler :  MS-DOS
  326.  ***/
  327.  
  328. EXTERN long getdiskfree( int disk );
  329.  
  330.  
  331. /***
  332.  *  Function    :  setcwd
  333.  *
  334.  *  Description :  Change to specified directory accross disks
  335.  *
  336.  *  Parameters  :  in    char *dir    input directory name
  337.  *
  338.  *  Return      :  0      OK
  339.  *                 other  Invalid syntax, drive or path
  340.  *
  341.  *  OS/Compiler :  MS-DOS
  342.  ***/
  343.  
  344. EXTERN int setcwd( const char *dir );
  345.  
  346.  
  347. /***
  348.  *  Function    :  getsetup
  349.  *
  350.  *  Description :  Read paramaters in setup file.
  351.  *
  352.  *  Parameters  :  in    FILE *file        input file
  353.  *                 in    char *format      "keyword1=format keyword2=format"
  354.  *                 out   pointer list
  355.  *
  356.  *  Decisions   :  Expected format is a list of lines of the form
  357.  *
  358.  *                                 <keyword>=<value>
  359.  *
  360.  *                 - All letters are treated (and returned) as uppercases.
  361.  *                 - Blanks and tabs are skipped.
  362.  *
  363.  *                 - <value> can be enclosed between double quotes
  364.  *                    to preserve lowercases and blanks.
  365.  *
  366.  *                   - 2 consecutive double quotes allow to keep
  367.  *                   a double quote in <value>.
  368.  *
  369.  *                 - A number sign (#) in first column forces the line
  370.  *                   to be ignored.
  371.  *
  372.  *                 - The input formats in parameter 'format' are C standard
  373.  *                   ones ( %d, %s, %f,...).
  374.  *
  375.  *                 - Special format added: "%b" will translate
  376.  *                   True/False, Yes/No, On/Off, 1/0 into integer 1/0
  377.  *
  378.  *                 - Special format added: "%S" will result into copying
  379.  *                   all the input line (with spaces in it if any).
  380.  *                   It will not be processed by scanf-like processing.
  381.  *
  382.  *  Return      :  number of found parameters
  383.  *                 -1     syntax error
  384.  *                 -2     file error
  385.  *
  386.  *  Precond     :  Input file must be opened with read access.
  387.  *
  388.  *  OS/Compiler :  All
  389.  */
  390.  
  391. EXTERN int getsetup( FILE *file, const char *format, ... );
  392.  
  393.  
  394. /***
  395.  *  Function    :  load_options
  396.  *
  397.  *  Description :  Load options from a file.
  398.  *
  399.  *  Parameters  :  out  void * options     ptr to variable containing options
  400.  *                 in   size_t size        size of data to read
  401.  *
  402.  *  Decisions   :  The file has the same name as the program
  403.  *                 with extension .OPT and is searched in
  404.  *                 current directory, then in program directory.
  405.  *                 If option file is not found, options are not modified.
  406.  *
  407.  *  Return      :  number of bytes read
  408.  *                 -1 if file was not found
  409.  *
  410.  *  Precond     :  Global variable extern char **_argv must be defined
  411.  *                 and assign to argv in main( int argc, char *argv[] )
  412.  *                 Built-in in Borland
  413.  *
  414.  *  OS/Compiler :  MS-DOS version >= 3.0
  415.  ***/
  416.  
  417. EXTERN int load_options( void *options, size_t size );
  418.  
  419. /***   To not specify the size   ***/
  420. #define Opt_load( opt )     load_options( (opt), sizeof(*(opt)) )
  421.  
  422.  
  423.  
  424.  
  425. /***
  426.  *  Function    :  save_options
  427.  *
  428.  *  Description :  Save options in a file.
  429.  *
  430.  *  Parameters  :  in  void * options     ptr to variable containing options
  431.  *                 in  size_t size        size of data to read
  432.  *
  433.  *  Decisions   :  If options were previously loaded, this file is used.
  434.  *                 Otherwise, the file has the same name as the program
  435.  *                 with extension .OPT and is located in program directory.
  436.  *
  437.  *  Return      :  number of bytes written
  438.  *                 -1 if file not opened
  439.  *
  440.  *  Precond     :  Global variable extern char **_argv must be defined
  441.  *                 and assign to argv in main( int argc, char *argv[] )
  442.  *                 Built-in in Borland
  443.  *
  444.  *  OS/Compiler :  MS-DOS version >= 3.0
  445.  ***/
  446.  
  447. EXTERN int save_options( const void *options, size_t size );
  448.  
  449. /***   To not specify the size   ***/
  450. #define Opt_save( opt )     save_options( (opt), sizeof(*(opt)) )
  451.  
  452.  
  453. /***
  454.  *
  455.  *  Function    :  load_cfg
  456.  *
  457.  *  Description :  Load configuration from a file.
  458.  *
  459.  *  Parameters :   in   char * name        filename
  460.  *                 out  void * config      ptr to variable containing configuration
  461.  *                 in   size_t size        size of data to read
  462.  *
  463.  *  Decisions   :  The file has the same name as the parameter 'name'
  464.  *                 but with extension .CFG
  465.  *                 If configuration file is not found, config is unchanged.
  466.  *
  467.  *  Return     :   number of bytes read
  468.  *                 -1 if file was not found
  469.  *
  470.  *  OS/Compiler :  MS-DOS
  471.  ***/
  472.  
  473. EXTERN int load_cfg( const char *name, void *config, size_t bsize );
  474.  
  475. /***   To not specify the size   ***/
  476. #define Cfg_load( name, cfg )   load_cfg( (name), (cfg), sizeof(*(cfg)) )
  477.  
  478.  
  479.  
  480. /***
  481.  *
  482.  *  Function    :  save_cfg
  483.  *
  484.  *  Description :  Save a configuration into a file.
  485.  *
  486.  *  Parameters  :  in   char * name        filename
  487.  *                 in   void * config      ptr to variable containing configuration
  488.  *                 in   size_t size        size of data to write
  489.  *
  490.  *  Decisions   :  The file has the same name as the parameter 'name'
  491.  *                 but with extension .CFG
  492.  *
  493.  *  Return      :  number of bytes written
  494.  *                 -1 if file was not found
  495.  *
  496.  *  OS/Compiler :  MS-DOS
  497.  ***/
  498.  
  499. EXTERN int save_cfg( const char *name, const void *config, size_t bsize );
  500.  
  501. /***   To not specify the size   ***/
  502. #define Cfg_save( name, cfg )   save_cfg( (name), (cfg), sizeof(*(cfg)) )
  503.  
  504.  
  505.  
  506. /***
  507.  *  Function    :  filencopy
  508.  *
  509.  *  Description :  Copy n characters from a file on another.
  510.  *
  511.  *  Parameters  :  in   int            target    target file descriptor
  512.  *                 in   int            source    source file descriptor
  513.  *                 in   unsigned long  length    number of bytes to copy
  514.  *
  515.  *  Precond     :  The two files must be opened.
  516.  *
  517.  *  Decisions   :  The copy can stop when eof is reached.
  518.  *
  519.  *  Warning     :  no rewind is performed on either file!!!
  520.  *
  521.  *  Return      :  0 if OK
  522.  *                 errno if set
  523.  *                 -1 otherwise
  524.  *
  525.  *  OS/Compiler :  All
  526.  ***/
  527.  
  528. EXTERN int filencopy( int source, int target, unsigned long length );
  529.  
  530. /* maximum size */
  531. #define filecopy( source, target )    filencopy( source, target, 0xFFFFFFFF )
  532.  
  533.  
  534. /***
  535.  *  Function    :  Filencopy
  536.  *
  537.  *  Description :  Copy n characters from a file on another.
  538.  *
  539.  *  Parameters  :  in   char *         source    source filename
  540.  *                 in   char *         target    target filename
  541.  *                 in   unsigned long  length    number of bytes to copy
  542.  *
  543.  *  Decisions   :  The copy can stop when eof is reached.
  544.  *
  545.  *  Return      :  0 if OK
  546.  *                 errno if set
  547.  *                 -1 otherwise
  548.  *
  549.  *  OS/Compiler :  All
  550.  ***/
  551.  
  552. EXTERN int Filencopy( const char *source, const char *target, unsigned long length );
  553.  
  554. /* maximum size */
  555. #define Filecopy( source, target )    Filencopy( source, target, 0xFFFFFFFF )
  556.  
  557.  
  558.  
  559.  
  560. /***
  561.  *  Function    :  Filecat
  562.  *
  563.  *  Description :  Concatenate two files on another.
  564.  *
  565.  *  Parameters  :  in   char *     newpath     result filename
  566.  *                 in   char *     path1       target filename
  567.  *                 in   char *     path2       source filename
  568.  *
  569.  *  Return      :  0 if OK
  570.  *                 errno if set
  571.  *                 -1 otherwise
  572.  *
  573.  *  OS/Compiler :  All
  574.  ***/
  575.  
  576. EXTERN int Filecat( const char *newpath, const char *path1, const char *path2 );
  577.  
  578.  
  579. /***
  580.  *  Function    :  filetrunc
  581.  *
  582.  *  Description :  Truncate a file to a specified length.
  583.  *
  584.  *  Parameters  :  in   int            fd        file descriptor
  585.  *                 in   unsigned long  length    number of bytes to copy
  586.  *
  587.  *  Precond     :  The file must be opened with writing permission
  588.  *
  589.  *  Warning     :  no rewind is performed on the file!!!
  590.  *
  591.  *  Return      :  0 if OK
  592.  *                 errno if error
  593.  *
  594.  *  OS/Compiler :  All
  595.  ***/
  596.  
  597. EXTERN int filetrunc( int fd, unsigned long length );
  598.  
  599.  
  600.  
  601.  
  602. /***
  603.  *  Function    :  Filetrunc
  604.  *
  605.  *  Description :  Truncate a file to a specified length.
  606.  *
  607.  *  Parameters  :  in   char *         path      file descriptor
  608.  *                 in   unsigned long  length    number of bytes to copy
  609.  *
  610.  *  Return      :  0 if OK
  611.  *                 errno if error
  612.  *
  613.  *  OS/Compiler :  All
  614.  ***/
  615.  
  616. EXTERN int Filetrunc( const char *path, int length );
  617.  
  618.  
  619.  
  620.  
  621. /***
  622.  *  Functions   :  filedel
  623.  *
  624.  *  Description :  Like 'unlink' but allows wildcards
  625.  *
  626.  *  Parameters  :  in   char *  filename   pathname with/without wildcards
  627.  *
  628.  *  Return      :  none
  629.  *
  630.  *  OS/Compiler :  MS-DOS
  631.  ***/
  632.  
  633. EXTERN void filedel( const char *filename );
  634.  
  635.  
  636.  
  637. /***
  638.  *  Function    :  getPasswd
  639.  *
  640.  *  Description :  Input a password (echoes '*')
  641.  *
  642.  *  Parameters  :  in  char *passwd    string to store password
  643.  *
  644.  *  Decisions   :  Echoes '*' in place of characters.
  645.  *                  Backspace and left arrow can be used to erase characters
  646.  *                  Characters must be in the range 32 - 254
  647.  *
  648.  *  Return      :  pointer to password
  649.  *
  650.  *  OS/Compiler :  MS-DOS & Turbo-C
  651.  ***/
  652.  
  653. EXTERN char *getPasswd( char *passwd );
  654.  
  655.  
  656.  
  657. /***
  658.  *  Function    :  vollabel
  659.  *
  660.  *  Description :  Get/Set disk volume label
  661.  *
  662.  *  Parameters  :  in       int drive         0 = A:, 1 = B:, ...
  663.  *                 in/out   char *vol         volume label
  664.  *
  665.  *  Decisions   :  - If *vol = '\0', it is filled with disk volume label.
  666.  *                 - Otherwise, vol is copied onto disk volume label.
  667.  *
  668.  *  Return      :   0 if OK
  669.  *                 -1 if error
  670.  ***/
  671.  
  672. EXTERN int vollabel( int drive, char *vol );
  673.  
  674.  
  675. #if ! defined(__TURBOC__)
  676. /*  void sound ( short frequency, long duration )
  677.  *
  678.  *  Makes the PC speaker emit a sound at `frequency' Herz for approximately
  679.  *  `duration' milliseconds.
  680.  */
  681. EXTERN void sound ( short frequency, long duration );
  682. #endif
  683.  
  684. /***
  685.  *
  686.  *  Function beep :   Emit a beep.
  687.  *
  688.  ***/
  689. EXTERN void beep( void );
  690.  
  691. /***
  692.  *
  693.  *  Function buzzer:   Emit a buzzer sound.
  694.  *
  695.  ***/
  696. EXTERN void buzzer( void );
  697.  
  698.  
  699. /***
  700.  *  Function    :  cpu_type
  701.  *
  702.  *  Description :  returns cpu type (86, 186, 286, 386, 486)
  703.  *
  704.  *  Parameters  :  none
  705.  *
  706.  *  Return      :  86, 186, 286, 386, 486
  707.  ***/
  708.  
  709. EXTERN int far cpu_type( void );
  710.  
  711.  
  712. #endif
  713.