Advertisement
Guest User

very basic HTML editor

a guest
Jun 29th, 2021
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.63 KB | None | 0 0
  1. /*  u,h,j,k zum aendern der Position des Felds, x,y,a,s, zum aendern der Groesse,
  2.     n um das Element einzufuegen (es wird eine HTML-Tabelle generiert), anschliessend
  3.     enthaltenen Text eintippen. q zum Speichern in Datei posdata
  4.      das #define unten auskommentieren und es generiert das andere Programm.
  5.      Im selben Verzeichnis liest es posdata und man kann die Ausgabe direkt in eine
  6.      zu erzeugende HTML-Datei leiten.
  7.      Beim Editor Konsolenfenster groß stellen (so 62x42).
  8.       Tabellen scheinen eine Moeglichkeit zu sein. Ansonsten ist die Formatierung mehr
  9.       als vertrackt oder unmoeglich.
  10.       */
  11.  
  12. #define COMPILE_EDITOR
  13.  
  14. #ifdef COMPILE_EDITOR
  15.  
  16. #include <stdio.h>
  17. #include <fcntl.h>
  18. #include <stdlib.h>
  19.  
  20. struct
  21. {
  22.  int x,y,xdim,ydim;
  23. }boxes[100];
  24.  
  25.  unsigned char htmltext[200*100];
  26.  
  27.  
  28. int generate(int n)
  29. {
  30.   unsigned char generatestring[200];
  31.   if ( boxes[n].xdim==0||boxes[n].ydim==0) return 0;
  32.   printf("(F) zum Fortfahren");
  33.   if ( toupper(getch())!='F') return 0;
  34.   unsigned char x[5],y[5];
  35.  
  36.   unsigned char contentstring[32];
  37.   printf("\nBitte Text eingeben:");
  38.   gets(contentstring);
  39.  
  40.   itoa(boxes[n].xdim*10,x,10);
  41.   itoa(boxes[n].ydim*10,y,10);
  42.  
  43.  
  44.   strcpy(generatestring,"<table align=left height="),
  45.   strcat(generatestring,x),strcat(generatestring," width="),
  46.   strcat(generatestring,y),strcat(generatestring,">");
  47.   strcat(generatestring,contentstring),strcat(generatestring,"</table>");
  48.  
  49.  
  50.   strcpy(&(htmltext[200*n]),generatestring);
  51.    
  52.     return 1;
  53.    
  54. }
  55.  
  56. signed int boxptr=0;
  57.  
  58. int main(void)
  59. {
  60.   enum
  61.   {
  62.    up,down,left,right,grewx,grewy
  63.   }LASTSTEP;
  64.  
  65.  unsigned char screen[60][40],collide[60][40];
  66.   int x,y;
  67.    int n=0;
  68.    y=0;
  69.    while ( y<40 )
  70.    {
  71.     x=0;
  72.     while ( x<60 ) screen[x][y]=collide[x][y]=0,x++;     y++;
  73.    }
  74.  
  75.    boxes[n].x=boxes[n].y=boxes[n].xdim=boxes[n].ydim=0;  
  76.  
  77.    while (1)
  78.    {
  79.     if ( kbhit())
  80.     {
  81.      switch(getch())
  82.      {
  83.       case 'x': if ( boxes[boxptr].xdim<15&&boxes[boxptr].x+boxes[boxptr].xdim<60)
  84.         boxes[boxptr].xdim++; LASTSTEP=grewx; break;
  85.       case 'y':if ( boxes[boxptr].ydim<15&&boxes[boxptr].y+boxes[boxptr].ydim<39)
  86.         boxes[boxptr].ydim++;  LASTSTEP=grewy;break;
  87.       case 's': if ( boxes[boxptr].xdim>0)
  88.         boxes[boxptr].xdim--;  break;
  89.    case 'a': if ( boxes[boxptr].ydim>0)
  90.         boxes[boxptr].ydim--;  break;
  91.    case 'u': if ( boxes[boxptr].y>0)
  92.          boxes[boxptr].y--; LASTSTEP=up;break;
  93.   case 'j': if ( boxes[boxptr].y+boxes[boxptr].ydim<39)
  94.          boxes[boxptr].y++;LASTSTEP=down; break;
  95.   case 'h': if ( boxes[boxptr].x>0)
  96.          boxes[boxptr].x--; LASTSTEP=left;break;
  97.   case 'k': if ( boxes[boxptr].x+boxes[boxptr].xdim<59)
  98.          boxes[boxptr].x++; LASTSTEP=right;break;
  99.   case 'n': if(generate(boxptr)==1) {
  100.            boxptr++;   boxes[n].x=boxes[n].y=boxes    [n].xdim=boxes[n].ydim=0; }
  101.      break;
  102.  
  103.   case 'q': goto writeout;
  104.      }
  105.     }
  106.  
  107.         y=0;
  108.    while ( y<40 )
  109.    {
  110.     x=0;
  111.     while ( x<60 ) screen[x][y]=' ',collide[x][y]=255,x++;
  112.     y++;
  113.    }
  114.    screen[boxes[boxptr].x][boxes[boxptr].y]='X';
  115.  
  116.    n=0;
  117.    while ( n < boxptr+1)
  118.    {
  119.     if ( n==boxptr&&boxes[n].xdim==0&&boxes[n].ydim==0)break;
  120.     int dimc_x,dimc_y;
  121.      
  122.      dimc_y=boxes[n].ydim;
  123.  
  124.      while ( dimc_y>=0)
  125.      {      
  126.        dimc_x=boxes[n].xdim;      
  127.  while ( dimc_x >= 0 )
  128.  {
  129.  
  130.     if ( collide[boxes[n].x+dimc_x][boxes[n].y+dimc_y]==255||collide[boxes[n].x+dimc_x][boxes[n].y+dimc_y]==n)
  131.      screen[boxes[n].x+dimc_x][boxes[n].y+dimc_y]=177,
  132.      collide[boxes[n].x+dimc_x][boxes[n].y+dimc_y]=n;
  133.      else
  134.      {
  135.      printf("\a");
  136.       switch(LASTSTEP)
  137.       {
  138.        case up: boxes[n].y++;break;
  139.        case down:boxes[n].y--;break;
  140.        case left:boxes[n].x++;break;
  141.        case right:boxes[n].x--;break;
  142.        case grewx: boxes[n].xdim--; break;
  143.        case grewy: boxes[n].ydim--; break;
  144.       }
  145.       dimc_x=0;
  146.       dimc_y=boxes[n].ydim+1;
  147.       goto repeat;
  148.      }
  149.  
  150.     dimc_x--;
  151.     }
  152.      repeat:
  153.        dimc_y--;
  154.       }    
  155.  
  156.     n++;  
  157.    }      
  158.  
  159.    system("cls");
  160.    y=0;
  161.    while ( y < 40)
  162.    {
  163.     x=0;
  164.     while ( x<60) printf("%c%c%c%c",screen[x][y],screen[x+1][y],screen[x+2][y],screen[x+3][y]),x+=4;
  165.      printf("\n");
  166.     y++;
  167.    }
  168.    
  169.    
  170.    }
  171.  
  172.   writeout:
  173.    
  174.     (0);
  175.     FILE *output=fopen("posdata","wb");
  176.       fwrite(boxes, sizeof(boxes),1,output);
  177.       fwrite(htmltext, sizeof(htmltext),1,output);
  178.        fwrite(&boxptr,sizeof(signed int),1,output);
  179.     fclose(output);
  180.  
  181. }
  182.  
  183. #else
  184.  
  185.  
  186. #include <stdio.h>
  187. #include <fcntl.h>
  188.  
  189.  
  190. #define XSPACING 1
  191. #define YSPACING 2
  192.  
  193. struct
  194. {
  195.  int x,y,xdim,ydim;
  196. }boxes[100],swap;
  197.  
  198. unsigned char htmltext[200*100], htmlswap[200];
  199.  
  200. signed int boxc=0;
  201.  
  202.  
  203. void sort_boxes(int num)
  204. {
  205.  int n=0,n2=0;
  206.   n=0;
  207.  while ( n<num-1)
  208.  {
  209.     n2=num-1;
  210.     while ( n2>n)
  211.     {
  212.  
  213.      if ( boxes[n2].x+boxes[n2].y*60 < boxes[n2-1].x+boxes[n2-1].y*60 )
  214.      {
  215.       swap.x=boxes[n2].x;
  216.       swap.y=boxes[n2].y;
  217.       swap.xdim=boxes[n2].xdim;
  218.       swap.ydim=boxes[n2].ydim;
  219.        strcpy(htmlswap,&(htmltext[n2*200]));
  220.       boxes[n2].x=boxes[n2-1].x;
  221.       boxes[n2].y=boxes[n2-1].y;
  222.       boxes[n2].xdim=boxes[n2-1].xdim;
  223.       boxes[n2].ydim=boxes[n2-1].ydim;
  224.         strcpy(&(htmltext[n2*200]),&(htmltext[(n2-1)*200]));
  225.       boxes[n2-1].x=swap.x;
  226.       boxes[n2-1].y=swap.y;
  227.       boxes[n2-1].xdim=swap.xdim;  
  228.       boxes[n2-1].ydim=swap.ydim;  
  229.          strcpy(&(htmltext[(n2-1)*200]),htmlswap);
  230.        }
  231.         n2--;
  232.      
  233.      }
  234.      n++;  
  235.      } 
  236.    
  237.    
  238. }
  239.  
  240.  
  241. int main(void)
  242. {
  243.     int x_off=0,y_off=0;
  244.     int old_x=0,old_y=0;
  245.  
  246.     FILE *input=fopen("posdata","rb");
  247.       fread(boxes, sizeof(boxes),1/*sizeof(boxes[0]),boxc+1*/,input);
  248.          fread(htmltext, sizeof(htmltext),1/*sizeof(boxes[0]),boxc+1*/,input);
  249.          fread(&boxc,sizeof(signed int),1,input);
  250.     fclose(input);
  251.      
  252.      int n=0;
  253.      
  254.            
  255.      sort_boxes(boxc);
  256.          
  257.             printf("<html>\n<body>\n");
  258.             n=0;
  259.            
  260.       int x=0,y=0;
  261.       y=0;
  262.      while (y<40)
  263.      {
  264.       x=0;
  265.       while(x<60)
  266.       {
  267.         if ( x==boxes[n].x&& y==boxes[n].y)
  268.         {
  269.             while ( (y+y_off-old_y)/YSPACING >0)old_y+=YSPACING,printf("<br>\n",YSPACING);
  270.             while ( (x+x_off-old_x)/XSPACING >0)old_x+=XSPACING,printf("&nbsp",XSPACING);
  271.          printf("%s",&htmltext[200*n]);
  272.             old_x=boxes[n].x,old_y=boxes[n].y;
  273.            
  274.             x_off+=x-old_x-boxes[n].xdim%XSPACING;
  275.             y_off+=y-old_y-boxes[n].ydim%YSPACING;
  276.              
  277.             n++; if ( n==boxc+1) goto end;
  278.           }
  279.         x++;
  280.       }
  281.       old_x=0;
  282.       x_off=0;
  283.       printf("<br>"); old_y+=YSPACING;
  284.       y++;
  285.      
  286.    }
  287.    
  288.    end: printf("</body></html>");
  289. }
  290.  
  291. #endif
  292.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement