home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Programming / Triton / Source / prefs / trPrefsUpdate.c < prev   
C/C++ Source or Header  |  1998-05-23  |  8KB  |  258 lines

  1. /*
  2.  *  OpenTriton -- A free release of the triton.library source code
  3.  *  Copyright (C) 1993-1998  Stefan Zeiger
  4.  *
  5.  *  This program is free software; you can redistribute it and/or modify
  6.  *  it under the terms of the GNU General Public License as published by
  7.  *  the Free Software Foundation; either version 2 of the License, or
  8.  *  (at your option) any later version.
  9.  *
  10.  *  This program is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *  GNU General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU General Public License
  16.  *  along with this program; if not, write to the Free Software
  17.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  *  trPrefsUpdate/trPrefsUpdate.c - Prefs updator
  20.  *
  21.  */
  22.  
  23.  
  24. #define TRPREFSUPDATE_VERSION "3.2"
  25. #define TRPREFSUPDATE_RELEASE "1.3"
  26. #define TRPREFSUPDATE_DATE    "3.6.95"
  27.  
  28.  
  29. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  30. //////////////////////////////////////////////////////////////////////////////////////// Include our stuff //
  31. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  32.  
  33. #define TR_THIS_IS_TRITON
  34. #define TR_PREFSONLY
  35.  
  36. #include <exec/memory.h>
  37. #include <dos/dos.h>
  38. #include <graphics/gfx.h>
  39. #include <libraries/gadtools.h>
  40. #include <libraries/triton.h>
  41. #include <proto/intuition.h>
  42. #include <proto/graphics.h>
  43. #include <proto/dos.h>
  44. #include <clib/alib_protos.h>
  45. #include <clib/exec_protos.h>
  46. #include <pragmas/exec_pragmas.h>
  47.  
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <string.h>
  51.  
  52. #include "Triton.h"
  53. #include "/internal.h"
  54.  
  55.  
  56. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  57. ///////////////////////////////////////////////////////////////////////////////////////// Global variables //
  58. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  59.  
  60. struct TR_AppPrefsV2
  61. {
  62.   // UBYTE version;
  63.   BOOL  frames_width;
  64.   UBYTE frames_title;
  65.   UWORD frames_type[5];
  66.   BOOL  frames_raised[5];
  67.   UBYTE pens_windowback;
  68.   UBYTE pens_reqback;
  69.   UBYTE pubscreen[32];
  70. };
  71.  
  72. struct TR_AppPrefsV3
  73. {
  74.   // UBYTE version;
  75.   BOOL  frames_width;
  76.   UBYTE frames_title;
  77.   UWORD frames_type[5];
  78.   BOOL  frames_raised[5];
  79.   ULONG pentype[8];
  80.   ULONG pendata[8];
  81.   ULONG imgtype[4];
  82.   ULONG imgdata[4];
  83.   UBYTE pubscreen[32];
  84.   ULONG flags;
  85. };
  86.  
  87.  
  88. struct PrefsNode
  89. {
  90.   struct Node node;
  91.   struct TR_AppPrefs *prefs;
  92.   UBYTE filename[50];
  93. };
  94.  
  95. struct FileList
  96. {
  97.   struct List list;
  98.   APTR mempool;
  99. };
  100.  
  101.  
  102. UBYTE *versionstring="\0$VER: trPrefsUpdate " TRPREFSUPDATE_VERSION " (" TRPREFSUPDATE_DATE ") Triton Preferences Updator, © 1994-1995 by Stefan Zeiger";
  103. struct FileList *prefslist;
  104.  
  105.  
  106. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  107. ////////////////////////////////////////////////////////////////////////////////// Node and prefs handling //
  108. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  109.  
  110. BOOL LoadAppPrefs(void *pool, struct PrefsNode *prefsnode)
  111. {
  112.   BPTR lock;
  113.   int i;
  114.   struct TR_AppPrefs *prefs;
  115.  
  116.   struct TR_AppPrefsV2 p2;
  117.   struct TR_AppPrefsV3 p3;
  118.  
  119.   if(!(prefs=LibAllocPooled(pool,sizeof(struct TR_AppPrefs)))) return FALSE;
  120.   CopyMem((APTR)(&TR_DefaultAppPrefs),(APTR)prefs,sizeof(struct TR_AppPrefs));
  121.   prefsnode->prefs=prefs;
  122.  
  123.   if(!(lock=Open(prefsnode->filename,MODE_OLDFILE))) return FALSE;
  124.   FRead(lock,(STRPTR)prefs,sizeof(UBYTE),1);
  125.   switch(prefs->version)
  126.   {
  127.     case 2:
  128.       FRead(lock,(STRPTR)(&p2),sizeof(struct TR_AppPrefsV2),1);
  129.       prefs->frames_width=p2.frames_width;
  130.       prefs->frames_title=p2.frames_title;
  131.       //for(i=0;i<4;i++) prefs->frames_type[i]=p2.frames_type[i];
  132.       //for(i=0;i<4;i++) prefs->frames_raised[i]=p2.frames_raised[i];
  133.       //for(i=0;i<31;i++) prefs->pubscreen[i]=p2.pubscreen[i];
  134.       printf("Loaded '%s' in format %d.\n",prefsnode->filename,prefs->version);
  135.       break;
  136.     case 3:
  137.       FRead(lock,(STRPTR)(&p3),sizeof(struct TR_AppPrefsV3),1);
  138.       CopyMem((APTR)(&p3),(APTR)(((ULONG)prefs)+1),sizeof(struct TR_AppPrefsV3));
  139.       printf("Loaded '%s' in format %d.\n",prefsnode->filename,prefs->version);
  140.       break;
  141.     default:
  142.       printf("File '%s' has wrong format (%d)!\n",prefsnode->filename,prefs->version);
  143.       Close(lock);
  144.       prefsnode->prefs=NULL;
  145.       return FALSE;
  146.   }
  147.   Close(lock);
  148.  
  149.   return TRUE;
  150. }
  151.  
  152.  
  153. VOID SaveAllAppPrefs(struct FileList *fl)
  154. {
  155.   struct Node *worknode;
  156.   BPTR lock;
  157.  
  158.   worknode=(struct Node *)(((struct List *)fl)->lh_Head);
  159.   while(worknode)
  160.   {
  161.     if(((struct PrefsNode *)worknode)->prefs)
  162.     {
  163.       if(lock=Open(((struct PrefsNode *)worknode)->filename,MODE_NEWFILE))
  164.       {
  165.         FWrite(lock,(STRPTR)(((struct PrefsNode *)worknode)->prefs),sizeof(struct TR_AppPrefs),1);
  166.         Close(lock);
  167.         printf("Saved '%s' in format %d.\n",((struct PrefsNode *)worknode)->filename,TR_PREFSVERSION);
  168.       } else printf("Can't save '%s'.\n",((struct PrefsNode *)worknode)->filename);
  169.     }
  170.     worknode=(struct Node *)(worknode->ln_Succ);
  171.   }
  172. }
  173.  
  174.  
  175. void DeleteFileList(struct FileList *fl)
  176. {
  177.   if(!fl) return;
  178.   LibDeletePool(fl->mempool);
  179. }
  180.  
  181.  
  182. struct FileList *CreatePrefsList(void)
  183. {
  184.   struct FileList *fl;
  185.   struct Node *node;
  186.   void *pool;
  187.   struct FileInfoBlock *fib=NULL;
  188.   BPTR lock;
  189.   BOOL ok=TRUE;
  190.   UBYTE totalfilename[50];
  191.  
  192.   if(!(pool=LibCreatePool(MEMF_CLEAR,1024,512))) return NULL;
  193.   if(!(fl=LibAllocPooled(pool,sizeof(struct FileList)))) { LibDeletePool(pool); return NULL; }
  194.   fl->mempool=pool;
  195.   NewList((struct List *)fl);
  196.  
  197.   if(!(lock=Lock("envarc:Triton",ACCESS_READ))) { ok=FALSE; goto cleanup; }
  198.   if(!(fib=(struct FileInfoBlock *)AllocDosObject(DOS_FIB,NULL))) { ok=FALSE; goto cleanup; }
  199.   if(!(Examine(lock,fib))) { ok=FALSE; goto cleanup; }
  200.   while(ExNext(lock,fib))
  201.   {
  202.     sprintf(totalfilename,"envarc:Triton/%s",fib->fib_FileName);
  203.     if(strstr(totalfilename,".tri"))
  204.     {
  205.       if(node=LibAllocPooled(pool,sizeof(struct PrefsNode)))
  206.       {
  207.         strcpy(((struct PrefsNode *)node)->filename,totalfilename);
  208.         if(LoadAppPrefs(pool,((struct PrefsNode *)node)))
  209.         {
  210.           AddTail((struct List *)fl,node);
  211.         } else printf("Can't load '%s'.\n",totalfilename);
  212.       }
  213.       else printf("Not enough memory to load '%s'.\n",totalfilename);
  214.     }
  215.   }
  216.  
  217.   cleanup:
  218.   if(fib) FreeDosObject(DOS_FIB,(void *)fib);
  219.   if(lock) UnLock(lock);
  220.   if(ok) return fl;
  221.   DeleteFileList(fl);
  222.   return NULL;
  223. }
  224.  
  225.  
  226. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  227. ////////////////////////////////////////////////////////////////////////////////////////////// Check prefs //
  228. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  229.  
  230. BOOL checkprefs(void)
  231. {
  232.   BPTR lock;
  233.  
  234.   if(lock=Lock("envarc:Triton",ACCESS_READ)) { UnLock(lock); return TRUE; }
  235.   else return FALSE;
  236. }
  237.  
  238.  
  239. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  240. /////////////////////////////////////////////////////////////////////////////////////////////////// main() //
  241. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  242.  
  243. int main(void)
  244. {
  245.   if(!checkprefs()) { puts("Triton Preferences system is not installed."); return 0; }
  246.  
  247.   if(prefslist=CreatePrefsList())
  248.   {
  249.     SaveAllAppPrefs(prefslist);
  250.     Execute("c:delete env:Triton all",NULL,NULL);
  251.     Execute("c:makedir env:Triton",NULL,NULL);
  252.     Execute("c:copy envarc:Triton/#? env:Triton/",NULL,NULL);
  253.     DeleteFileList(prefslist);
  254.   } else { puts("Not enough memory to create preferences list."); return 20; }
  255.  
  256.   return 0;
  257. }
  258.