sgrmfile.cpp

Go to the documentation of this file.
00001 // $Id: sgrmfile.cpp 1282 2006-06-09 09:46:49Z alex $
00002 /* @@tag:xara-cn@@ DO NOT MODIFY THIS LINE
00003 ================================XARAHEADERSTART===========================
00004  
00005                Xara LX, a vector drawing and manipulation program.
00006                     Copyright (C) 1993-2006 Xara Group Ltd.
00007        Copyright on certain contributions may be held in joint with their
00008               respective authors. See AUTHORS file for details.
00009 
00010 LICENSE TO USE AND MODIFY SOFTWARE
00011 ----------------------------------
00012 
00013 This file is part of Xara LX.
00014 
00015 Xara LX is free software; you can redistribute it and/or modify it
00016 under the terms of the GNU General Public License version 2 as published
00017 by the Free Software Foundation.
00018 
00019 Xara LX and its component source files are distributed in the hope
00020 that it will be useful, but WITHOUT ANY WARRANTY; without even the
00021 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00022 See the GNU General Public License for more details.
00023 
00024 You should have received a copy of the GNU General Public License along
00025 with Xara LX (see the file GPL in the root directory of the
00026 distribution); if not, write to the Free Software Foundation, Inc., 51
00027 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00028 
00029 
00030 ADDITIONAL RIGHTS
00031 -----------------
00032 
00033 Conditional upon your continuing compliance with the GNU General Public
00034 License described above, Xara Group Ltd grants to you certain additional
00035 rights. 
00036 
00037 The additional rights are to use, modify, and distribute the software
00038 together with the wxWidgets library, the wxXtra library, and the "CDraw"
00039 library and any other such library that any version of Xara LX relased
00040 by Xara Group Ltd requires in order to compile and execute, including
00041 the static linking of that library to XaraLX. In the case of the
00042 "CDraw" library, you may satisfy obligation under the GNU General Public
00043 License to provide source code by providing a binary copy of the library
00044 concerned and a copy of the license accompanying it.
00045 
00046 Nothing in this section restricts any of the rights you have under
00047 the GNU General Public License.
00048 
00049 
00050 SCOPE OF LICENSE
00051 ----------------
00052 
00053 This license applies to this program (XaraLX) and its constituent source
00054 files only, and does not necessarily apply to other Xara products which may
00055 in part share the same code base, and are subject to their own licensing
00056 terms.
00057 
00058 This license does not apply to files in the wxXtra directory, which
00059 are built into a separate library, and are subject to the wxWindows
00060 license contained within that directory in the file "WXXTRA-LICENSE".
00061 
00062 This license does not apply to the binary libraries (if any) within
00063 the "libs" directory, which are subject to a separate license contained
00064 within that directory in the file "LIBS-LICENSE".
00065 
00066 
00067 ARRANGEMENTS FOR CONTRIBUTION OF MODIFICATIONS
00068 ----------------------------------------------
00069 
00070 Subject to the terms of the GNU Public License (see above), you are
00071 free to do whatever you like with your modifications. However, you may
00072 (at your option) wish contribute them to Xara's source tree. You can
00073 find details of how to do this at:
00074   http://www.xaraxtreme.org/developers/
00075 
00076 Prior to contributing your modifications, you will need to complete our
00077 contributor agreement. This can be found at:
00078   http://www.xaraxtreme.org/developers/contribute/
00079 
00080 Please note that Xara will not accept modifications which modify any of
00081 the text between the start and end of this header (marked
00082 XARAHEADERSTART and XARAHEADEREND).
00083 
00084 
00085 MARKS
00086 -----
00087 
00088 Xara, Xara LX, Xara X, Xara X/Xtreme, Xara Xtreme, the Xtreme and Xara
00089 designs are registered or unregistered trademarks, design-marks, and/or
00090 service marks of Xara Group Ltd. All rights in these marks are reserved.
00091 
00092 
00093       Xara Group Ltd, Gaddesden Place, Hemel Hempstead, HP2 6EX, UK.
00094                         http://www.xara.com/
00095 
00096 =================================XARAHEADEREND============================
00097  */
00098 // SGRMfile.cpp - Gallery Resource Management file routines
00099 
00100 #include "camtypes.h"
00101 #include "sgrmfile.h"
00102 
00103 #include "product.h"    // for PRODUCT_BASENAME
00104 #include "sgliboil.h"
00105 //#include "pathname.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00106 //#include "sgscan.h"       // Generate code with hourglass... - in camtypes.h [AUTOMATICALLY REMOVED]
00107 //#include "nev.h"
00108 #include "progress.h"
00109 //#include "list.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00110 
00111   
00112 // Implement the dynamic class bits...
00113 CC_IMPLEMENT_DYNCREATE(LibSettingsListItem, ListItem)
00114 CC_IMPLEMENT_DYNCREATE(LibSettings, CCObject)
00115 
00116 // This line mustn't go before any CC_IMPLEMENT_... macros
00117 #define new CAM_DEBUG_NEW
00118 
00119 /********************************************************************************************
00120 
00121 >   LibSettingsListItem::LibSettingsListItem(String_256 *Entry)
00122 
00123     Author:     Richard_Millican (Xara Group Ltd) <camelotdev@xara.com>
00124     Created:    25/1/96
00125     Inputs:     Entry   - The Entry
00126     Purpose:    A GRM file line list entry
00127 
00128 ********************************************************************************************/
00129 
00130 LibSettingsListItem::LibSettingsListItem(String_256 *Entry)
00131 {
00132     if(Entry != NULL)
00133         Line = new String_256(*Entry);
00134     else
00135         Line = NULL;
00136 }
00137 
00138 /********************************************************************************************
00139 
00140 >   LibSettingsListItem::LibSettingsListItem()
00141 
00142     Author:     Richard_Millican (Xara Group Ltd) <camelotdev@xara.com>
00143     Created:    25/1/96
00144     Purpose:    GRM file line list entry default contructor
00145 
00146 ********************************************************************************************/
00147 
00148 LibSettingsListItem::LibSettingsListItem()
00149 {
00150     ERROR3("LibSettingsListItem::LibSettingsListItem - call the other constructor");
00151     Line = NULL;
00152 }
00153 
00154 /********************************************************************************************
00155 
00156 >   LibSettingsListItem::~LibSettingsListItem()
00157 
00158     Author:     Richard_Millican (Xara Group Ltd) <camelotdev@xara.com>
00159     Created:    25/1/96
00160     Purpose:    GRM file line list entry destructor
00161 
00162 ********************************************************************************************/
00163 
00164 LibSettingsListItem::~LibSettingsListItem()
00165 {
00166     if(Line != NULL)
00167     {
00168         delete Line;
00169         Line = NULL;
00170     }
00171 }
00172 
00173 
00174 
00175 
00176 /********************************************************************************************
00177 
00178 >   LibSettings::LibrarySettings()
00179 
00180     Author:     Richard_Millican (Xara Group Ltd) <camelotdev@xara.com>
00181     Created:    18/5/95
00182     Inputs:
00183     Outputs:
00184     Returns:
00185     Purpose:    The Gallery Resource Management code...
00186 
00187     Notes:      Currently this code uses the GRM file to remember which groups were in which
00188                 libraries and in which order when quitting the program so than on loading next
00189                 time we can reflect the old status in the library...
00190 
00191 ********************************************************************************************/
00192 
00193 LibSettings::LibSettings()
00194 {
00195 }
00196 
00197 /********************************************************************************************
00198 
00199 >   LibSettings::~LibrarySettings()
00200 
00201     Author:     Richard_Millican (Xara Group Ltd) <camelotdev@xara.com>
00202     Created:    18/5/95
00203     Inputs:
00204     Outputs:
00205     Returns:
00206     Purpose:    Destructor
00207 
00208 ********************************************************************************************/
00209 
00210 LibSettings::~LibSettings()
00211 {
00212 }
00213 
00214 
00215 /********************************************************************************************
00216 
00217 >   static BOOL LibSettings::Init(void)
00218 
00219     Author:     Richard_Millican (Xara Group Ltd) <camelotdev@xara.com>
00220     Created:    18/5/95
00221     Inputs:
00222     Outputs:
00223     Returns:
00224     Purpose:    Value returning initialisation routine...
00225 
00226 ********************************************************************************************/
00227 
00228 BOOL LibSettings::Init(void)
00229 {
00230     return TRUE;
00231 }
00232 
00233 
00234 /********************************************************************************************
00235 
00236 >   static BOOL LibSettings::GetEntry(String_256 *Section, INT32 Line, String_256 *Entry,
00237             FilePos *Pos = NULL, String_256 * pIniFileString = NULL)
00238 
00239     Author:     Richard_Millican (Xara Group Ltd) <camelotdev@xara.com>
00240     Created:    18/5/95
00241     Inputs:     Section - Text to match with the section names
00242                 Line    - Line offset in the section to return
00243                 pIniFileString  set to NULL if using default gallery ini file (Default)
00244                                 or to the leafname of the ini file to use
00245     Outputs:    Entry   - The line associated with the two input params
00246                 Pos     - The FilePos after the item was found
00247     Returns:    TRUE if this was successful. FALSE if we hit the next section, or EOF
00248                 or if the file doesn't exist...
00249 
00250     Purpose:    Returns a single line from the grm file.
00251 
00252     Notes:      As this is fairly general read and write ini file code, then this has been
00253                 expanded so that an option other ini file name can be specified. If this is
00254                 specified then this will be used instead of the GRM file name.
00255                 Added by Neville 14/1/97 for saving/loading paths for plug-ins as the main 
00256                 ini file code cannot cope with the concept of sections filled with an arbitrary
00257                 number of items which need cleaning out when resaving to remove old items no
00258                 longer required.
00259 
00260 ********************************************************************************************/
00261 
00262 BOOL LibSettings::GetEntry(String_256 *Section, INT32 Line, String_256 *Entry, FilePos *Pos,
00263                            String_256 * pIniFileString)
00264 {
00265     ERROR3IF(Entry == NULL, "LibSettings::GetEntry - not happy with null params");
00266 
00267     BOOL ok = FALSE;
00268 
00269     CCDiskFile GetEntryFile(1024, FALSE, TRUE);
00270 
00271     if(GetEntryStart(&GetEntryFile))
00272     {
00273         ok = GetEntryFromFile(&GetEntryFile, Section, Line, Entry, Pos);
00274     }
00275 
00276     GetEntryFinish(&GetEntryFile);
00277     
00278     return ok;  
00279 }
00280 
00281 
00282 /********************************************************************************************
00283 
00284 >   static BOOL LibSettings::GetEntryStart(CCDiskFile *DiskFile, String_256 * pIniFileString = NULL)
00285 
00286     Author:     Richard_Millican (Xara Group Ltd) <camelotdev@xara.com>
00287     Created:    24/1/96
00288     Inputs:     DiskFile    - The DiskFile to use in following GetEntryFromFile calls
00289                 pIniFileString  set to NULL if using default gallery ini file (Default)
00290                                 or to the leafname of the ini file to use
00291     Outputs:    DiskFile    - This will be initialised and opened withe the correct grm file
00292     Returns:    TRUE if this was successful
00293 
00294     Purpose:    Call with a pre-created, but not initialised CCDiskFile. After calling this,
00295                 calls to GetEntryFromFile can be passed this diskfile. When finished, call
00296                 GetEntryFinish with the DiskFile to De-do all the stuff we done here, like...
00297 
00298     Notes:      As this is fairly general read and write ini file code, then this has been
00299                 expanded so that an option other ini file name can be specified. If this is
00300                 specified then this will be used instead of the GRM file name.
00301                 Added by Neville 14/1/97 for saving/loading paths for plug-ins as the main 
00302                 ini file code cannot cope with the concept of sections filled with an arbitrary
00303                 number of items which need cleaning out when resaving to remove old items no
00304                 longer required.
00305 
00306 ********************************************************************************************/
00307 
00308 BOOL LibSettings::GetEntryStart(CCDiskFile *DiskFile, String_256 * pIniFileString)
00309 {
00310     if(DiskFile == NULL)
00311     {
00312         ERROR3("LibSettings::GetEntryStart given null diskfile");
00313         return FALSE;
00314     }
00315     
00316     // Get the pathname of the grm file
00317     PathName GRMPath;
00318     if(!GetPathName(&GRMPath, pIniFileString))
00319     {
00320         ERROR3("LibSettings::GetEntryStart couldn't even get the pathname without going wrong...");
00321         return FALSE;
00322     }
00323 
00324     if(!DiskFile->InitLexer(TRUE))
00325     {
00326         ERROR3("LibSettings::GetEntryStart - Problems with InitLexer");
00327         return FALSE;
00328     }
00329 
00330     TRY
00331     {
00332         // Open the GRM file
00333         if(!DiskFile->open(GRMPath, ios::in | ios::nocreate))
00334             return FALSE;
00335     }   
00336     CATCH(CFileException, e)
00337     {
00338         // Something bad happened with the GRM file. It possibly doesn't exist...
00339         Error::ClearError();
00340         return FALSE;
00341     }
00342     END_CATCH
00343 
00344     return TRUE;
00345 }
00346 
00347 /********************************************************************************************
00348 
00349 >   static BOOL LibSettings::GetEntryFinish(CCDiskFile *DiskFile)
00350 
00351     Author:     Richard_Millican (Xara Group Ltd) <camelotdev@xara.com>
00352     Created:    24/1/96
00353     Inputs:     DiskFile    - The DiskFile to tidy up
00354     Outputs:
00355     Returns:    TRUE if this was successful
00356 
00357     Purpose:    Call after GetEntryStart, and after any GetEntryFromFile calls, but before
00358                 killing the DiskFile off.
00359                 This will close the file, and de-initialise the lexer, etc...
00360 
00361 ********************************************************************************************/
00362 
00363 BOOL LibSettings::GetEntryFinish(CCDiskFile *DiskFile)
00364 {
00365     if(DiskFile == NULL)
00366     {
00367         ERROR3("LibSettings::GetEntryFinish given null diskfile");
00368         return FALSE;
00369     }
00370 
00371     if(DiskFile == NULL)
00372         return TRUE;
00373 
00374     BOOL ok = TRUE;
00375 
00376     // Make sure that the files are closed and the memory is reclaimed properly...
00377     TRY
00378     {
00379         // Tidy up...
00380         DiskFile->DeinitLexer();
00381         if (DiskFile->isOpen()) DiskFile->close();
00382     }
00383     CATCH(CFileException, e)
00384     {
00385         // Failed to close the files - not much we can do about it really
00386         Error::ClearError();
00387         ok = FALSE;
00388     }
00389     END_CATCH
00390 
00391     return ok;
00392 }
00393 
00394 
00395 /********************************************************************************************
00396 
00397 >   static BOOL LibSettings::GetEntryFromFile(CCLexFile *TheFile, String_256 *Section, INT32 Line,
00398         String_256 *Entry)
00399 
00400     Author:     Richard_Millican (Xara Group Ltd) <camelotdev@xara.com>
00401     Created:    25/1/96
00402     Inputs:     TheFile - An open and initialised LexFile which we should be using
00403                 Section - Text to match with the section names
00404                 Line    - Line offset in the section to return
00405     Outputs:    Entry   - The line associated with the two input params
00406                 Pos     - The FilePos after the item was found
00407 
00408     Returns:    TRUE if this was successful. FALSE if we hit the next section, or EOF
00409                 or if the file doesn't exist...
00410 
00411     Purpose:    Returns a single line from the grm file.
00412 
00413 ********************************************************************************************/
00414 
00415 BOOL LibSettings::GetEntryFromFile(CCLexFile *TheFile, String_256 *Section, INT32 Line, String_256 *Entry,
00416                                    FilePos *Pos)
00417 {
00418     BOOL FoundLine = FALSE;
00419     BOOL ok = TRUE;
00420 
00421     TRY
00422     {
00423         // When we're doing lines in the section specified this will be true
00424         BOOL InOurSection = FALSE;
00425 
00426         // This will count the number of the lines since the last section header
00427         INT32 LineCount = 0;
00428 
00429         String_256 TokenBuffer;
00430         LexTokenType TokType = TOKEN_LINE;
00431 
00432         // Loop through file checking for sections and incrementing the line count
00433         // Finish at EOF, if we find a token, or if there was a problem...
00434         while(TokType != TOKEN_EOF && ok && !FoundLine && TheFile->GetLineToken())
00435         {
00436             TokType = TheFile->GetTokenType();
00437 
00438             if(TokType == TOKEN_LINE)
00439             {
00440                 TokenBuffer = TheFile->GetTokenBuf();
00441                 LineCount ++;
00442 
00443                 // Check if we've hit a new section name, if so check it and stop the output...
00444                 String_256 PossibleSectionName;             
00445                 if(IsThisANewSection(&TokenBuffer, &PossibleSectionName))
00446                 {
00447                     LineCount = 0;
00448                     if(InOurSection)
00449                     {
00450                         // ERROR3("LibSettings::GetEntry - Line outside section");
00451                         ok = FALSE;
00452                     }
00453                     InOurSection = (PossibleSectionName == *Section);
00454                 }
00455 
00456                 // Check if it's the line we're interested in or not
00457                 if(InOurSection && Line == LineCount)
00458                 {
00459                     *Entry = TokenBuffer;
00460                     FoundLine = TRUE;
00461                 }
00462             }
00463         }
00464     }
00465 
00466     // See if there was a file io error
00467     CATCH(CFileException, e)
00468     {
00469         // Something rather unhelpful has taken place...
00470         ok = FALSE;
00471 
00472         // Report the error if no one else did
00473         if (Error::GetErrorNumber() != _R(IDN_USER_CANCELLED))
00474             InformError();
00475         else
00476             Error::ClearError();    // otherwise remove the error so it won't get reported
00477     }
00478     END_CATCH
00479 
00480     TRY
00481     {
00482         // Tidy up...
00483         if(Pos != NULL)
00484             *Pos = TheFile->tellIn();
00485     }
00486     CATCH(CFileException, e)
00487     {
00488         // Failed to close the files - not much we can do about it really
00489         Error::ClearError();
00490     }
00491     END_CATCH
00492 
00493 #ifdef _DEBUG
00494     // Something went wrong...
00495     if(!ok && FoundLine)
00496     {
00497         PathName GRMPath;
00498         if(GetPathName(&GRMPath))
00499             if(SGLibOil::FileExists(&GRMPath))
00500                 ERROR3("LibSettings::GetEntry - Something went wrong...");
00501     }
00502 #endif
00503 
00504     return (FoundLine && ok);
00505 }
00506 
00507 
00508 /********************************************************************************************
00509 
00510 >   static BOOL LibSettings::AddLine(String_256 *Line, String_256 * pIniFileString = NULL)
00511 
00512     Author:     Richard_Millican (Xara Group Ltd) <camelotdev@xara.com>
00513     Created:    18/5/95
00514     Inputs:     Line    - Line of text to add to file
00515                 pIniFileString  set to NULL if using default gallery ini file (Default)
00516                                 or to the leafname of the ini file to use
00517     Outputs:
00518     Returns:    TRUE if this was successful.
00519 
00520     Purpose:    Adds a single line to the end of the grm file.
00521 
00522     Notes:      As this is fairly general read and write ini file code, then this has been
00523                 expanded so that an option other ini file name can be specified. If this is
00524                 specified then this will be used instead of the GRM file name.
00525                 Added by Neville 14/1/97 for saving/loading paths for plug-ins as the main 
00526                 ini file code cannot cope with the concept of sections filled with an arbitrary
00527                 number of items which need cleaning out when resaving to remove old items no
00528                 longer required.
00529 
00530 ********************************************************************************************/
00531 
00532 BOOL LibSettings::AddLine(String_256 *Line, String_256 * pIniFileString)
00533 {
00534     ERROR3IF(Line == NULL, "LibSettings::AddLine - not happy with null params");
00535 
00536     BOOL ok = TRUE;
00537 
00538     PathName GRMPath;
00539     if(!GetPathName(&GRMPath, pIniFileString))
00540     {
00541         ERROR3("LibSettings::AddLine - Problems getting pathname for grm file");
00542         return FALSE;
00543     }
00544 
00545     CCDiskFile GRMFile(1024, FALSE, TRUE);
00546 
00547     TRY
00548     {
00549         // Initialise the lexer memory for the token routines...
00550         if (!GRMFile.InitLexer(FALSE))
00551         {
00552             ERROR3("LibSettings::AddLine - Problems with InitLexer");
00553             ok = FALSE;
00554         }
00555 
00556         // Open file for appending to...
00557         if (ok && !GRMFile.open(GRMPath, ios::app))
00558         {
00559             ERROR3("LibSettings::AddLine - Failed to open the grm file for appending");
00560             ok = FALSE;
00561         }
00562 
00563         // Pop the token at the end of the file...
00564         if(ok) ok = GRMFile.PutToken(*Line, (String_8)"\r\n");
00565     }
00566 
00567     // See if there was a file io error
00568     CATCH(CFileException, e)
00569     {
00570         // Report the error if no one else did
00571         if (Error::GetErrorNumber() != _R(IDN_USER_CANCELLED))
00572             InformError();
00573         else
00574             Error::ClearError();    // otherwise remove the error so it won't get reported
00575 
00576         ok = FALSE;
00577     }
00578     END_CATCH
00579 
00580     // Make sure that the file is closed
00581     TRY
00582     {
00583         // Tidy up...
00584         GRMFile.DeinitLexer();
00585         if (GRMFile.isOpen()) GRMFile.close();
00586     }
00587     CATCH(CFileException, e)
00588     {
00589         // Failed to close the file - not much we can do about it really
00590         Error::ClearError();
00591     }
00592     END_CATCH
00593 
00594     if(!ok) ERROR3("LibSettings::AddLine - Something went wrong");
00595     return ok;
00596 }
00597 
00598 
00599 /********************************************************************************************
00600 
00601 >   static BOOL LibSettings::AddSection(String_256 *Section, String_256 * pIniFileString = NULL)
00602 
00603     Author:     Richard_Millican (Xara Group Ltd) <camelotdev@xara.com>
00604     Created:    18/5/95
00605     Inputs:     Section - Section name to add to file
00606                 pIniFileString  set to NULL if using default gallery ini file (Default)
00607                                 or to the leafname of the ini file to use
00608     Outputs:
00609     Returns:    TRUE if this was successful.
00610 
00611     Purpose:    Adds a new section to the end of the grm file.
00612 
00613     Notes:      This is essentially the same as AddLine but it gives us control of what
00614                 sections look like.
00615 
00616     Notes:      As this is fairly general read and write ini file code, then this has been
00617                 expanded so that an option other ini file name can be specified. If this is
00618                 specified then this will be used instead of the GRM file name.
00619                 Added by Neville 14/1/97 for saving/loading paths for plug-ins as the main 
00620                 ini file code cannot cope with the concept of sections filled with an arbitrary
00621                 number of items which need cleaning out when resaving to remove old items no
00622                 longer required.
00623 
00624 ********************************************************************************************/
00625 
00626 BOOL LibSettings::AddSection(String_256 *Section, String_256 * pIniFileString)
00627 {
00628     ERROR3IF(Section == NULL, "LibSettings::AddSection - not happy with null params");
00629 
00630     String_256 Line("[");
00631     Line += *Section;
00632     Line += "]";
00633     return AddLine(&Line, pIniFileString);
00634 }
00635 
00636 
00637 /********************************************************************************************
00638 
00639 >   static BOOL LibSettings::IsThisANewSection(String_256 *Line, String_256 *Section)
00640 
00641     Author:     Richard_Millican (Xara Group Ltd) <camelotdev@xara.com>
00642     Created:    18/5/95
00643     Inputs:     Line    - Line of text to check
00644     Outputs:    Section - The actual section name itself
00645     Returns:    TRUE if this line is a new section name
00646 
00647     Purpose:    Checks if a given line is a new section or not and returns the section name
00648                 if it is...
00649 
00650 ********************************************************************************************/
00651 
00652 BOOL LibSettings::IsThisANewSection(String_256 *Line, String_256 *Section)
00653 {
00654     ERROR3IF(Section == NULL, "LibSettings::IsThisANewSection - not happy with null params");
00655     ERROR3IF(Line == NULL, "LibSettings::IsThisANewSection - not happy with null params");
00656 
00657     if((Line->Sub((String_8)"[") != -1) && (Line->Sub((String_8)"]") != -1))
00658     {
00659         INT32 first = Line->Sub((String_8)"[");
00660         INT32 second = Line->Sub((String_8)"]");
00661 
00662         if(first < second-1)
00663         {
00664             Line->Mid(Section, first+1, (second - first)-1);
00665             return TRUE;
00666         }
00667     }
00668     return FALSE;
00669 }
00670 
00671 
00672 /********************************************************************************************
00673 
00674 >   static BOOL LibSettings::StartOutput(String_256 Section, String_256 * pIniFileString = NULL)
00675 
00676     Author:     Richard_Millican (Xara Group Ltd) <camelotdev@xara.com>
00677     Created:    18/5/95
00678     Inputs:     Section - The section we're about to add
00679                 pIniFileString  set to NULL if using default gallery ini file (Default)
00680                                 or to the leafname of the ini file to use
00681     Outputs:
00682     Returns:    TRUE if this was successful.
00683 
00684     Purpose:    Checks if there's already a grm file on the disk. If so, create a new .bak file
00685                 holding copies of all the sections (except the one specified), then copy this
00686                 new file over the old .grm file.
00687 
00688                 Doing this means that future calls to AddLine will append the information to
00689                 the end of the grm file, and there won't be any duplication.
00690 
00691     Notes:      This avoids the old system of sending a message around all the galleries, and
00692                 also means that unopened galleries don't need to store a list of their groups.
00693 
00694                 Also, if there's a crisis, the old grm file is still on the disk...
00695 
00696     Notes:      As this is fairly general read and write ini file code, then this has been
00697                 expanded so that an option other ini file name can be specified. If this is
00698                 specified then this will be used instead of the GRM file name.
00699                 Added by Neville 14/1/97 for saving/loading paths for plug-ins as the main 
00700                 ini file code cannot cope with the concept of sections filled with an arbitrary
00701                 number of items which need cleaning out when resaving to remove old items no
00702                 longer required.
00703 
00704 ********************************************************************************************/
00705 
00706 BOOL LibSettings::StartOutput(String_256 *Section, String_256 * pIniFileString)
00707 {
00708     ERROR3IF(Section == NULL, "LibSettings::StartOutput - not happy with null params");
00709 
00710     // Get the pathname of the grm file
00711     PathName GRMPath;
00712     BOOL ok = GetPathName(&GRMPath, pIniFileString);
00713     if(!ok)
00714     {
00715         ERROR3("LibSettings::StartOutput couldn't even get the pathname without going wrong...");
00716         return FALSE;
00717     }
00718 
00719     if(!SGLibOil::FileExists(&GRMPath))
00720     {
00721         // No existing GRM file, don't bother keeping its contents...
00722         return TRUE;
00723     }
00724 
00725     PathName BAKPath(GRMPath);
00726     BAKPath.SetType("bak");
00727 
00728     CCDiskFile GRMFile(1024, FALSE, TRUE);
00729     CCDiskFile BAKFile(1024, FALSE, TRUE);
00730 
00731     TRY
00732     {
00733         if (!GRMFile.InitLexer(FALSE) || !BAKFile.InitLexer(FALSE))
00734         {
00735             ERROR3("LibSettings::StartOutput - Problems with InitLexer");
00736             ok = FALSE;
00737         }
00738 
00739         if(ok) ok = GRMFile.open(GRMPath, ios::in | ios::nocreate);
00740         if(ok) ok = BAKFile.open(BAKPath, ios::out);
00741         
00742         if(ok)
00743         {
00744             // When we're doing lines in the section specified this will be true
00745             BOOL InOurSection = FALSE;
00746             BOOL Finished = FALSE;
00747 
00748             String_256 TokenBuffer;
00749             LexTokenType TokType = TOKEN_LINE;
00750 
00751             // Read and write the lines...
00752             while(TokType != TOKEN_EOF && ok && GRMFile.GetLineToken())
00753             {
00754                 TokType = GRMFile.GetTokenType();
00755 
00756                 if(TokType == TOKEN_LINE)
00757                 {
00758                     TokenBuffer = GRMFile.GetTokenBuf();
00759 
00760                     // Check if we've hit a new section name, if so check it and stop the output...
00761                     String_256 PossibleSectionName;             
00762                     if(IsThisANewSection(&TokenBuffer, &PossibleSectionName))
00763                         InOurSection = (PossibleSectionName == *Section);
00764 
00765                     // If it's not the specified section, write the line straight out to the bak file
00766                     if(!InOurSection)
00767                     {
00768                         if(ok) ok = BAKFile.PutToken(TokenBuffer, (String_8)"\r\n");
00769                     }
00770                 }
00771             }
00772         }
00773     }
00774 
00775     // See if there was a file io error
00776     CATCH(CFileException, e)
00777     {
00778         // Something rather unhelpful has taken place...
00779         ok = FALSE;
00780 
00781         // Report the error if no one else did
00782         if (Error::GetErrorNumber() != _R(IDN_USER_CANCELLED))
00783             InformError();
00784         else
00785             Error::ClearError();    // otherwise remove the error so it won't get reported
00786     }
00787     END_CATCH
00788 
00789     // Make sure that the files are closed and the memory is reclaimed properly...
00790     TRY
00791     {
00792         // Tidy up...
00793         GRMFile.DeinitLexer();
00794         BAKFile.DeinitLexer();
00795         if (GRMFile.isOpen()) GRMFile.close();
00796         if (BAKFile.isOpen()) BAKFile.close();
00797     }
00798     CATCH(CFileException, e)
00799     {
00800         // Failed to close the files - not much we can do about it really
00801         Error::ClearError();
00802     }
00803     END_CATCH
00804 
00805     // Copy the BAK file over the GRM file
00806     if(ok) SGLibOil::FileDelete(&GRMPath);
00807     if(ok) ok = SGLibOil::FileCopy(&BAKPath, &GRMPath);
00808     
00809     // Always delete the bak file, whatever happened
00810     SGLibOil::FileDelete(&BAKPath);
00811 
00812     // Something went wrong...
00813     if(!ok) ERROR3("LibSettings::StartOutput - Something went wrong...");
00814     return ok;
00815 }
00816 
00817 /********************************************************************************************
00818 
00819 >   static BOOL LibSettings::NukeSettings(String_256 * pIniFileString = NULL)
00820 
00821     Author:     Richard_Millican (Xara Group Ltd) <camelotdev@xara.com>
00822     Created:    12/6/95
00823     Inputs:     
00824     Outputs:
00825     Returns:    TRUE if this was successful.
00826 
00827     Purpose:    Delete all the settings from the gallery resource file.
00828 
00829                 This will be called when the user hits 'default' at startup, so the galleries
00830                 will have to find some other way of working out where their groups are - the
00831                 ini file for instance, but since this will also have been destroyed, there only
00832                 remains 1 place - the default static setting - eek !
00833 
00834     Notes:      As this is fairly general read and write ini file code, then this has been
00835                 expanded so that an option other ini file name can be specified. If this is
00836                 specified then this will be used instead of the GRM file name.
00837                 Added by Neville 14/1/97 for saving/loading paths for plug-ins as the main 
00838                 ini file code cannot cope with the concept of sections filled with an arbitrary
00839                 number of items which need cleaning out when resaving to remove old items no
00840                 longer required.
00841 
00842 ********************************************************************************************/
00843 
00844 BOOL LibSettings::NukeSettings(String_256 * pIniFileString)
00845 {
00846     PathName GRMFile;
00847     if(LibSettings::GetPathName(&GRMFile, pIniFileString))
00848     {
00849         SGLibOil::FileDelete(&GRMFile);
00850         return TRUE;
00851     }
00852     return FALSE;
00853 }
00854 
00855 /********************************************************************************************
00856 
00857 >   static BOOL LibSettings::GetPathName(PathName *PathName, String_256 * pIniFileString = NULL)
00858 
00859     Author:     Richard_Millican (Xara Group Ltd) <camelotdev@xara.com>
00860     Created:    18/5/95
00861     Inputs:     pIniFileString  set to NULL if using default gallery ini file (Default)
00862                                 or to the leafname of the ini file to use
00863     Outputs:    PathName    - returns the pathname of the settings file in c:\windows, or wherever
00864     Returns:    TRUE if successful
00865     Purpose:    Returns the path and filename for the gallery resource management file...
00866                 Currently this file holds the groups in the gallery for use when it next
00867                 starts up... 
00868     
00869     Notes:      As this is fairly general read and write ini file code, then this has been
00870                 expanded so that an option other ini file name can be specified. If this is
00871                 specified then this will be used instead of the GRM file name.
00872                 Added by Neville 14/1/97 for saving/loading paths for plug-ins as the main 
00873                 ini file code cannot cope with the concept of sections filled with an arbitrary
00874                 number of items which need cleaning out when resaving to remove old items no
00875                 longer required.
00876 
00877 ********************************************************************************************/
00878 
00879 BOOL LibSettings::GetPathName(PathName *PathName, String_256 * pIniFileString)
00880 {
00881     ERROR3IF(PathName == NULL, "LibSettings::GetPathName - not happy with null params");
00882 
00883     String_256 GRMFile;
00884 
00885     // Have a look in the windows directory for our GRM file...
00886     UINT32 Len = GetWindowsDirectory((TCHAR *)GRMFile, 255);
00887     if ((Len > 0) && (Len < _MAX_PATH))
00888     {
00889         GRMFile += "\\";
00890         // File was called XStudio.grm or XViewer.grm
00891         // We will now call them all CorelX##.ini where ## in this case is GM
00892         String_256 File(PRODUCT_GALLERY_INIFILE);
00893         // If somebody has asked us to use a different ini file name then use this instead
00894         if (pIniFileString != NULL)
00895             File = *pIniFileString;
00896 
00897         GRMFile += File;
00898 
00899         //TRACEUSER( "Richard", _T("Path Gallery Resource Management file - %s\n"), (TCHAR *)GRMFile);
00900 
00901         PathName->SetPathName(GRMFile);
00902         if(!PathName->IsValid())
00903         {
00904             Error::ClearError();
00905             ERROR3("LibSettings::GetPathName - invalid pathname");
00906             return FALSE;
00907         }
00908         
00909         return TRUE;
00910     }
00911 
00912     ERROR3("LibSettings::GetPathName - Problems getting the windows directory name for the Gallery Resource Management file");
00913 
00914     // oops .. Problems getting the windows directory name
00915     return FALSE;
00916 }
00917 
00918 
00919 /********************************************************************************************
00920 
00921 >   static BOOL LibSettings::BuildList(String_256 *Section, List *OutputList);
00922 
00923     Author:     Richard_Millican (Xara Group Ltd) <camelotdev@xara.com>
00924     Created:    25/1/96
00925     Inputs:     Section     - Section title for who contents should be popped in the list
00926     Outputs:    OutputList  - Appends each line it finds to this list
00927     Returns:    TRUE if this was successful.
00928 
00929     Purpose:    Given a section, build a list of entries in the section. This should speed
00930                 reading lots of things from a section up by quite a bit...
00931 
00932 ********************************************************************************************/
00933 
00934 BOOL LibSettings::BuildList(String_256 *Section, List *OutputList)
00935 {
00936     ERROR3IF(Section == NULL || OutputList == NULL, "LibSettings::BuildList given NULL jobbies...");
00937 
00938     BOOL AddedItems = FALSE;
00939     String_256 Entry;
00940     INT32 Line = 1;
00941 
00942     CCDiskFile GetEntryFile(1024, FALSE, TRUE);
00943 
00944     if(GetEntryStart(&GetEntryFile))
00945     {
00946         if(SeekToSection(&GetEntryFile, Section))
00947         {
00948             while(GetNextLine(&GetEntryFile, &Entry))
00949             {
00950                 LibSettingsListItem *LibSettingsItem = new LibSettingsListItem(&Entry);
00951                 OutputList->AddTail((ListItem *)LibSettingsItem);
00952                 AddedItems = TRUE;
00953             }
00954         }
00955 
00956         GetEntryFinish(&GetEntryFile);
00957     }
00958 
00959     return AddedItems;
00960 }
00961 
00962 
00963 /********************************************************************************************
00964 
00965 >   static BOOL LibSettings::GetNextLine(CCDiskFile *TheFile, String_256 *Line)
00966 
00967     Author:     Richard_Millican (Xara Group Ltd) <camelotdev@xara.com>
00968     Created:    26/1/96
00969     Inputs:     TheFile - Open and seeked lex/disk file
00970     Outputs:    Line    - Will contain the next line (if we return TRUE)
00971     Returns:    TRUE if this was successful and a line was found and placed in Line.
00972 
00973     Purpose:    Output the next line of the file passed in. Return FALSE if problems.
00974 
00975     Notes:      If we hit a section title, we also return FALSE
00976 
00977 ********************************************************************************************/
00978 
00979 BOOL LibSettings::GetNextLine(CCDiskFile *TheFile, String_256 *Line)
00980 {
00981     BOOL FoundLine = FALSE;
00982     BOOL ok = TRUE;
00983 
00984     TRY
00985     {
00986         String_256 TokenBuffer;
00987         LexTokenType TokType = TOKEN_LINE;
00988 
00989         while(TokType != TOKEN_EOF && !FoundLine && TheFile->GetLineToken())
00990         {
00991             TokType = TheFile->GetTokenType();
00992             if(TokType == TOKEN_LINE)
00993             {
00994                 TokenBuffer = TheFile->GetTokenBuf();
00995 
00996                 // Check if we've hit a new section name, if so check it and stop the output...
00997                 String_256 PossibleSectionName;             
00998                 if(IsThisANewSection(&TokenBuffer, &PossibleSectionName))
00999                     return FALSE;
01000 
01001                 *Line = TokenBuffer;
01002                 FoundLine = TRUE;
01003             }
01004         }
01005     }
01006 
01007     // See if there was a file io error
01008     CATCH(CFileException, e)
01009     {
01010         // Something rather unhelpful has taken place...
01011         ok = FALSE;
01012 
01013         // Report the error if no one else did
01014         if (Error::GetErrorNumber() != _R(IDN_USER_CANCELLED))
01015             InformError();
01016         else
01017             Error::ClearError();    // otherwise remove the error so it won't get reported
01018     }
01019     END_CATCH
01020 
01021     return (FoundLine && ok);
01022 }
01023 
01024 /********************************************************************************************
01025 
01026 >   static BOOL LibSettings::SeekToSection(CCDiskFile *TheFile, String_256 *Section)
01027 
01028     Author:     Richard_Millican (Xara Group Ltd) <camelotdev@xara.com>
01029     Created:    26/1/96
01030     Inputs:     TheFile - Open lex/disk file (preferably not seeked half way through)
01031                 Section - The section we're a'looking for
01032     Returns:    TRUE if this was successful and we're now seeked to the EOL after the section
01033                 name
01034     Purpose:    Hunt down our section in the grm file
01035 
01036 ********************************************************************************************/
01037 
01038 BOOL LibSettings::SeekToSection(CCDiskFile *TheFile, String_256 *Section)
01039 {
01040     TRY
01041     {
01042         String_256 TokenBuffer;
01043         LexTokenType TokType = TOKEN_LINE;
01044 
01045         while(TokType != TOKEN_EOF && TheFile->GetLineToken())
01046         {
01047             TokType = TheFile->GetTokenType();
01048             if(TokType == TOKEN_LINE)
01049             {
01050                 TokenBuffer = TheFile->GetTokenBuf();
01051 
01052                 // Check if we've hit a new section name, if so check it and stop the output...
01053                 String_256 PossibleSectionName;             
01054                 if(IsThisANewSection(&TokenBuffer, &PossibleSectionName))
01055                 {
01056                     if(PossibleSectionName == *Section)
01057                         return TRUE;
01058                 }
01059             }
01060         }
01061     }
01062 
01063     // See if there was a file io error
01064     CATCH(CFileException, e)
01065     {
01066         // Report the error if no one else did
01067         if (Error::GetErrorNumber() != _R(IDN_USER_CANCELLED))
01068             InformError();
01069         else
01070             Error::ClearError();    // otherwise remove the error so it won't get reported
01071     }
01072     END_CATCH
01073 
01074     return FALSE;
01075 }

Generated on Sat Nov 10 03:48:56 2007 for Camelot by  doxygen 1.4.4