home *** CD-ROM | disk | FTP | other *** search
/ Network PC / Network PC.iso / amiga utilities / disk utilities / backup / backup_restore / backup_src_v3.20.lha / TapeDrives.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-18  |  7.1 KB  |  275 lines

  1. // TapeDrives.c
  2. // 18 Jan 1996 17:59:45
  3.  
  4. #ifndef    BACKUP_INCLUDE
  5. #include "IncludeAll.c"
  6. #endif
  7. #include "Backup.h"
  8. #include "Backup_proto.h"
  9. #include "TapeDrive.h"
  10. #include "BackupStrings.h"
  11.  
  12.  
  13. static struct TapeDriveProperties *SizeDriveData(size_t NeededLength, size_t *AllocLength, struct TapeDriveProperties *Old);
  14. static BOOL WriteTapeDDEntry(FILE *fd, const struct TapeDriveProperties *DriveData);
  15.  
  16.  
  17. // aus Revision.c
  18. extern short Version;            // aktuelle Versionsnummer
  19.  
  20. // aus Backup.c
  21. extern struct BackupOptions myOptions;        // Einstellungen für die aktuelle Sicherung
  22.  
  23.  
  24. struct TapeDriveProperties *ReadTapeDriveDataBase(void)
  25. {
  26.     static const struct TapeDriveProperties BuiltInDrives[] =
  27.         {
  28.         { "CALIPER CP150    ",            { 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0 } },
  29.         { "HP      HP35470A        8 09", { 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1 } },
  30.         { "TANDBERG TDC 3800       =04Y", { 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0 } },
  31.         { "",                             { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }
  32.         };
  33.     struct TapeDriveProperties *DriveData = NULL;
  34.     const struct TapeDriveProperties *BuiltInDriveData;
  35.     size_t DriveDataAlloc = 0;
  36.     size_t DriveDataCount = 0;
  37.     FILE *fd;
  38.  
  39.     // zuerst die eingebauten Laufwerke eintragen
  40.     BuiltInDriveData = BuiltInDrives;
  41.     while (BuiltInDriveData->Name[0])
  42.         {
  43.         DriveData = SizeDriveData(DriveDataCount + 1, &DriveDataAlloc, DriveData);
  44.         if (NULL == DriveData)
  45.             return NULL;
  46.  
  47.         DriveData[DriveDataCount] = *BuiltInDriveData;
  48.         DriveDataCount++;
  49.         BuiltInDriveData++;
  50.         }
  51.  
  52.     fd = fopen(myOptions.bo_TapeDriveDataFileName, "r");
  53.     if (fd)
  54.         {
  55.         static const char *DDTemplate="Name/K/A,QFA/K/S,FastSpace/K/S,SpaceBack/K/S,Locate/K/S,"
  56.             "PageFormat/K/S,InvertEOT/K/S,EraseLONG/K/S,AppendQFA/K/S,SpaceEOR/K/S,UpdateInPlace/K/S";
  57.         LONG DDResult[12];
  58.         struct RDArgs *RdArgs;
  59.  
  60.         RdArgs = AllocDosObject(DOS_RDARGS, NULL);
  61.         if (NULL == RdArgs)
  62.             {
  63.             // AllocDosObject hat versagt!
  64.             alarm(GetString(MSG_ALLOCDOSOBJ_FAIL), __FUNC__, "DOS_RDARGS");
  65.             }
  66.         else
  67.             {
  68.             RdArgs->RDA_DAList = NULL;
  69.             RdArgs->RDA_Buffer = NULL;
  70.             RdArgs->RDA_BufSiz = 0;
  71.             RdArgs->RDA_ExtHelp = (char *) "";
  72.             RdArgs->RDA_Flags = 0;
  73.             }
  74.  
  75.         while (RdArgs && !feof(fd))
  76.             {
  77.             char Line[250];
  78.             long LineNr = 0;
  79.  
  80.             if (fgets(Line, sizeof(Line), fd))
  81.                 {
  82.                 LineNr++;
  83.  
  84.                 // Kommentarzeilen ("%" am Anfang) überlesen!!
  85.                 if ('%' != Line[0])
  86.                     {
  87.                     RdArgs->RDA_Source.CS_Buffer = (UBYTE *) Line;
  88.                     RdArgs->RDA_Source.CS_Length = strlen(Line);
  89.                     RdArgs->RDA_Source.CS_CurChr = 0;
  90.                     memset(DDResult, 0, sizeof(DDResult));
  91.  
  92.                     if (ReadArgs((STRPTR) DDTemplate, DDResult, RdArgs))
  93.                         {
  94.                         size_t n;
  95.                         BOOL Found;
  96.                         struct TapeDriveProperties *NewDD;
  97.                         struct TapeDriveProperties *DDInsert = DriveData;
  98.  
  99.                         // prüfen ob dieser Eintrag schon vorhanden ist
  100.                         for (Found=FALSE, n=0, NewDD=DriveData; n<DriveDataCount; n++, NewDD++)
  101.                             {
  102.                             if (0 == strcmp(NewDD->Name, (char *) DDResult[0]))
  103.                                 {
  104.                                 // den Eintrag gibt es schon: er wird überschrieben
  105.                                 DDInsert = NewDD;
  106.                                 Found = TRUE;
  107.                                 break;
  108.                                 }
  109.                             }
  110.  
  111.                         if (!Found)
  112.                             {
  113.                             // neuen Eintrag am Ende anhängen
  114.                             DriveData = SizeDriveData(DriveDataCount + 1, &DriveDataAlloc, DriveData);
  115.                             if (NULL == DriveData)
  116.                                 break;
  117.                             DDInsert = &DriveData[DriveDataCount];
  118.                             DriveDataCount++;
  119.                             }
  120.  
  121.                         stccpy(DDInsert->Name, (char *) DDResult[0], sizeof(DDInsert->Name));
  122.                         DDInsert->Properties.Valid = 1;
  123.                         DDInsert->Properties.DriveKnown = 1;
  124.  
  125.                         DDInsert->Properties.canQFA        = ~0 == DDResult[1];
  126.                         DDInsert->Properties.FastSpace     = ~0 == DDResult[2];
  127.                         DDInsert->Properties.canLocate     = ~0 == DDResult[4];
  128.                         DDInsert->Properties.SpaceBack     = ~0 == DDResult[3];
  129.                         DDInsert->Properties.PageFormat    = ~0 == DDResult[5];
  130.                         DDInsert->Properties.InvertEOT     = ~0 == DDResult[6];
  131.                         DDInsert->Properties.EraseLONG     = ~0 == DDResult[7];
  132.                         DDInsert->Properties.AppendQFA     = ~0 == DDResult[8];
  133.                         DDInsert->Properties.SpaceEOR      = ~0 == DDResult[9];
  134.                         DDInsert->Properties.UpdateInPlace = ~0 == DDResult[10];
  135.                         }
  136.                     else
  137.                         {
  138.                         // Formatfehler in dieser Zeile
  139.                         alarm(GetString(MSG_DDFORMAT_ERROR), myOptions.bo_TapeDriveDataFileName, LineNr, Line);
  140.                         }
  141.                     }
  142.                 }
  143.             }
  144.  
  145.         if (RdArgs)
  146.             FreeDosObject(DOS_RDARGS, RdArgs);
  147.         fclose(fd);
  148.         }
  149.  
  150.     // Ende-Marke setzen
  151.     DriveData = SizeDriveData(DriveDataCount + 1, &DriveDataAlloc, DriveData);
  152.     if (DriveData)
  153.         {
  154.         DriveData[DriveDataCount].Name[0] = '\0';
  155.         DriveData[DriveDataCount].Properties.Valid = 0;
  156.         }
  157.  
  158.     return DriveData;
  159. }
  160.  
  161.  
  162. static struct TapeDriveProperties *SizeDriveData(size_t NeededLength, size_t *AllocLength, struct TapeDriveProperties *Old)
  163. {
  164.     struct TapeDriveProperties *DriveData = Old;
  165.  
  166.     if (NeededLength > *AllocLength)
  167.         {
  168.         *AllocLength += DRIVEDATA_ALLOC_STEP;
  169.  
  170.         DriveData = realloc(Old, *AllocLength * sizeof(struct TapeDriveProperties));
  171.         }
  172.  
  173.     return DriveData;
  174. }
  175.  
  176.  
  177. void WriteTapeDriveDataBase(const struct TapeDriveProperties *NewDriveData)
  178. {
  179.     struct TapeDriveProperties *DriveData, *NewDD;
  180.     FILE *fd;
  181.     BOOL Found;
  182.  
  183.     DriveData = ReadTapeDriveDataBase();
  184.  
  185.     for (Found=FALSE, NewDD=DriveData; NewDD->Name[0]; NewDD++)
  186.         {
  187.         if (0 == strcmp(NewDD->Name, NewDriveData->Name))
  188.             {
  189.             // den Eintrag gibt es schon: er wird überschrieben
  190.             *NewDD = *NewDriveData;
  191.             Found = TRUE;
  192.             break;
  193.             }
  194.         }
  195.  
  196.     fd = fopen(myOptions.bo_TapeDriveDataFileName, "w");
  197.     if (fd)
  198.         {
  199.         char zeit[8];
  200.  
  201.         // Kopfzeile schreiben
  202.         getclk(zeit);
  203.         fprintf(fd, GetString(MSG_TAPEDD_HEADER), Version/100, Version % 100,
  204.                 DateString(zeit[3], zeit[2], zeit[1]+80));
  205.  
  206.         for (NewDD=DriveData; NewDD->Name[0]; NewDD++)
  207.             {
  208.             WriteTapeDDEntry(fd, NewDD);
  209.             }
  210.         if (!Found)
  211.             WriteTapeDDEntry(fd, NewDriveData);
  212.  
  213.         fclose(fd);
  214.         }
  215.     else
  216.         {
  217.         // Datenbank kann nicht geöffnet werden
  218.         alarm(GetString(MSG_CANNOTOPEN_TAPEDD), myOptions.bo_TapeDriveDataFileName, GetIoErrText());
  219.         }
  220.  
  221.     if (DriveData)
  222.         free(DriveData);
  223. }
  224.  
  225.  
  226. static BOOL WriteTapeDDEntry(FILE *fd, const struct TapeDriveProperties *DriveData)
  227. {
  228.     fprintf(fd, "Name=\"%s\"", DriveData->Name);
  229.     if (DriveData->Properties.canQFA)
  230.         fputs(" QFA", fd);
  231.     if (DriveData->Properties.FastSpace)
  232.         fputs(" FastSpace", fd);
  233.     if (DriveData->Properties.canLocate)
  234.         fputs(" Locate", fd);
  235.     if (DriveData->Properties.SpaceBack)
  236.         fputs(" SpaceBack", fd);
  237.     if (DriveData->Properties.PageFormat)
  238.         fputs(" PageFormat", fd);
  239.     if (DriveData->Properties.InvertEOT)
  240.         fputs(" InvertEOT", fd);
  241.     if (DriveData->Properties.EraseLONG)
  242.         fputs(" EraseLONG", fd);
  243.     if (DriveData->Properties.AppendQFA)
  244.         fputs(" AppendQFA", fd);
  245.     if (DriveData->Properties.SpaceEOR)
  246.         fputs(" SpaceEOR", fd);
  247.     if (DriveData->Properties.UpdateInPlace)
  248.         fputs(" UpdateInPlace", fd);
  249.  
  250.     fputs("\n", fd);
  251.  
  252.     return TRUE;
  253. }
  254.  
  255.  
  256. struct TapeDriveProperties *FindTapeDrive(struct TapeDriveProperties *DriveData, const char *InquiryName)
  257. {
  258.     struct TapeDriveProperties *Result = NULL;
  259.  
  260.     while (DriveData->Name[0])
  261.         {
  262.         if (0 == strncmp(InquiryName, DriveData->Name, strlen(DriveData->Name)))
  263.             {
  264.             DriveData->Properties.DriveKnown = 1;
  265.             Result = DriveData;
  266.             break;
  267.             }
  268.         DriveData++;
  269.         }
  270.  
  271.     return Result;
  272. }
  273.  
  274.  
  275.