home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / CFGED1B.ZIP / CFGEDSRC.ZIP / CFGFILE.CC < prev    next >
C/C++ Source or Header  |  1992-08-29  |  3KB  |  133 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* cfgfile.cc
  3.  *
  4.  * Implementation of class CfgFile
  5.  *
  6.  * this class allows access to the Configuration File CONFIG.SYS
  7.  *
  8.  * Language        : C++
  9.  * Operating System: OS/2 V2.0 and higher
  10.  * Compiler        : GNU GCC V2.1 and higher
  11.  *
  12.  * $Id: cfgfile.cc,v 1.2 1992/08/29 16:12:00 gruen Exp $
  13.  * $Log: cfgfile.cc,v $
  14. // Revision 1.2  1992/08/29  16:12:00  gruen
  15. // Fixed several bugs. This will become the official beta-release.
  16. //
  17. // Revision 1.1  1992/07/17  00:23:29  gruen
  18. // Initial revision
  19. //
  20.  *
  21.  * Copyright (c) 1992 Lutz Grueneberg
  22.  *
  23.  * This library is free software; you can redistribute it and/or modify
  24.  * it under the terms of the GNU Library General Public License as
  25.  * published by the Free Software Foundation; either version 2 of the
  26.  * License, or (at your option) any later version.  This library is
  27.  * distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  28.  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  29.  * A PARTICULAR PURPOSE.  See the GNU Library General Public License for
  30.  * more details. You should have received a copy of the GNU Library
  31.  * General Public License along with this library; if not, write to the
  32.  * Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  33.  */
  34. #define INCL_PM     
  35. #include <os2.h>
  36. #include <stdio.h>
  37. #include <unistd.h>
  38. #include <pmviews/pmviews.h>
  39. #include <Regx.h>
  40. #include <Strng.h>
  41. #include "stream.h"
  42. #include "stdio.h"
  43. #include "cfgfile.h"
  44.  
  45. void CfgFile::clear()
  46. {
  47.   //for(Pix i = strList.first(); i != 0; strList.next(i)) {
  48.   //  delete &strList(i);
  49.   //}
  50.   strList.clear();
  51. }
  52.  
  53. int  CfgFile::read( char *szFileName)
  54. {
  55.   FILE   *fp;
  56.   char   szBuf[512];
  57.   String *pstrLine;
  58.  
  59.   if((fp=fopen( szFileName, "rt"))==NULL) {
  60.     return 0;
  61.   }
  62.   while( fgets( szBuf, 512, fp)) {   
  63.     if( strlen( szBuf) && szBuf[strlen(szBuf)-1] == '\n')
  64.       szBuf[strlen(szBuf)-1] = 0;
  65.     pstrLine = new String( szBuf);
  66.     strList.append( *pstrLine);
  67.   }
  68.   fclose( fp);
  69.   return 1;
  70. }
  71.     
  72.  
  73. int  CfgFile::save( char *szFileName, char *szOldFileName)
  74. {
  75.   FILE   *fp;
  76.   String *pstrLine;
  77.  
  78.   if( !access( szOldFileName, 0)) unlink( szOldFileName);
  79.   rename( szFileName, szOldFileName);
  80.  
  81.   if((fp=fopen( szFileName, "wt"))==NULL) {
  82.     return 0;
  83.   }
  84.   for(Pix i = strList.first();
  85.       i != 0;
  86.       strList.next(i)) {
  87.     fputs( (char*)strList(i), fp);
  88.     fputs( "\n", fp);
  89.   }
  90.   fclose( fp);
  91.   return 1;
  92. }
  93.  
  94. void CfgFile::printToListBox( ListBox  *plb)
  95. {
  96.   plb->enableUpdate( FALSE);    // disable update for performance reasons
  97.   plb->deleteAll();
  98.   for(Pix i = strList.first();
  99.       i != 0;
  100.       strList.next(i)) 
  101.     plb->insertItem( LIT_END, (char*)(strList(i)));
  102.   plb->show( TRUE);        // enable update and force redraw
  103. }
  104.  
  105. String* CfgFile::retrieve( SHORT iItem)
  106.   SHORT s;
  107.   Pix   i;
  108.  
  109.   for(s=0, i=strList.first(); s != iItem; s++)
  110.     strList.next(i);
  111.   return &strList(i);
  112. }
  113.  
  114. Pix CfgFile::retrievePix( SHORT iItem)
  115. {
  116.   SHORT s;
  117.   Pix   i;
  118.  
  119.   for(s=0, i=strList.first(); s != iItem; s++)
  120.     strList.next(i);
  121.   return (i);
  122. }
  123.  
  124. void CfgFile::list( void)
  125. {
  126.   for(Pix i = strList.first();
  127.       i != 0;
  128.       strList.next(i))
  129.     cout << strList(i) << '\n';
  130. }
  131.  
  132.