Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.88 KB | None | 0 0
  1. #include "common.h"
  2. #include "event.h"
  3. #include "file.h"
  4. #include "dialog.h"
  5.  
  6. Row *ReadFile(void *n, char *filename, Event *readerr)
  7. /* Read all content from a file, and format all the data to the struct */
  8. {
  9.     FILE *fp;
  10.     Char *ch, *chtemp;
  11.     Row *row, *rowtemp, *rowhead;
  12.     if ((fp = fopen(filename, "rt")) == NULL)
  13.     {
  14.         DoEvent(readerr, n);
  15.         return NULL;
  16.     }
  17.     ch = ALLOC(sizeof(Char));
  18.     CheckAlloc((void *)ch);
  19.     row = ALLOC(sizeof(Row));
  20.     CheckAlloc((void *)row);
  21.     rowhead = row;
  22.     row -> ch = ch;
  23.     while(!feof(fp))
  24.     {
  25.         fscanf(fp, "%c", &ch -> ch);
  26.         if(ch -> ch == 0)
  27.             continue;
  28.         if(ch -> ch == 10 || ch -> ch == 13)
  29.         {
  30.             ch -> ch = 0;
  31.             ch -> next = NULL;
  32.             rowtemp = ALLOC(sizeof(Row));
  33.             CheckAlloc((void *)rowtemp);
  34.             row -> next = rowtemp;
  35.             row = rowtemp;
  36.             ch = ALLOC(sizeof(Char));
  37.             CheckAlloc((void *)ch);
  38.             row -> ch = ch;
  39.         }
  40.         else
  41.         {
  42.             chtemp = ALLOC(sizeof(Char));
  43.             CheckAlloc((void *)chtemp);
  44.             ch -> next = chtemp;
  45.             ch = chtemp;
  46.         }
  47.     }
  48.     ch -> ch = 0;
  49.     ch -> next = NULL;
  50.     row -> next = NULL;
  51.     fclose(fp);
  52.     return rowhead;
  53. }
  54.  
  55. void NewFile(void *n)
  56. /* open a new file */
  57. {
  58.     State *s;
  59.     Char *ch;
  60.     Row *head;
  61.     Row *row;
  62.     s = (State *)n;
  63.     if(s -> highlight)
  64.         ChangeHighlight(n);
  65.     ch = ALLOC(sizeof(Char));
  66.     CheckAlloc((void *)ch);
  67.     row = ALLOC(sizeof(Row));
  68.     CheckAlloc((void *)row);
  69.     row -> ch = ch;
  70.     ch -> ch = 0;
  71.     ch -> next = NULL;
  72.     row -> next = NULL;
  73.     head = row;
  74.     if(s -> filehead != NULL)
  75.         FREEMEMRow(s -> filehead);
  76.     s -> filehead = head;
  77.     s -> currentrow = 1;
  78.     s -> currentcolumn = 1;
  79.     s -> pagemovecount = 0;
  80.     memset(s -> filename, 0, 20);
  81.     //s -> filename[0] = 0;
  82.     CleanUp(n);
  83.     if(!s -> dotted)
  84.     {
  85.         setactivepage(!s -> page);
  86.         DrawMainWindow(n);
  87.         DrawStatusBox(n);
  88.         DoEvent(s -> redraw, n);
  89.         CleanUp(n);
  90.         setvisualpage(!s -> page);
  91.         s -> page = !s -> page;
  92.     }
  93.     else
  94.     {
  95.         DrawMainWindow(n);
  96.         DrawStatusBox(n);
  97.         DoEvent(s -> redraw, n);
  98.         CleanUp(n);
  99.     }
  100.     /* Do a redraw before draw it */
  101. }
  102.  
  103. void ShowFile(void *row)
  104. {
  105.     Char *ch;
  106.     Row *rowtmp;
  107.     rowtmp = (Row *)row;
  108.     do
  109.     {
  110.         ch = rowtmp -> ch;
  111.         do
  112.         {
  113.             printf("%c", ch -> ch);
  114.             ch = ch -> next;
  115.         }while(ch != NULL);
  116.         printf("\n");
  117.         rowtmp = rowtmp -> next;
  118.     }while(rowtmp != NULL);
  119. }
  120.  
  121. /*Row *NewFile(char *NewFile, Event *readerr)
  122. {
  123.     Char *ch;
  124.     Row *head;
  125.     Row *row;
  126.     FILE *fp;
  127.     if ((fp = fopen(NewFile, "wr+")) == NULL)
  128.     {
  129.         DoEvent(readerr, (void *)NewFile);
  130.         return NULL;
  131.     }
  132.     ch = ALLOC(sizeof(Char));
  133.     row = ALLOC(sizeof(Row));
  134.     head = row;
  135.     row -> ch = ch;
  136.     ch = '\0';
  137.     ch -> next = NULL;
  138.     row -> next = NULL;
  139.     fclose(fp);
  140.     return head;
  141. }*/
  142.  
  143. void ReadErr(void *n)
  144. {
  145.     State *s;
  146.     char tmp[40];
  147.     s = (State *)n;
  148.     sprintf(tmp, "无法打开文件 %s", s -> filename);
  149.     MsgBox(n, "错误", tmp);
  150.     //NewFile(n);
  151.     //printf("Error reading file %s!\n", (char *)filename);
  152. }
  153.  
  154. void SaveFile(void *n)
  155. {
  156.     State *s;
  157.     FILE *fp;
  158.     Char *ch;
  159.     Row *line;
  160.     int i,j;
  161.    
  162.     s = (State *)n;
  163.    
  164.     if(s -> filename == NULL)
  165.         return ;
  166.    
  167.     if ((fp = fopen(s -> filename, "w")) == NULL)
  168.     {
  169.         printf("SaveError");
  170.         return;
  171.     }
  172.    
  173.     line = s -> filehead;
  174.     if( s -> filehead == NULL)
  175.         return;
  176.    
  177.     for(j = 0;line -> next != NULL;j++)
  178.     {
  179.         ch = line -> ch;
  180.         for(i = 0;ch -> next != NULL;i++)
  181.         {
  182.             fprintf(fp, "%c", &ch -> ch);
  183.             ch = ch -> next;
  184.         }
  185.         line = line -> next;
  186.     }
  187.     fclose(fp);
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement