home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR13 / INIFIL.ZIP / EDITINI.CPP next >
C/C++ Source or Header  |  1993-10-11  |  3KB  |  143 lines

  1. /*
  2.     Copyright (c) 1993, BioPhotonics Corporation
  3.                 7300 West Huron River Drive
  4.                 Dexter, MI 48130
  5.                 (313)-426-8299
  6.  
  7.                 ALL RIGHTS RESERVED
  8.  
  9.     filename:    editini.cpp
  10.  
  11.     purpose:    edit any *.ini file.
  12.                 Used as part of many install procedures
  13.  
  14.     instructions for building:
  15.         compile:
  16.         link:
  17.         other:
  18.  
  19.     change log:
  20.         When        Version    Who            Why
  21. */
  22.  
  23. /*
  24.         PRAGMAS
  25. */
  26.  
  27. /*
  28.         INCLUDE FILES
  29. */
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <sys\stat.h>        /* for S_IREAD and S_IWRITE */
  34. #include <fcntl.h>
  35. #include <io.h>
  36. #include <dos.h>
  37. #include <dir.h>
  38.  
  39. #include "inifile.h"
  40.  
  41. /*
  42.         LOCAL DEFINES AND MACROS
  43. */
  44.  
  45. /*
  46.         LOCAL STRUCTURES AND TYPEDEFS
  47. */
  48.  
  49. /*
  50.         FORWARD REFERENCES TO FUNCTIONS DEFINED IN THIS SOURCE FILE
  51. */
  52.  
  53. /*
  54.         EXTERN FUNCTION AND DATA REFERENCES
  55. */
  56.  
  57. /*
  58.         LOCAL DATA  (MAY BE GLOBAL OR STATIC)
  59. */
  60.  
  61. char cwdString[256];
  62. char inBuf[256];
  63.  
  64. /*
  65.     performs editing of a *.ini file.
  66.     does some fancy recognition of a string like '$CWD' as a value
  67.     and substitutes the current working directory
  68. */
  69.  
  70. int main ( int argc, char *argv[] )
  71. {
  72.     int rc;
  73.  
  74.     // you need an exefile name, an ini file name, a section name, and
  75.     // at least a key name.  If that's all, the value will be reported.
  76.     // If more is included on the command line, it will all be passed as
  77.     // parameters to the write call
  78.     if ( argc < 4 ) {
  79.       printf (
  80.         "usage: editini inifilename sectionname keyname [new values...]\n" );
  81.       exit ( 30 );
  82.     }
  83.  
  84.     FILE *pIniFile = fopen ( argv[1], "rt" );
  85.     if ( !pIniFile ) {
  86.         printf ( "editini: couldn't open ini file '%s'\n", argv[1] );
  87.       printf (
  88.         "usage: editini inifilename sectionname keyname [new values...]\n" );
  89.       exit ( 30 );
  90.       printf (
  91.         "usage: editini inifilename sectionname keyname [new values...]\n" );
  92.       exit ( 30 );
  93.     }
  94.     fclose ( pIniFile );
  95.  
  96.     char *pRC;
  97.     int    cwdLen;
  98.  
  99.     // get the current working directory
  100.     pRC = getcwd ( cwdString, sizeof ( cwdString ) );
  101.     if ( !pRC ) {
  102.         // couldn't get a current working directory
  103.         cwdString[0] = '\0';
  104.     }
  105.  
  106.     // count its length
  107.     cwdLen = strlen ( cwdString );
  108.  
  109.     // if the cwd string is not null, then make sure it has a backslash
  110.     // at the end
  111.     if ( cwdLen  && cwdString[ cwdLen ] != '\\' ) {
  112.         strcat ( cwdString, "\\" );
  113.     }
  114.  
  115.     char rb[256];
  116.     GetPrivateProfileString ( argv[2], argv[3], "not found", rb, 256, argv[1]);
  117.     printf ( "INI file %s, section [%s]:\n", argv[1], argv[2] );
  118.     printf ( "Current value for key '%s' is '%s'\n", argv[3], rb );
  119.     
  120.     // are there new values to write???
  121.     if ( argc > 4 ) {
  122.         // first, concat all the args after 4 together
  123.         int    numArgs = 4;
  124.         char otherArgs[256];
  125.         otherArgs[0] = '\0';
  126.         while (numArgs < argc) {
  127.             // this is where numArgs is incremented
  128.             if ( numArgs > 4 ) {
  129.                 strcat ( otherArgs, " " );
  130.             }
  131.             if ( !stricmp(argv[numArgs],"$CWD") ) {
  132.                 strcat ( otherArgs, cwdString );
  133.             }
  134.             else {
  135.                 strcat ( otherArgs, argv[numArgs++] );
  136.             }
  137.         }
  138.         rc = WritePrivateProfileString(argv[2], argv[3], otherArgs, argv[1]);
  139.     }
  140.     // all done, exit
  141.     return 0;
  142. }
  143.