home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 635.lha / mini / mini.c < prev    next >
C/C++ Source or Header  |  1990-10-28  |  4KB  |  152 lines

  1. /* The routines in this file are copyright © 1990 by Lorenzo Zago.
  2.  * Permission is granted for use and free distribution as long as the
  3.  * original author's name is included with the code.
  4.  * These routines may be modified for improvements and adaptations as
  5.  * long as:
  6.  * - credits for the original ideas and code are given to the original
  7.  *   author;
  8.  * - the modified program is made freely available in the PD distribution;
  9.  * - a copy of the new source code and executables are sent to L. Zago.
  10.  *
  11.  * Compiled with Lattice C 4.0
  12.  *  LC:lc -M -r -v -b mini.c
  13.  * Linked as
  14.  *  Blink LIB:c.o+_main.o+mini.o+Sprintf.o TO mini SC SD ND LIB amiga.lib+lc.lib */
  15. #include <libraries/dosextens.h>
  16.  
  17. #include <exec/memory.h>
  18. #include <intuition/intuitionbase.h>
  19. #include <intuition/intuition.h>
  20. #include <workbench/startup.h>
  21.  
  22. #define max(a,b) ((a)>(b)?(a):(b))
  23. #define min(a,b) ((a)<=(b)?(a):(b))
  24.  
  25. #define ACTIVESCREEN       IntuitionBase->ActiveScreen
  26. #define ACTIVESCREENWIDTH  IntuitionBase->ActiveScreen->Width
  27. #define ACTIVESCREENHEIGHT IntuitionBase->ActiveScreen->Height
  28.  
  29. struct IntuitionBase *IntuitionBase;
  30. struct Window *wind ;
  31.  
  32. char *lines[70],*strchr();
  33. int oldlock;
  34. struct FileHandle *fp,*fc,*Open(),*fo;
  35.  
  36. VOID OpenLibs(), MemCleanup(), MiniWindow(),
  37.      Finish();
  38.  
  39. void main(argc,argv)
  40. int argc;
  41. char **argv;
  42. {   char *buf,*ib,*eb; UBYTE qc=0;
  43.     long i,lin=1,len=1,slen[70];
  44.     int ic=0,maxlin,maxcol,tc,ms,id=0;
  45.     short x,y,w,h;
  46.     struct WBStartup *argmsg; /* *WBenchMsg;  */
  47.     struct WBArg *arg;
  48.  
  49.     IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",0);
  50.     if (IntuitionBase == NULL)  exit(FALSE);
  51.     fo=Output();
  52.     if (argc > 0)
  53.       { if((argc != 2) || !strcmp(argv[1],"?"))
  54.          { fprintf(fo,"Mini ©1990 L.Zago \n Usage: %s <filename>\n",argv[0]);
  55.            CloseLibrary( IntuitionBase); exit(20); }
  56.         ib=argv[1];
  57.       }
  58.     else
  59.       { argmsg = (struct WBStartup *)argv;
  60.         arg= argmsg->sm_ArgList; arg++;
  61.         ib=arg->wa_Name; oldlock=CurrentDir (arg->wa_Lock); }
  62.  
  63.     if ((fp=Open(ib,MODE_OLDFILE))==0)
  64.       { fprintf (fo,"Problems opening file %s \n",ib);
  65.         CloseLibrary( IntuitionBase); exit(20);        }
  66.  
  67.     maxlin = (ACTIVESCREENHEIGHT-13)/8-1;
  68.     maxcol = (ACTIVESCREENWIDTH-20) /8;
  69.     tc=maxlin*maxcol; ms=tc+2;
  70.     buf = (char *)AllocMem (ms,MEMF_CLEAR);
  71.     tc = Read (fp,buf,tc); ib=buf;
  72.     while (ic<20)
  73.       { if ((*ib==0) || ((*ib>127) && (*ib<160)))
  74.           { ic=MessageWindow ("File contain binaries ...");
  75.             Finish(); }
  76.         ib++; ic++;
  77.       }
  78.     ib=buf; lines[1]=ib;
  79.     eb=buf+tc; *eb=0;
  80.     eb++; *eb=0;
  81.     while ((ib < eb) && (lin <= maxlin+1))
  82.       { ib=strchr (ib,10); if (ib==0) break;
  83.         *ib = 0;
  84.         slen[lin]=strlen(lines[lin]);
  85.         len=max(len,slen[lin]);
  86.         ib++; if (*ib == 0) break;
  87.         lin=lin+1; lines[lin]=ib;
  88.       }
  89.     while (slen[lin]==0) lin--;
  90.     if (lin > maxlin)
  91.       { ic=MessageWindow ("File has too many lines ...");
  92.         if (ic == 113) Finish(); else lin=maxlin; id=1; }
  93.     if (len > maxcol)
  94.       { ic=MessageWindow ("File has too long lines ...");
  95.         if (ic == 113) Finish();  else len=maxcol; id=1; }
  96.     w=len*8+14; h=(lin+id)*8+13;
  97.  
  98.     x=Seed()*(ACTIVESCREENWIDTH-w)/3000;
  99.     y=Seed()*(ACTIVESCREENHEIGHT-h)/3000;
  100.     MiniWindow (x,y,w,h);
  101.     for (i=1;i<lin;i++) fprintf (fc,"%s\n",lines[i]);
  102.     fprintf (fc,"%s",lines[lin]);
  103.     while (qc != 113)
  104.      { i=WaitForChar (fc,1000000000L);
  105.        Read (fc,&qc,1);
  106.      }
  107.     if (buf) FreeMem (buf,ms);
  108.     Finish();
  109.  }
  110.  
  111. VOID Finish()
  112.  {  if (fp) Close (fp);
  113.     if (fc) Close (fc);
  114.     if (oldlock) CurrentDir (oldlock);
  115.     if (IntuitionBase) CloseLibrary( IntuitionBase);
  116.     exit(0);
  117.  }
  118.  
  119. Seed()
  120.  { long c[3];
  121.    DateStamp(&c[0]);
  122.    return (c[2]);
  123.  }
  124.  
  125. MessageWindow(text)
  126.   char *text;
  127.  { short x,y,w,h; int i; UBYTE ic;
  128.  
  129.    x=160; y=100; w=392; h=29;
  130.    MiniWindow (x,y,w,h);
  131.    fprintf (fc,"%s\n",text);
  132.    i= WaitForChar (fc,1000000000L);
  133.    Read (fc,&ic,1);
  134.    return (ic);
  135.  }
  136.  
  137. VOID MiniWindow (x,y,w,h)
  138.   short x,y,h,w;
  139.  { char con[100];
  140.    w=w+8;
  141.    if (wind == 0)
  142.      { Sprintf (&con[0],"RAW:%ld/%ld/%ld/%ld/Mini ©1990 L.Zago",x,y,w,h);
  143.        fc = Open (con,1005L);
  144.        wind = IntuitionBase->ActiveWindow;
  145.      }
  146.    else { MoveWindow (wind,x-wind->LeftEdge,y-wind->TopEdge);
  147.           SizeWindow (wind,w-wind->Width,h-wind->Height);
  148.           Delay(20);   }
  149.  }
  150.  
  151. VOID MemCleanup(){}
  152.