home *** CD-ROM | disk | FTP | other *** search
/ Zodiac Super OZ / MEDIADEPOT.ISO / FILES / 13 / DEG.ZIP / SCN.C < prev    next >
C/C++ Source or Header  |  1994-07-26  |  6KB  |  378 lines

  1. #include <conio.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <io.h>
  5. #include <dos.h>
  6. #include <fcntl.h>
  7. #include <string.h>
  8. #include <sys\types.h>
  9. #include <sys\stat.h>
  10.  
  11. #define BACKSPACE '\010'
  12. #define BACKUP "\010"
  13. #define CR 0x0d
  14. #define ESC 27
  15. #define ALPHA '@'
  16. #define NUMERIC '#'
  17. #define UP 5
  18. #define DOWN 24
  19. #define LEFT 19
  20. #define RIGHT 4
  21. #define INSERT 25
  22. #define DELETE 2
  23.  
  24. char screen[26][84];
  25. char fname[20];
  26. short position;
  27.  
  28. main()
  29. {
  30. short y,x,key;
  31. short i; 
  32.  
  33.   for(y=0;y<26;y++)
  34.   {
  35.     for(x=0;x<79;x++)
  36.       screen[y][x] = 0x20;
  37.   }
  38.  
  39.   load();
  40.   for(y=0;y<26;y++)
  41.   {
  42.     for(x=0;x<80;x++)
  43.     {
  44.       if(screen[y][x]==0)
  45.         screen[y][x]=32; 
  46.       if(screen[y][x]==13)
  47.         screen[y][x]=32; 
  48.     }
  49.     screen[y][80] = 0;
  50.   }
  51.  
  52.   x = 1;
  53.   y = 2;
  54.  
  55.   printf("\33[01;01H");
  56.   for(;;)
  57.   {
  58.     if(y==1)
  59.       y = 2;
  60.     if(x==0)
  61.       x = 1;
  62.     key = getch();
  63.     if(key==0)
  64.     {
  65.       key = getch();
  66.       switch(key)
  67.       {
  68.         case 72:
  69.           key = UP;
  70.           break;
  71.         case 80:
  72.           key = DOWN;
  73.           break;
  74.         case 75:
  75.           key = LEFT;
  76.           break;
  77.         case 77:
  78.           key = RIGHT;
  79.           break;
  80.         case 71:
  81.           key = CR;
  82.           break;
  83.         case 82:
  84.           key = INSERT;
  85.           break;
  86.         case 83:
  87.           key = DELETE;
  88.           break;
  89.       }
  90.     }
  91.     switch(key)
  92.     {
  93.       case UP:
  94.       if (y>2) 
  95.         y--;
  96.       printf("\33[%d;%dH",y,x);
  97.       break;
  98.  
  99.       case DOWN:
  100.       if (y<23)
  101.         y++;
  102.       printf("\33[%d;%dH",y,x);
  103.       break;
  104.  
  105.       case LEFT:
  106.       case 8:
  107.       if (x>1) 
  108.         x--;
  109.       printf("\33[%d;%dH",y,x);
  110.       break;
  111.  
  112.       case RIGHT:
  113.       if (x<78)
  114.         x++;
  115.       printf("\33[%d;%dH",y,x);
  116.       break;
  117.  
  118.       case INSERT:
  119.         for(i=24;i>=y;i--)
  120.         {
  121.           memcpy(screen[i+1], screen[i], 79);
  122.         }
  123.         memset(screen[y], 32, 79);
  124.         screen[y][80] = 0;
  125.         display();
  126.       break;
  127.  
  128.       case DELETE:
  129.         for(i=y;i<25;i++)
  130.         {
  131.           memcpy(screen[i], screen[i+1], 79);
  132.         }
  133.         memset(screen[25], 32, 79);
  134.         screen[y][80] = 0;
  135.         display();
  136.       break;
  137.  
  138.       case 03:
  139.         return -1;
  140.       break;
  141.  
  142.       case ESC:
  143.         for(y=0;y<26;y++)
  144.         {
  145.           for(x=0;x<80;x++)
  146.           {
  147.             if(screen[y][x]==0)
  148.               screen[y][x]=32; 
  149.             if(screen[y][x]==13)
  150.               screen[y][x]=32; 
  151.           }
  152.           screen[y][80] = 0;
  153.         }
  154.         store();
  155.         printf("\nlength %d\nIncludes CR-LF\n", position + 2);
  156.  
  157.         return 0;
  158.       break;
  159.  
  160.       case CR:
  161.       if(y<23)
  162.         y++;
  163.       x = 1; 
  164.       printf("\33[%d;%dH",y,x);
  165.       break;
  166.  
  167.       case 10:
  168.       break;
  169.  
  170.       default:
  171.  
  172.       if(isprint(key))
  173.         if((x<78) && (y<24))
  174.         {
  175.           printf("\33[%d;%dH",y,x);
  176.           printf("%c",key);
  177.           screen[y][x] = key;
  178.           x++;
  179.         }
  180.       break;
  181.     }
  182.   }
  183. }
  184.  
  185.  
  186. store()
  187. {
  188. short x,y;
  189. FILE *fn;
  190. char edk;
  191.  
  192.   do
  193.     dataentry("Enter Filename",fname,"@@@@@@@@@@@@@@@");
  194.   while(fname[0] == 0);
  195.  
  196.   position = 0;
  197.   fn = fopen(fname,"w");
  198.   if(fn != 0)
  199.   {
  200.     for(y=1;y<26;y++)
  201.     {
  202.       parse(y,fn);
  203.     }
  204.   }
  205.   else
  206.     printf("file open error %d\n",fn);
  207.   fclose(fn);
  208. }
  209.  
  210.  
  211. parse(y,fn)
  212. short y;
  213. FILE *fn;
  214. {
  215. char *strng;
  216. char *strt;
  217. short new;
  218. char temp[82];
  219. short edk;
  220. short cnt;
  221. char pchr;
  222.  
  223.   edk = 32;
  224.   pchr = 0;
  225.   strng = &screen[y][0];
  226.  
  227.   cnt = 0;
  228.   while(*strng!=0)
  229.   {
  230.     while(*strng==' ')
  231.     {
  232.       strng++;
  233.       cnt++;
  234.     }
  235.  
  236.     switch(*strng)
  237.     {
  238.       case 0:
  239.         return;
  240.       break;
  241.       default:
  242.         fprintf(fn, "P%d\n%d\n", cnt, y);
  243.         fprintf(fn, "%d\n", position);
  244.         while(*strng!=' ')
  245.         {
  246.           if(*strng==0)
  247.             break;
  248.           if(*strng==13)
  249.             break;
  250.  
  251.           fputc((short) *strng, fn);
  252.  
  253.           if((*strng == '@') || (*strng == '!') || (*strng == '#') ||
  254.              (*strng == '|'))
  255.           {
  256.             position++;
  257.             pchr = 2;
  258.           }
  259.           if(*strng == '.')
  260.           {
  261.             if(pchr >= 1)
  262.               position++;
  263.           }
  264.           if(pchr > 0)
  265.             pchr--;
  266.  
  267.           cnt++;
  268.           strng++;
  269.         }
  270.         fputs("\n",fn);
  271.       break;
  272.     }
  273.   }
  274. }
  275.  
  276.  
  277. load()
  278. {
  279. short x,y, p;
  280. FILE *fd, *fopen();
  281. char temp[81];
  282.  
  283.   fname[0] = 0;
  284.   do
  285.   {
  286.     dataentry("Enter Filename",fname,"@@@@@@@@@@@@@@@");
  287.     printf("\33[2J");
  288.     if(fname[0]==0)
  289.       return;
  290.     fd = fopen(fname,"r");
  291.   }
  292.   while(fd==-1);
  293.  
  294.   if(fd != 0)
  295.   {
  296.     do
  297.     {
  298.       p = 0;
  299.       if(fgets(temp, 5, fd)==0)
  300.         break;
  301.  
  302.       if(temp[0] =='P')
  303.       {
  304.         x = atoi(&temp[1]);
  305.         p = 1;
  306.       }
  307.       else
  308.       {
  309.         x = atoi(temp);
  310.         p = 0;
  311.       }
  312.  
  313.       if(fgets(temp, 5, fd)==0)
  314.         break;
  315.       y = atoi(temp);
  316.  
  317.       if(p == 1)
  318.       {
  319.         if(fgets(temp, 5, fd)==0)
  320.           break;
  321.         p = atoi(temp);
  322.       }
  323.       if(fgets(&screen[y][x], 81, fd)==0)
  324.         break;
  325.     }
  326.     while(1);
  327.     display();
  328.     fclose(fd);
  329.   }
  330.   for(y=1;y<26;y++)
  331.     for(x=1;x<80;x++)
  332.       if(screen[y][x] < 0x20)
  333.         screen[y][x] = 0x20;
  334. }
  335.  
  336.  
  337. display()
  338. {
  339. short y,x;
  340.  
  341.   printf("\33[2J");
  342.   for(y=1;y<26;y++)
  343.   {
  344.     for(x=1;x<79;x++)
  345.     {
  346.       if(screen[y][x]=='\n')
  347.         screen[y][x] = 32;
  348.       printf("\33[%d;%dH",y,x); 
  349.       putch((short) screen[y][x] & 0xff);
  350.     }
  351.   }
  352. }
  353.  
  354.  
  355. edchr(char s)
  356. {
  357.   if(s == ' ')
  358.     return 1;
  359.  
  360.   if(s == 0)
  361.     return 1;
  362.  
  363.   if(s == '#')
  364.     return 1;
  365.  
  366.   if(s == '@')
  367.     return 1;
  368.              
  369.   if(s == '|')
  370.     return 1;
  371.              
  372.   if(s == '!')
  373.     return 1;
  374.  
  375.   return 0;
  376. }
  377.  
  378.