home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 594a.lha / maker_v0.1 / file.c < prev    next >
C/C++ Source or Header  |  1991-10-18  |  18KB  |  702 lines

  1. #include "global.h"
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <exec/memory.h>
  6. #include <libraries/asl.h>
  7.  
  8. #include <proto/intuition.h>
  9. #include <proto/exec.h>
  10. #include <proto/asl.h>
  11. #include <proto/dos.h>
  12.  
  13.  
  14. void PutTF(BOOL flag);
  15. BOOL ReadStrings(FILE *filePtr, char ***theList, Remember **remKey);
  16. BOOL WriteStrings(FILE *filePtr, char **charList);
  17. BOOL WriteList(FILE *filePtr, List *itemList);
  18. BOOL ReadList(FILE *filePtr, List *itemList, Remember **remKey);
  19. void ResolveListRefs(void);
  20.  
  21. #if 0
  22.             printf("\t%d, %d,\t\t\t// LeftEdge, TopEdge\n", gadgObj->rect.minX, gadgObj->rect.minY);
  23.             printf("\t%d, %d,\t\t\t// Width, Height\n",
  24.                                     gadgObj->rect.maxX - gadgObj->rect.minX + 1,
  25.                                     gadgObj->rect.maxY - gadgObj->rect.minY + 1);
  26.             printf("\t\"%s\",\t\t\t// GadgetText\n", gadgObj->label);
  27.             printf("\t0L,\t\t\t// TextAttr\n");
  28.             printf("\t%d,\t\t\t// GadgetID\n", nodePtr->itemNum);
  29.             printf("\t%ld,\t\t\t// Flags\n", gadgObj->flags);
  30.             printf("\tgVI,\t\t\t// VisualInfo\n");
  31.             printf("\t0L\t\t\t// UserData\n");
  32. #endif
  33.  
  34. #define FILEVERSION    1
  35. #define    OFFSET(mem, st)    ( (long) &(((st *)0L)->mem))
  36.  
  37. BOOL DoSaveAs(USHORT code)
  38. {
  39.     LinkNode        *nodePtr;
  40.     GadgetObj    *gadgObj;
  41.     FILE            *filePtr;
  42.     ULONG            lval;
  43.     short            itemNum;
  44.     char            buf[MAX_TEXT_LEN],
  45.                     fileName[256];
  46.     TextAttr        tempAttr;
  47.     USHORT        len;
  48.     
  49. /*    gFileRequest->rf_File[0] = 0;    */
  50.  
  51.     if (!RequestFile(gFileRequest))
  52.         return FALSE;
  53.     
  54.     strcpy(fileName, gFileRequest->rf_Dir);
  55.     if (!AddPart(fileName, gFileRequest->rf_File, 256))
  56.     {
  57.         printf("Error in file name\n");
  58.         return FALSE;
  59.     }
  60.         
  61.     memset(&tempAttr, 0, sizeof(TextAttr));
  62.     
  63.     //    Open the output file for binary access
  64.     
  65.     filePtr = fopen(fileName, "wb");
  66.     if (!filePtr)
  67.     {
  68.         printf("Error: unable to open file '%s'\n", fileName);
  69.         return FALSE;
  70.     }
  71.     
  72.     // write out the file signature data (signature='MAKR', plus FILEVERSION)
  73.     
  74.     lval = 'MAKR';
  75.     if (fwrite( (void *) &lval, sizeof(ULONG), 1, filePtr) != 1)
  76.         goto error;
  77.         
  78.     lval = FILEVERSION;
  79.     if (fwrite( (void *) &lval, sizeof(ULONG), 1, filePtr) != 1)
  80.         goto error;
  81.     
  82.     // write out any supplemental header data
  83.     
  84.     lval = 4 * sizeof(UWORD) + 2 * sizeof(BOOL);            // sizeof the supplemental header data
  85.     if (fwrite( (void *) &lval, sizeof(ULONG), 1, filePtr) != 1)
  86.         goto error;
  87.     
  88.     // fwrite here for header data
  89.     
  90.     if (fwrite( (void *) &gWindPtr->LeftEdge, 4L * sizeof(UWORD), 1, filePtr) != 1)
  91.         goto error;
  92.     if (fwrite( (void *) &gWindSizeable, sizeof(BOOL), 1, filePtr) != 1)
  93.         goto error;
  94.     if (fwrite( (void *) &gEditMode, sizeof(BOOL), 1, filePtr) != 1)
  95.         goto error;
  96.     if (fwrite( (void *) &gDisableAll, sizeof(BOOL), 1, filePtr) != 1)
  97.         goto error;
  98.     
  99.     // now write out the individual data for each object in the list
  100.  
  101.     for (nodePtr=gObjList; nodePtr; nodePtr=nodePtr->next)
  102.     {
  103.     
  104.         // save the data that is common to all objects
  105.         
  106.         if (fwrite( (void *) &nodePtr->type, sizeof(LinkNode) - OFFSET(type, LinkNode), 1, filePtr) != 1)
  107.             goto error;
  108.     
  109.         // if this is a gadget based object, dispose the data common to all gadgets
  110.         
  111.         if (GetGadgetData(nodePtr, 0L, 0L, 0L, 0L, 0L))
  112.         {
  113.             
  114.             if (fwrite( (void *) &((GadgetObj *)nodePtr)->flags, sizeof(ULONG), 1, filePtr) != 1)
  115.                 goto error;
  116.             len = strlen(((GadgetObj *)nodePtr)->label) + 1;
  117.             if (fwrite( (void *) &len, sizeof(USHORT), 1, filePtr) != 1)
  118.                 goto error;
  119.             if (len)
  120.                 if (fwrite( (void *) &((GadgetObj *)nodePtr)->label, len, 1, filePtr) != 1)
  121.                     goto error;
  122.             if (fwrite( (void *) &((GadgetObj *)nodePtr)->disable, sizeof(BOOL), 1, filePtr) != 1)
  123.                 goto error;
  124.         }
  125.         
  126.         // now write out the data that is specific to each object type
  127.         
  128.         switch(nodePtr->type)
  129.         {
  130.             case OTYPE_Text:
  131.                 if (fwrite( (void *) &((TextObj *)nodePtr)->copyText,
  132.                             sizeof(TextObj) - OFFSET(copyText, TextObj), 1, filePtr) != 1)
  133.                     goto error;
  134.                 break;
  135.     
  136.             case OTYPE_Button:
  137.                 break;
  138.     
  139.             case OTYPE_Cycle:
  140.                 if (fwrite( (void *) &((CycleObj *)nodePtr)->activeItem, sizeof(UWORD), 1, filePtr) != 1)
  141.                     goto error;
  142.                 
  143.                 if (!WriteStrings(filePtr, ((CycleObj *)nodePtr)->textLabels))
  144.                     goto error;
  145.                     
  146.                 break;
  147.     
  148.             case OTYPE_MX:
  149.                 if (fwrite( (void *) &((MXObj *)nodePtr)->activeItem, 2 * sizeof(UWORD), 1, filePtr) != 1)
  150.                     goto error;
  151.                 
  152.                 if (!WriteStrings(filePtr, ((MXObj *)nodePtr)->textLabels))
  153.                     goto error;
  154.                 break;
  155.     
  156.             case OTYPE_CheckBox:
  157.                 if (fwrite( (void *) &((CheckBoxObj *)nodePtr)->checked,
  158.                             sizeof(CheckBoxObj) - OFFSET(checked, CheckBoxObj), 1, filePtr) != 1)
  159.                     goto error;
  160.                 break;
  161.     
  162.             case OTYPE_Scroller:
  163.                 if (fwrite( (void *) &((ScrollerObj *)nodePtr)->topItem,
  164.                             sizeof(ScrollerObj) - OFFSET(topItem, ScrollerObj), 1, filePtr) != 1)
  165.                     goto error;
  166.                 break;
  167.     
  168.             case OTYPE_Slider:
  169.                 if (fwrite( (void *) &((SliderObj *)nodePtr)->minLevel,
  170.                             sizeof(SliderObj) - OFFSET(minLevel, SliderObj), 1, filePtr) != 1)
  171.                     goto error;
  172.                 break;
  173.     
  174.             case OTYPE_String:
  175.                 if (fwrite( (void *) &((StringObj *)nodePtr)->maxChars,
  176.                             sizeof(StringObj) - OFFSET(maxChars, StringObj), 1, filePtr) != 1)
  177.                     goto error;
  178.                 break;
  179.     
  180.             case OTYPE_Palette:
  181.                 if (fwrite( (void *) &((PaletteObj *)nodePtr)->firstColor,
  182.                             sizeof(PaletteObj) - OFFSET(firstColor, PaletteObj), 1, filePtr) != 1)
  183.                     goto error;
  184.                 break;
  185.     
  186.             case OTYPE_ListView:
  187.     
  188.                 // walk the gadget list & find the specified gadget
  189.                 
  190.                 itemNum = -1;
  191.                 if (((ListViewObj *)nodePtr)->stringObj)
  192.                 {
  193.                     for (gadgObj= (GadgetObj *) gObjList, itemNum=0; gadgObj;
  194.                                                 gadgObj=gadgObj->next, itemNum++)
  195.                         if ( ((ListViewObj *)nodePtr)->stringObj == gadgObj )
  196.                             break;
  197.                     
  198.                     if (!gadgObj || gadgObj->type != OTYPE_String)
  199.                     {
  200.                         itemNum = -1;
  201.                         ((ListViewObj *)nodePtr)->stringObj = 0L;
  202.                     }
  203.                 }
  204.                 ((ListViewObj *)nodePtr)->tempStringID = itemNum;
  205.                 
  206.                 if (fwrite( (void *) &((ListViewObj *)nodePtr)->topItem,
  207.                             sizeof(ListViewObj) - OFFSET(topItem, ListViewObj), 1, filePtr) != 1)
  208.                     goto error;
  209.                 
  210.                 if (!WriteList(filePtr, &((ListViewObj *)nodePtr)->itemList))
  211.                     goto error;
  212.                     
  213.                 break;
  214.             
  215.             case OTYPE_IText:
  216.                 if (fwrite( (void *) &((ITextObj *)nodePtr)->iText,
  217.                             3 * sizeof(UBYTE) + 2 * sizeof(SHORT), 1, filePtr) != 1)
  218.                     goto error;
  219.                 
  220.                 if (((ITextObj *)nodePtr)->iText.ITextFont)
  221.                 {
  222.                     if (fwrite( (void *) &((ITextObj *)nodePtr)->iText.ITextFont->ta_YSize,
  223.                                 sizeof(UWORD) + 2 * sizeof(UBYTE), 1, filePtr) != 1)
  224.                         goto error;
  225.                     len = strlen( ((ITextObj *)nodePtr)->iText.ITextFont->ta_Name ) + 1;
  226.                     if (fwrite( (void *) &len, sizeof(USHORT), 1, filePtr) != 1)
  227.                         goto error;
  228.                     if (fwrite( (void *) ((ITextObj *)nodePtr)->iText.ITextFont->ta_Name,
  229.                                 len, 1, filePtr) != 1)
  230.                         goto error;
  231.                 }
  232.                 else
  233.                 {
  234.                     memset(buf, 0, FONT_NAME_LEN);
  235.                     if (fwrite( (void *) buf, sizeof(UWORD) + 2 * sizeof(UBYTE), 1, filePtr) != 1)
  236.                         goto error;
  237.                     len = 0;
  238.                     if (fwrite( (void *) &len, sizeof(USHORT), 1, filePtr) != 1)
  239.                         goto error;
  240.                 }
  241.                 
  242.                 len = strlen( ((ITextObj *)nodePtr)->label ) + 1;
  243.                 if (fwrite( (void *) &len, sizeof(USHORT), 1, filePtr) != 1)
  244.                     goto error;
  245.                 if (fwrite( (void *) ((ITextObj *)nodePtr)->label, len, 1, filePtr) != 1)
  246.                     goto error;
  247.                     
  248.                 break;
  249.         }
  250.     }
  251.     
  252.     fclose(filePtr);
  253.     ReDraw();
  254.     return TRUE;
  255.     
  256. error:
  257.  
  258.     printf("An error occurred while writing file '%s'\n", fileName);
  259.     fclose(filePtr);
  260.     return FALSE;
  261. }
  262.  
  263. BOOL DoOpen(USHORT code)
  264. {
  265.     LinkNode        *nodePtr;
  266.     FILE            *filePtr;
  267.     ULONG            lval;
  268.     UWORD            dim[4], objType, sval, len;
  269.     char            fileName[256];
  270.     
  271. /*    gFileRequest->rf_File[0] = 0;    */
  272.  
  273.     if (!RequestFile(gFileRequest))
  274.         return FALSE;
  275.     
  276.     strcpy(fileName, gFileRequest->rf_Dir);
  277.     if (!AddPart(fileName, gFileRequest->rf_File, 256))
  278.     {
  279.         printf("Error in file name\n");
  280.         return FALSE;
  281.     }
  282.         
  283.     
  284.     //    Open the file for binary read access
  285.     
  286.     filePtr = fopen(fileName, "rb");
  287.     if (!filePtr)
  288.     {
  289.         printf("Error: unable to open file '%s'\n", fileName);
  290.         return FALSE;
  291.     }
  292.     
  293.     // read the file signature data (signature='MAKR', plus FILEVERSION)
  294.     
  295.     if (fread( (void *) &lval, sizeof(ULONG), 1, filePtr) != 1)
  296.         goto error;
  297.     if (lval != 'MAKR')
  298.     {
  299.         printf("Error: file has a bad signature!\n");
  300.         goto error;
  301.     }
  302.         
  303.     if (fread( (void *) &lval, sizeof(ULONG), 1, filePtr) != 1)
  304.         goto error;
  305.     if (lval != FILEVERSION)
  306.     {
  307.         printf("Error: file is of an incompatible version\n");
  308.         goto error;
  309.     }    
  310.  
  311.     // read any supplemental header data
  312.     
  313.     if (fread( (void *) &lval, sizeof(ULONG), 1, filePtr) != 1)
  314.         goto error;
  315.     
  316.     if (fread( (void *) dim, 4L * sizeof(UWORD), 1, filePtr) != 1)
  317.         goto error;
  318.     if (fread( (void *) &gWindSizeable, sizeof(BOOL), 1, filePtr) != 1)
  319.         goto error;
  320.     if (fread( (void *) &gEditMode, sizeof(BOOL), 1, filePtr) != 1)
  321.         goto error;
  322.     if (fread( (void *) &gDisableAll, sizeof(BOOL), 1, filePtr) != 1)
  323.         goto error;
  324.     
  325.     NewWindow(dim[0], dim[1], dim[2], dim[3], gWindSizeable);
  326.     
  327.     while (gObjList)
  328.         DisposeObj( gObjList );
  329.     gSelObj = 0L;
  330.     
  331.     // now read in the individual data for each object in the list
  332.  
  333.     while (fread( (void *) &objType, sizeof(UWORD), 1, filePtr) == 1)
  334.     {
  335.         nodePtr = AllocateObj(objType);
  336.         if (!nodePtr)
  337.         {
  338.             printf("Error: failed to allocate an object!\n");
  339.             goto error;
  340.         }
  341.             
  342.         if (fread( (void *) &sval, sizeof(UWORD), 1, filePtr) != 1)
  343.             goto error;
  344.         if (sval != nodePtr->numBytes)
  345.         {
  346.             printf("Error: Object size for '%s' does not match\n", gObjName[objType]);
  347.             goto error;
  348.         }
  349.         
  350.         if (fread( (void *) &nodePtr->rect, sizeof(Rect), 1, filePtr) != 1)
  351.             goto error;
  352.             
  353.         // if this is a gadget based object, read the data common to all gadgets
  354.         
  355.         if (GetGadgetData(nodePtr, 0L, 0L, 0L, 0L, 0L))
  356.         {
  357.             
  358.             if (fread( (void *) &((GadgetObj *)nodePtr)->flags, sizeof(ULONG), 1, filePtr) != 1)
  359.                 goto error;
  360.             if (fread( (void *) &len, sizeof(USHORT), 1, filePtr) != 1)
  361.                 goto error;
  362.             if (len)
  363.                 if (fread( (void *) &((GadgetObj *)nodePtr)->label, len, 1, filePtr) != 1)
  364.                     goto error;
  365.             if (fread( (void *) &((GadgetObj *)nodePtr)->disable, sizeof(BOOL), 1, filePtr) != 1)
  366.                 goto error;
  367.         }
  368.         
  369.         // now read in the data that is specific to each object type
  370.         
  371.         switch(nodePtr->type)
  372.         {
  373.             case OTYPE_Text:
  374.                 if (fread( (void *) &((TextObj *)nodePtr)->copyText,
  375.                             sizeof(TextObj) - OFFSET(copyText, TextObj), 1, filePtr) != 1)
  376.                     goto error;
  377.                 break;
  378.     
  379.             case OTYPE_Button:
  380.                 break;
  381.     
  382.             case OTYPE_Cycle:
  383.                 if (fread( (void *) &((CycleObj *)nodePtr)->activeItem, sizeof(UWORD), 1, filePtr) != 1)
  384.                     goto error;
  385.                 
  386.                 if (!ReadStrings(filePtr, &((CycleObj *)nodePtr)->textLabels,
  387.                                         &((CycleObj *)nodePtr)->remKey))
  388.                     goto error;
  389.                 break;
  390.     
  391.             case OTYPE_MX:
  392.                 if (fread( (void *) &((MXObj *)nodePtr)->activeItem, 2 * sizeof(UWORD), 1, filePtr) != 1)
  393.                     goto error;
  394.                 
  395.                 if (!ReadStrings(filePtr, &((MXObj *)nodePtr)->textLabels,
  396.                                         &((MXObj *)nodePtr)->remKey))
  397.                     goto error;
  398.                 break;
  399.     
  400.             case OTYPE_CheckBox:
  401.                 if (fread( (void *) &((CheckBoxObj *)nodePtr)->checked,
  402.                             sizeof(CheckBoxObj) - OFFSET(checked, CheckBoxObj), 1, filePtr) != 1)
  403.                     goto error;
  404.                 break;
  405.     
  406.             case OTYPE_Scroller:
  407.                 if (fread( (void *) &((ScrollerObj *)nodePtr)->topItem,
  408.                             sizeof(ScrollerObj) - OFFSET(topItem, ScrollerObj), 1, filePtr) != 1)
  409.                     goto error;
  410.                 break;
  411.     
  412.             case OTYPE_Slider:
  413.                 if (fread( (void *) &((SliderObj *)nodePtr)->minLevel,
  414.                             sizeof(SliderObj) - OFFSET(minLevel, SliderObj), 1, filePtr) != 1)
  415.                     goto error;
  416.                 break;
  417.     
  418.             case OTYPE_String:
  419.                 if (fread( (void *) &((StringObj *)nodePtr)->maxChars,
  420.                             sizeof(StringObj) - OFFSET(maxChars, StringObj), 1, filePtr) != 1)
  421.                     goto error;
  422.                 break;
  423.     
  424.             case OTYPE_Palette:
  425.                 if (fread( (void *) &((PaletteObj *)nodePtr)->firstColor,
  426.                             sizeof(PaletteObj) - OFFSET(firstColor, PaletteObj), 1, filePtr) != 1)
  427.                     goto error;
  428.                 break;
  429.     
  430.             case OTYPE_ListView:
  431.                 ( (ListViewObj *) nodePtr)->stringObj = 0L;
  432.                 if (fread( (void *) &((ListViewObj *)nodePtr)->topItem,
  433.                             sizeof(ListViewObj) - OFFSET(topItem, ListViewObj), 1, filePtr) != 1)
  434.                     goto error;
  435.                     
  436.                 if (!ReadList(filePtr, &((ListViewObj *)nodePtr)->itemList,
  437.                                     &((ListViewObj *)nodePtr)->remKey))
  438.                     goto error;
  439. /*                    
  440.                 if (fread( (void *) &itemNum, sizeof(UWORD), 1, filePtr) != 1)
  441.                     goto error;
  442. */                
  443.                 break;
  444.             
  445.             case OTYPE_IText:
  446.                 if (fread( (void *) &((ITextObj *)nodePtr)->iText,
  447.                             3 * sizeof(UBYTE) + 2 * sizeof(SHORT), 1, filePtr) != 1)
  448.                     goto error;
  449.                 
  450.                 if (fread( (void *) &((ITextObj *)nodePtr)->textAttr.ta_YSize,
  451.                                 sizeof(UWORD) + 2 * sizeof(UBYTE), 1, filePtr) != 1)
  452.                     goto error;
  453.                     
  454.                 if (fread( (void *) &len, sizeof(USHORT), 1, filePtr) != 1)
  455.                     goto error;
  456.                     
  457.                 if (len)
  458.                 {
  459.                     if (fread( (void *) ((ITextObj *)nodePtr)->fontName, len, 1, filePtr) != 1)
  460.                         goto error;                
  461.                     ((ITextObj *)nodePtr)->iText.ITextFont = &((ITextObj *)nodePtr)->textAttr;
  462.                 }
  463.                 else
  464.                 {
  465.                     ((ITextObj *)nodePtr)->iText.ITextFont = 0L;
  466.                     ((ITextObj *)nodePtr)->fontName[0] = 0;
  467.                 }
  468.                 
  469.                 ((ITextObj *)nodePtr)->textAttr.ta_Name = ((ITextObj *)nodePtr)->fontName;
  470.                     
  471.                 if (fread( (void *) &len, sizeof(USHORT), 1, filePtr) != 1)
  472.                     goto error;
  473.                     
  474.                 if (fread( (void *) ((ITextObj *)nodePtr)->label, len, 1, filePtr) != 1)
  475.                     goto error;
  476.                     
  477.                 ((ITextObj *)nodePtr)->iText.IText = (STRPTR) ((ITextObj *)nodePtr)->label;
  478.                     
  479.                 break;
  480.     
  481.         }
  482.         if (GetGadgetData(nodePtr, 0L, 0L, 0L, 0L, 0L))
  483.         {
  484.             if (MakeGadget(nodePtr) == 2)
  485.             {
  486.                 // the operation failed, so nuke the object
  487.                 
  488.                 DisposeObj(nodePtr);
  489.             }
  490.         }
  491.     }
  492.     
  493.     fclose(filePtr);
  494.     ResolveListRefs();        // remake the ListView gadgets with user specified string gadgets
  495.                                     // attached
  496.     ReDraw();
  497.     return TRUE;
  498.     
  499. error:
  500.  
  501.     printf("An error occurred while reading file '%s'\n", fileName);
  502.     fclose(filePtr);
  503.     return FALSE;
  504. }
  505.  
  506. void ResolveListRefs(void)
  507. {
  508.     ListViewObj        *listPtr;
  509.     StringObj        *stringPtr;
  510.     
  511.     for (listPtr=(ListViewObj *) gObjList; listPtr; listPtr=listPtr->next)
  512.     {
  513.         if (listPtr->type == OTYPE_ListView)
  514.         {
  515.             if (listPtr->tempStringID >= 0)
  516.             {
  517.                     stringPtr = FindNode(&gObjList, listPtr->tempStringID);
  518.                     if (stringPtr && stringPtr->type == OTYPE_String)
  519.                         listPtr->stringObj = stringPtr;
  520.                         
  521.                     MakeGadget(listPtr);
  522.             }
  523.             else
  524.                 listPtr->stringObj = 0L;
  525.         }
  526.     }
  527. }
  528.  
  529. BOOL ReadStrings(FILE *filePtr, char ***theList, Remember **remKey)
  530. {
  531.     UWORD        numNode, iNode;
  532.     ULONG        totLength, iDest;
  533.     char        *charPtr, **charList,
  534.                 *tempMem = 0L,
  535.                 *tempPtr, *strPtr;
  536.     
  537.     if (fread( (void *) &numNode, sizeof(UWORD), 1, filePtr) != 1)
  538.         return FALSE;
  539.     if (fread( (void *) &totLength, sizeof(ULONG), 1, filePtr) != 1)
  540.         return FALSE;
  541.                     
  542.     charPtr = AllocRemember( remKey,
  543.                             (ULONG) numNode * MAX_TEXT_LEN + (numNode + 1) * sizeof(char *),
  544.                                         MEMF_CLEAR);
  545.     if (!charPtr)
  546.         return FALSE;
  547.         
  548.     *theList = (char **) charPtr;
  549.     if (!totLength)
  550.         return TRUE;
  551.  
  552.     strPtr = charPtr + (numNode + 1) * sizeof(char *);
  553.     
  554.     // read in the character data
  555.     
  556.     if (!GetMem( (void **) &tempMem, totLength))
  557.         return FALSE;
  558.     tempPtr = tempMem;
  559.             
  560.     if (fread( (void *) tempMem, totLength, 1, filePtr) != 1)
  561.     {
  562.         DropMem( (void **) &tempMem, totLength);
  563.         return FALSE;
  564.     }
  565.         
  566.     // write out the character data
  567.                     
  568.     charList = (char **) charPtr;
  569.     for (iNode=0; iNode<numNode; iNode++)
  570.     {
  571.         iDest = 0;
  572.         while (*tempPtr && iDest < MAX_TEXT_LEN - 1)
  573.             strPtr[iNode * MAX_TEXT_LEN + iDest++] = *tempPtr++;
  574.         tempPtr++;
  575.         strPtr[iNode * MAX_TEXT_LEN + iDest] = 0;
  576.         charList[iNode] = &strPtr[iNode * MAX_TEXT_LEN];
  577.     }
  578.     charList[iNode] = 0L;
  579.         
  580.     DropMem( (void **) &tempMem, totLength);
  581.     
  582.     return TRUE;
  583. }
  584.  
  585. BOOL WriteStrings(FILE *filePtr, char **charList)
  586. {
  587.     UWORD        numNode = 0, i;
  588.     ULONG        totLength = 0;
  589.     
  590.     // Count strings & figure total length
  591.                 
  592.     while (charList[numNode])
  593.         totLength += strlen(charList[numNode++]) + 1;
  594.                 
  595.     if (fwrite( (void *) &numNode, sizeof(UWORD), 1, filePtr) != 1)
  596.         return FALSE;
  597.     if (fwrite( (void *) &totLength, sizeof(ULONG), 1, filePtr) != 1)
  598.         return FALSE;
  599.                     
  600.     // write out the character data
  601.                 
  602.     if (totLength)
  603.     {
  604.         for (i=0; i<numNode; i++)
  605.         {
  606.             if (fwrite( (void *) charList[i], strlen(charList[i]) + 1, 1, filePtr) != 1)
  607.                 return FALSE;
  608.         }
  609.     }
  610.     
  611.     return TRUE;
  612. }
  613.  
  614. BOOL WriteList(FILE *filePtr, List *itemList)
  615. {
  616.     struct Node        *amigaNode;
  617.     UWORD                numNode;
  618.     ULONG                totLength = 0;
  619.     
  620.     
  621.     // Count strings & figure total length
  622.                 
  623.     for (        amigaNode = itemList->lh_Head, numNode=0; amigaNode->ln_Succ;
  624.                 amigaNode=amigaNode->ln_Succ, numNode++ )
  625.         totLength += strlen(amigaNode->ln_Name) + 1;
  626.                 
  627.     if (fwrite( (void *) &numNode, sizeof(UWORD), 1, filePtr) != 1)
  628.         return FALSE;
  629.     if (fwrite( (void *) &totLength, sizeof(ULONG), 1, filePtr) != 1)
  630.         return FALSE;
  631.     
  632.     if (totLength)
  633.     {
  634.         // write out the character data
  635.                     
  636.         for (    amigaNode = itemList->lh_Head; amigaNode->ln_Succ; amigaNode=amigaNode->ln_Succ )
  637.         {
  638.             if (fwrite( (void *) amigaNode->ln_Name, strlen(amigaNode->ln_Name)+1, 1, filePtr) != 1)
  639.                 return FALSE;
  640.         }
  641.     }
  642.     
  643.     return TRUE;
  644. }
  645.  
  646. BOOL ReadList(FILE *filePtr, List *itemList, Remember **remKey)
  647. {
  648.     struct Node        *amigaNode;
  649.     UWORD                numNode, iNode;
  650.     ULONG                totLength, iDest;
  651.     char                *charPtr,
  652.                         *tempMem = 0L,
  653.                         *tempPtr = 0L;
  654.     
  655.     if (fread( (void *) &numNode, sizeof(UWORD), 1, filePtr) != 1)
  656.         return FALSE;
  657.     if (fread( (void *) &totLength, sizeof(ULONG), 1, filePtr) != 1)
  658.         return FALSE;
  659.     
  660.     NewList(itemList);
  661.  
  662.     if (totLength)
  663.     {
  664.         charPtr = AllocRemember(remKey, MAX_TEXT_LEN * (ULONG) numNode, MEMF_CLEAR);
  665.         amigaNode = (Node *) AllocRemember(remKey, sizeof(struct Node) * (ULONG) numNode,
  666.                                                         MEMF_CLEAR);
  667.         if (!charPtr || !amigaNode)
  668.         {
  669.             printf("Error: out of memory\n");
  670.             return FALSE;
  671.         }
  672.  
  673.         if (!GetMem( (void **) &tempMem, totLength))
  674.             return FALSE;
  675.         tempPtr = tempMem;
  676.             
  677.         if (fread( (void *) tempMem, totLength, 1, filePtr) != 1)
  678.         {
  679.             DropMem( (void **) &tempMem, totLength);
  680.             return FALSE;
  681.         }
  682.         
  683.         // write out the character data
  684.                     
  685.         for (iNode=0; iNode<numNode; iNode++)
  686.         {
  687.             iDest = 0;
  688.             while (*tempPtr && iDest < MAX_TEXT_LEN - 1)
  689.                 charPtr[iNode * MAX_TEXT_LEN + iDest++] = *tempPtr++;
  690.             tempPtr++;
  691.             charPtr[iNode * MAX_TEXT_LEN + iDest] = 0;
  692.             amigaNode[iNode].ln_Name = &charPtr[iNode * MAX_TEXT_LEN];
  693.             AddTail(itemList, &amigaNode[iNode]);
  694.         }
  695.         
  696.         DropMem( (void **) &tempMem, totLength);
  697.     }
  698.     
  699.     return TRUE;
  700. }
  701.        
  702.