home *** CD-ROM | disk | FTP | other *** search
/ swCHIP 1991 January / swCHIP_95-1.bin / utility / grpini / setini16 / setini.pas < prev   
Pascal/Delphi Source File  |  1995-12-09  |  7KB  |  254 lines

  1.  
  2. {$I-}   {I/O checking must be off.}
  3.  
  4. program setini;
  5.  
  6. uses dos,strings;
  7.  
  8. {  Program to make automated modifications to Windows style .INI files.  }
  9.  
  10. const {initialised variables}
  11.   status  : (looking, in_section, done) = looking;
  12.   oldline : string = '';                              {line to search for}
  13.   newline : string = '';                              {line to substitute, or
  14.                                                          blank for delete}
  15. var
  16.  
  17.   filename      : string;                             {file name to process}
  18.   section       : string;                             {file section to find}
  19.   keyname       : string;                             {keyname to find}
  20.   value         : string;                             {new value}
  21.   arg           : string;                             {may be /add or /del}
  22.   thisline      : string;                             {current line}
  23.   tempfile      : pathstr;                            {temp file name}
  24.   dir           : dirstr;                             {components of temp}
  25.   name          : namestr;
  26.   ext           : extstr;
  27.   inputfile, outputfile : text;
  28.  
  29.  
  30. procedure syntaxerror;
  31.  
  32.   begin
  33.     writeln('SETINI Windows Configuration Changer v1.6b - (C) Cardiology Dept, RMH.');
  34.     writeln('  by Peter Summers <peter@cardiology.medrmh.unimelb.edu.au> - 8 July 95');
  35.     writeln;
  36.     writeln('SYNTAX: SETINI <file> <section> <keyname> <value> <option>');
  37.     writeln;
  38.     writeln('  eg. SETINI system 386Enh TimerCriticalSection 10000');
  39.     writeln('      SETINI win.ini "Microsoft Word 2.0" DOC-path h:\');
  40.     writeln('      SETINI d:system.ini 386Enh device *vtd /del');
  41.     writeln('      SETINI c:\win31\system.ini 386Enh device vtda.386 /add');
  42.     writeln('      SETINI c:\config.sys "" files 50');
  43.     halt(1);
  44.   end;
  45.  
  46.  
  47. procedure raiserror(exitcode:word; message:string);
  48.  
  49. begin
  50.   writeln(message);
  51.   halt(exitcode);
  52. end;
  53.  
  54.  
  55. function nextarg:string;
  56.  
  57. {retrieves the next arguement from command line, using quotes for delimiting}
  58.  
  59. const param : integer = 1;            {use for parsing command line}
  60. var   arg : string;
  61.  
  62. begin
  63.   arg := paramstr(param);
  64.   param := param + 1;
  65.   if arg[1] = '"' then
  66.     begin
  67.       delete(arg,1,1);                        {delete starting quote}
  68.       while (arg[length(arg)] <> '"') do
  69.         begin
  70.         if param > paramcount then syntaxerror;
  71.         arg := arg + ' ' + paramstr(param);
  72.         param := param + 1;
  73.       end;
  74.       delete(arg,length(arg),1);              {delete ending quote}
  75.     end;
  76.   nextarg := arg;
  77. end;
  78.  
  79.  
  80. function match(string1,string2:string):boolean;
  81.  
  82. var i,strlen : integer;
  83.  
  84. begin
  85.   strlen := length(string1);
  86.   if (length(string2) < strlen) or
  87.     ((length(string2) > strlen) and (string1[strlen] <> '=')) then
  88.       begin
  89.         match := false;
  90.         exit;
  91.       end;
  92.  
  93.   for i:=1 to strlen do
  94.     if upcase(string1[i]) <> upcase(string2[i]) then
  95.       begin
  96.         match:=false;
  97.         exit;
  98.       end;
  99.  
  100.   match:=true;
  101. end;
  102.  
  103.  
  104. procedure writeout(line:string);
  105.  
  106. {write a line to output file and check the result}
  107.  
  108. begin
  109.   writeln(outputfile,line);
  110.   if IOresult<>0 then raiserror(5,'Can''t write to file '+tempfile+'.');
  111. end;
  112.  
  113.  
  114. begin {program}
  115.  
  116.   if paramcount<4 then syntaxerror;
  117.  
  118.   filename:=nextarg;
  119.   fsplit(filename,dir,name,ext);
  120.  
  121.   if dir = '' then
  122.     dir := 'c:\windows\'
  123.   else if (length(dir)=2) and (dir[2]=':') then
  124.     dir := dir + '\windows\';
  125.  
  126.   if ext = '' then ext := '.ini';
  127.  
  128.   filename := dir + name + ext;
  129.   tempfile := dir + name + '.$$$';
  130.  
  131.   assign(inputfile, filename);               {open input file}
  132.   reset(inputfile);
  133.   if IOresult<>0 then
  134.     raiserror(2,'Can''t open for reading file '+filename+'.');
  135.  
  136.   section := nextarg;
  137.   if section = '' then begin
  138.     status := in_section;
  139.     section := 'preface';
  140.   end else
  141.     if section[1] <> '[' then section := '[' + section + ']';
  142.  
  143.   keyname := nextarg;
  144.   if keyname = '' then syntaxerror;
  145.  
  146.   value := nextarg;
  147.  
  148.   repeat
  149.     arg := nextarg;
  150.  
  151.     if arg = '' then begin
  152.       oldline := keyname+'=';
  153.       newline := keyname+'='+value;
  154.     end else if match('/add',arg) then begin
  155.       oldline := keyname+'='+value;
  156.       newline := keyname+'='+value;
  157.     end else if match('/del',arg) then begin
  158.       oldline := keyname+'='+value;
  159.       newline := '';
  160.     end else value := value + ' ' + arg;
  161.  
  162.   until oldline <> '';
  163.  
  164.   assign(outputfile, tempfile);
  165.   rewrite(outputfile);
  166.   if IOresult<>0 then
  167.     raiserror(3,'Can''t open for writing file '+tempfile+'.');
  168.  
  169.   while(not eof(inputfile)) do
  170.     begin
  171.       readln(inputfile,thisline);               {read a line from input file}
  172.       if IOresult<>0 then
  173.         raiserror(4,'Can''t read from file '+filename+'.');
  174.  
  175.       { strip starting and trailing spaces }
  176.       while (length(thisline)>0) and (thisline[1] = ' ') do
  177.         delete(thisline,1,1);
  178.       while (length(thisline)>0) and (thisline[length(thisline)] = ' ') do
  179.         delete(thisline,length(thisline),1);
  180.  
  181.       if thisline[1] = '[' then                 {we've started a new section}
  182.         case status of
  183.           looking:
  184.             if match(section,thisline) then
  185.               status := in_section;
  186.           in_section:
  187.             begin
  188.               if newline<>'' then begin
  189.                 writeout(newline);
  190.                 writeln('"'+newline+'" added to '+section+' section of '
  191.                   +filename+'.');
  192.               end else
  193.                 writeln('"'+oldline+'" not found in '+section+' section of '
  194.                   +filename+'.');
  195.               status:=done;
  196.             end;
  197.         end;
  198.  
  199.       if (status = in_section) and match(oldline,thisline) then {do our stuff}
  200.         begin
  201.           if newline='' then
  202.             writeln('"'+oldline+'" deleted from '+section+' section of '
  203.               +filename+'.')
  204.           else
  205.             begin
  206.               writeout(newline);
  207.               writeln('"'+newline+'" set in '+section+' section of '
  208.                 +filename+'.');
  209.             end;
  210.           status:=done;
  211.         end
  212.       else if thisline <> '' then          {otherwise just write out the line}
  213.         begin
  214.           if thisline[1] = '[' then writeout('');
  215.           writeout(thisline);
  216.         end;
  217.     end;                                            {end of main loop}
  218.  
  219.   if newline <> '' then
  220.     case status of
  221.       looking:
  222.         begin
  223.           writeout('');
  224.           writeout(section);
  225.           writeout(newline);
  226.           writeln('"'+newline+'" added to new '+section+' section of '
  227.             +filename+'.');
  228.         end;
  229.       in_section:
  230.         begin
  231.           writeout(newline);
  232.           writeln('"'+newline+'" added to '+section+' section of '
  233.             +filename+'.');
  234.         end;
  235.     end else {newline=''}
  236.       if status <> done then
  237.         writeln('"'+oldline+'" not found in '+section+' section of '
  238.           +filename+'.');
  239.  
  240.   close(inputfile);
  241.   close(outputfile);
  242.  
  243.   erase(inputfile);
  244.  
  245.   if IOResult<>0 then
  246.     raiserror(6,'Couldn''t delete old version of '+filename+'.');
  247.  
  248.   rename(outputfile,filename);
  249.  
  250.   if IOResult<>0 then
  251.     raiserror(7,'Couldn''t rename temporary file '+tempfile+'.');
  252.  
  253. end.
  254.