home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / dirs / rramdisk_451.lzh / 'Liner / Source / disk.c < prev    next >
C/C++ Source or Header  |  1991-02-06  |  7KB  |  258 lines

  1. #include "Globals.h"
  2. #include "icon.h"
  3.  
  4.  
  5. WriteItemList(FileName,arexx) /*Write the item list to disk*/
  6. char *FileName;
  7. BYTE arexx;
  8. {
  9.    BPTR fp;
  10.    struct LineItem *CurItem;
  11.    char chip buffer[100];
  12.  
  13.    if((fp=(BPTR)Open(FileName,MODE_NEWFILE))==NULL)
  14.    {
  15.       if(!arexx)
  16.      TitleError("Can't open file for save!");
  17.       return(FALSE);
  18.    }
  19.  
  20.    strcpy(buffer,"LDB3");  /*ID:  Liner Data Block version 3*/
  21.  
  22.    /*Store StartingLevel & double spacing flag as one byte on disk*/
  23.    /*(holdover from the old data format)*/
  24.    buffer[5]=StartingLevel+((prefs.DS) ? 32 : 0);
  25.    Write(fp,buffer,6); /*Write out the header*/
  26.  
  27.    CurItem=(struct LineItem *)FirstItem;
  28.  
  29.    do     /*Write each LineItem to the file*/
  30.    {
  31.       buffer[0]=strlen(CurItem->Text)+3;
  32.       buffer[1]=CurItem->Level;
  33.       buffer[2]=CurItem->ItemNumber;
  34.       buffer[3]=CurItem->cont;
  35.       strcpy(&buffer[4],CurItem->Text);
  36.       Write(fp,buffer,buffer[0]+2);
  37.       CurItem=(struct LineItem *)CurItem->NextItem;
  38.    }
  39.    while(CurItem != NULL);
  40.  
  41.    Close(fp);
  42.    if(prefs.Icons)   /*Create an icon?*/
  43.       PutDiskObject(FileName,&iconinfo);  /*If so, write it out*/
  44.  
  45.    return(TRUE);
  46. }
  47.  
  48. extern void CreatePrefsIcon() /*Create an icon for liner.prefs*/
  49. {
  50.    if(prefs.Icons)
  51.       PutDiskObject("liner:liner.prefs",&prefsiconinfo);
  52. }
  53.  
  54. Old_ReadItemList(FileName,arexx)  /*Read a pre V1.5 item list from disk*/
  55. char *FileName;
  56. BYTE arexx;
  57. {
  58.    BPTR fp;
  59.    struct Old_LineItem chip buffer;
  60.    struct LineItem *CurItem;
  61.    int len;
  62.    char TempIndent;
  63.    char text[80];
  64.  
  65.    if((fp=(BPTR)Open(FileName,MODE_OLDFILE))==NULL)
  66.    {
  67.       if(!arexx)
  68.      TitleError("Can't open file for read!");
  69.       return(FALSE);
  70.    }
  71.  
  72.    Read(fp,&buffer,sizeof(struct Old_LineItem));
  73.    TempIndent=buffer.Text[4];
  74.    buffer.Text[4]=NULL;
  75.    if(strcmp(buffer.Text,"LFDB")) /*'Liner Format Data Block'*/
  76.       {
  77.       Close(fp);
  78.       return(FALSE);
  79.       }
  80.  
  81.    FreeListMem(FirstItem,LastItem); /*Get rid of old outline*/
  82.  
  83.    ModifyMenus(TempIndent);   /*Modify the Double Spacing and Starting Level*/
  84.                   /*menus according to file being read in*/
  85.    SetRowsInScreen();      /*Set the number of rows*/
  86.  
  87.    CurItem=FirstItem=FirstScrnItem=(struct LineItem *)InsertItem(NULL,NULL);
  88.    if(CurItem==NULL)
  89.    {
  90.       CloseGraphics();
  91.       CloseLibrary(IconBase);
  92.       CloseLibrary(DosBase);
  93.       Leave(100,"Memory too fragmented to continue!");
  94.    }
  95.  
  96.    len=Read(fp,&buffer,sizeof(struct Old_LineItem));
  97.  
  98.    while(len > 0) /*While there is still data to be read,*/
  99.    {          /*read it*/
  100.       struct LineItem *old;
  101.  
  102.       CurItem->ItemNumber=buffer.ItemNumber;
  103.       CurItem->Level=buffer.Level;
  104.       CurItem->cont=FALSE;
  105.       strcpy(CurItem->Text,buffer.Text);
  106.       len=Read(fp,&buffer,sizeof(struct Old_LineItem));
  107.       strcpy(text,CurItem->Text);
  108.       CurItem=(struct LineItem *)BreakLineApart(CurItem,NULL,text);
  109.       old=(struct LineItem *)CurItem;
  110.       CurItem=(struct LineItem *)InsertItem(NULL,CurItem);
  111.       if(CurItem==NULL)
  112.       {
  113.      LastItem=(struct LineItem *)old;
  114.      NewAll();
  115.      Leave(0,"Memory too fragmented to perform OPEN!");
  116.      return(FALSE);
  117.       }
  118.    }
  119.  
  120.    LastItem=(struct LineItem *)CurItem->PrevItem;
  121.    LastItem->NextItem=NULL;
  122.    Close(fp);
  123.  
  124.    PrintItemList(FirstItem,1);
  125.    CurrentItem=(struct LineItem *)FirstItem;
  126.    FreeMem(CurItem,sizeof(struct LineItem));
  127.    PlotCursor(MinX(CurrentItem),1);
  128.    return(TRUE);
  129. }
  130.  
  131. ReadItemList(FileName,arexx)  /*Read an item list from disk*/
  132. char *FileName;
  133. BYTE arexx;
  134. {
  135.    BPTR fp;
  136.    struct LineItem *CurItem;
  137.    char chip buffer[100];
  138.    int len;
  139.    char TempIndent;
  140.  
  141.    if((fp=(BPTR)Open(FileName,MODE_OLDFILE))==NULL)
  142.    {
  143.       if(!arexx)
  144.      TitleError("Can't open file for read!");
  145.       return(FALSE);
  146.    }
  147.    Read(fp,buffer,6);
  148.  
  149.    if(strcmp(buffer,"LDB3")) /*'Liner Data Block version 3'*/
  150.    {
  151.       Close(fp);  /*Try reading the old file format*/
  152.       return(Old_ReadItemList(FileName,arexx));
  153.    }
  154.  
  155.    TempIndent=buffer[5];
  156.    FreeListMem(FirstItem,LastItem); /*Get rid of old outline*/
  157.  
  158.    ModifyMenus(TempIndent);   /*Modify the Double Spacing and Starting Level*/
  159.                   /*menus according to file being read in*/
  160.    SetRowsInScreen();      /*Set the number of rows*/
  161.  
  162.    CurItem=FirstItem=FirstScrnItem=(struct LineItem *)InsertItem(NULL,NULL);
  163.    if(CurItem==NULL)
  164.    {
  165.       CloseGraphics();
  166.       CloseLibrary(IconBase);
  167.       CloseLibrary(DosBase);
  168.       Leave(0,"Memory too fragmented to perform OPEN!");
  169.       exit();
  170.    }
  171.  
  172.    len=Read(fp,buffer,1);
  173.  
  174.    while(len > 0 )   /*While information is there*/
  175.    {
  176.       struct LineItem *old;
  177.  
  178.       Read(fp,buffer,buffer[0]+1);
  179.       CurItem->Level=buffer[0]; /*Put it into a LineItem*/
  180.       CurItem->ItemNumber=buffer[1];
  181.       CurItem->cont=buffer[2];
  182.       strcpy(CurItem->Text,&buffer[3]);
  183.       len=Read(fp,buffer,1);
  184.       old=(struct LineItem *)CurItem;
  185.       CurItem=(struct LineItem *)InsertItem(NULL,CurItem);
  186.       if(CurItem==NULL) /*Out of memory*/
  187.       {
  188.      LastItem=(struct LineItem *)old;
  189.      NewAll();
  190.      Leave(0,"Memory too fragmented to perform OPEN!");
  191.      return(FALSE);
  192.       }
  193.    }
  194.  
  195.    LastItem=(struct LineItem *)CurItem->PrevItem;
  196.    LastItem->NextItem=NULL;     /*Delete the last LineItem*/
  197.    FreeMem(CurItem,sizeof(struct LineItem));
  198.    Close(fp);
  199.  
  200.    PrintItemList(FirstItem,1);   /*Print the new outline*/
  201.    CurrentItem=(struct LineItem *)FirstItem; /*Put the cursor*/
  202.    PlotCursor(MinX(CurrentItem),1);       /*at the top*/
  203.    return(TRUE);
  204.  
  205. }
  206.  
  207. /*This function deals with the situation of having a line longer than*/
  208. /*is currently supported for its level (the line lengths were shortened*/
  209. /*slightly from 1.32 to 2.00).    This function puts any extra text onto a*/
  210. /*continuation line that it tacks on after the too-long line.  It will*/
  211. /*copy whole words if it can find them.  If the offending line has no*/
  212. /*spaces, then it will be chopped into two pieces.  No data is lost*/
  213.  
  214. struct LineItem *BreakLineApart(Item,Next,text)
  215. struct LineItem *Item,*Next;
  216. char *text;
  217. {
  218.    BYTE len,c,max;
  219.    struct LineItem *cont;
  220.  
  221.    len=(strlen(text));
  222.    max=MaxLen(Item->Level);
  223.    if(len>max) /*Checks to see if the line is too big.  This way, it*/
  224.    {     /*can be used as a check by the calling function.*/
  225.       cont=(struct LineItem *)InsertItem(Next,Item);
  226.       if(cont==NULL)
  227.       {
  228.      Leave(0,"Memory too fragmented to perform operation!");
  229.      return(Item);
  230.       }
  231.  
  232.       cont->cont=TRUE;
  233.       cont->ItemNumber=Item->ItemNumber;
  234.       cont->Level=Item->Level;
  235.  
  236.      /*Search for a space (breaking apart on a word to make it*/
  237.      /*look nice)*/
  238.       for(c=max;c>=len-max && text[c]!=' ';--c);
  239.       if(c>=len-max && text[c]==' ')
  240.       {     /*Found a space, so break the line apart there*/
  241.      strcpy(cont->Text,&text[c+1]);
  242.      text[c]=NULL;
  243.       }
  244.       else /*Otherwise, chop the line in two*/
  245.       {
  246.      strcpy(cont->Text,&text[max]);
  247.      text[max]=NULL;
  248.       }
  249.       strcpy(Item->Text,text);
  250.       return(cont);
  251.    }
  252.    strcpy(Item->Text,text);
  253.    return(Item);
  254. }
  255.  
  256. /*~~~End of disk.c*/
  257.  
  258.