home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / clib / progs / haswinlib / c / general < prev    next >
Encoding:
Text File  |  1991-02-04  |  9.2 KB  |  296 lines

  1. /* > $.CLIB.C.general
  2.  *
  3.  *      HASWIN Graphics Library
  4.  *     =========================
  5.  *
  6.  *      Copyright (C) H.A.Shaw 1990.
  7.  *              Howard A. Shaw.
  8.  *              The Unit for Space Sciences,
  9.  *              Room 165,
  10.  *              Physics Building,
  11.  *              University of Kent at Canterbury.
  12.  *              Canterbury.
  13.  *              Kent.  CT2 7NJ
  14.  *      You may use and distribute this code freely, however please leave
  15.  *      it alone.  If you find bugs (and there will be many) please contact
  16.  *      me and the master source can be modified.  If you keep me informed
  17.  *      of who you give copies of this to then I can get release upgrades
  18.  *      to them.
  19.  *
  20.  *     general routines to do things at global levels.
  21.  */
  22. #include "includes.h"
  23.  
  24. /*
  25.  *      scan string 'str' and put '\0' at first non printable.
  26.  *      returns length.
  27.  */
  28. int asciilen(char *str) {
  29.  
  30.         register int    i = 0;
  31.  
  32.         if (!str)
  33.                 return(0);
  34.         while (*str >= ' ') {
  35.                 i++; str++;
  36.         }
  37.         *str = '\0';
  38.         return(i);
  39. }
  40.  
  41. /*
  42.  *      do the same as strncpy, but don't copy single quotes
  43.  */
  44. void strnqcpy(char *str1, char *str2, int max) {
  45.  
  46.         while ((*str2) && (max > 0)) {
  47.                 if (*str2 == '\'')
  48.                         str2++;
  49.                 else
  50.                       *(str1++) = *(str2++);
  51.                 max--;
  52.         }
  53.         *str1 = '\0';
  54. }
  55.  
  56. /*
  57.  *      do the same as strcpy, but don't copy single quotes
  58.  */
  59. void strqcpy(char *str1, char *str2) {
  60.  
  61.         while (*str2) {
  62.                 if (*str2 == '\'')
  63.                         str2++;
  64.                 else
  65.                       *(str1++) = *(str2++);
  66.         }
  67.         *str1 = '\0';
  68. }
  69.  
  70. /*
  71.  *      turn a string into a number.  The string is of the following form:
  72.  *      <spaces...>0-9*          - decimal
  73.  *      <spaces...>0[xX]0-9*     - hexadecimal
  74.  *      <spaces...>O0-9*         - octal
  75.  */
  76. int atoint(char *str) {
  77.  
  78.         int     state, value;
  79.  
  80.         value = state = 0;
  81.         while (*str == ' ')
  82.                 str++;
  83.         while (*str > ' ') {
  84.                 switch (state) {
  85.                 case 0: /* starting state */
  86.                         if ((*str >= '1') && (*str <= '9')) {
  87.                                 state = 3;
  88.                                 value = *str - '0';
  89.                                 break;
  90.                         }
  91.                         if (*str == '0') {
  92.                                 state = 1;
  93.                                 break;
  94.                         }
  95.                         if (*str == 'O') {
  96.                                 state = 2;
  97.                                 break;
  98.                         }
  99.                         return(0);
  100.                 case 1: /* if first character is '0' */
  101.                         if ((*str >= '0') && (*str <= '9')) {
  102.                                 state = 3;
  103.                                 value = *str - '0';
  104.                                 break;
  105.                         }
  106.                         if ((*str == 'x') || (*str == 'X')) {
  107.                                 state = 4;
  108.                                 break;
  109.                         }
  110.                         return(0);
  111.                 case 2: /* octal number state */
  112.                         if ((*str >= '0') && (*str <= '7')) {
  113.                                 value = value*8 + (*str - '0');
  114.                                 break;
  115.                         }
  116.                         return(value);
  117.                 case 3: /* decimal number state */
  118.                         if ((*str >= '0') && (*str <= '9')) {
  119.                                 value = value*10 + (*str - '0');
  120.                                 break;
  121.                         }
  122.                         return(value);
  123.                 case 4: /* hexadecimal number state */
  124.                         if ((*str >= '0') && (*str <= '9')) {
  125.                                 value = value*16 + (*str - '0');
  126.                                 break;
  127.                         }
  128.                         if ((*str >= 'a') && (*str <= 'f')) {
  129.                                 value = value*16 + 10 + (*str - 'a');
  130.                                 break;
  131.                         }
  132.                         if ((*str >= 'A') && (*str <= 'F')) {
  133.                                 value = value*16 + 10 + (*str - 'A');
  134.                                 break;
  135.                         }
  136.                         return(value);
  137.                 }
  138.                 str++;
  139.         }
  140.         return(value);
  141. }
  142.  
  143. int haswin_getversion() {
  144.         if (!(haswin_flags & HASWIN_FLAGS_STARTED))
  145.                 return(HASWIN_UNKNOWN);
  146.         return(haswin_version);
  147. }
  148.  
  149. int haswin_getflags() {
  150.         return(haswin_flags);
  151. }
  152.  
  153. int haswin_setflags(int new) {
  154.  
  155.         haswin_flags |= (new & HASWIN_INIT_USERFLAGS);
  156.         return(haswin_flags);
  157. }
  158.  
  159. int haswin_clearflags(int new) {
  160.  
  161.         haswin_flags &= ~(new & HASWIN_INIT_USERFLAGS);
  162.         return(haswin_flags);
  163. }
  164.  
  165. /*
  166.  *      initialise the window managment system.
  167.  */     
  168. int haswin_initialise(char *name, int flags, int mem) {
  169.  
  170.         _kernel_swi_regs  regs;
  171.  
  172. /* initialise internal list pointers */
  173.         haswin_baricons = (icon *)0;
  174.         haswin_topwindow = (window *)0;
  175.         memset((char *)&haswin_menu, 0, sizeof(menu));
  176. /*
  177.  *      find out about the memory limits and regions.
  178.  */
  179.         if (mem > 0)
  180.                 regs.r[0] = mem;
  181.         else
  182.                 regs.r[0] = -1;
  183.         regs.r[1] = -1;
  184.         if (!haswin_swi(HASWIN_Slot_size, ®s))
  185.                 return(HASWIN_FALSE);
  186. /* get the virtual address of the byte past the top of the program, the
  187.    time of program start, and the command line string */
  188.         haswin_swi(OS_GetEnv, ®s);
  189.         haswin_commline = (char *)(regs.r[0]);
  190.         haswin_programtop = haswin_getthisslotsize();
  191.         haswin_starttime = (int *)(regs.r[2]);
  192. /* initialise the malloc()/free() system */
  193.         if (!haswin_initmemorymanager())
  194.                 return(HASWIN_FALSE);
  195. /* start the WIMP and the HASWIN task manager */
  196.         if (!haswin_taskinitialise(name))
  197.                 return(HASWIN_FALSE);
  198.         haswin_flags |= HASWIN_FLAGS_STARTED | (flags&HASWIN_INIT_USERFLAGS);
  199. /* initialise the VDU variables */
  200.         haswin_updateallvduvariables(0);
  201. /* initialise window saving into sprites */
  202.         haswin_windowtosprite(0, 0, 0);
  203. /* get the error system going */
  204.         if (!haswin_initerrorhandlers())
  205.                 return(HASWIN_FALSE);
  206. /* get the HASWIN sprites */
  207.         haswin_addhaswinsprites(HASWIN_SPRITEFILE);
  208. /* initialise the pointer stack */
  209.         if (!haswin_initpointerinfo())
  210.                 return(HASWIN_FALSE);
  211.         return(haswin_version);
  212. }
  213.  
  214. /*
  215.  *      this returns the last state of the hourglass, so it can be restored
  216.  */
  217. int haswin_hourglass(int onoff) {
  218.  
  219. static  int     hourglass = 0;
  220.         int     tmp;
  221.         _kernel_swi_regs  regs;
  222.  
  223.         if (onoff > 1)
  224.                 onoff = 1;
  225.         else if (onoff < 0)
  226.                 onoff = 0;
  227.         switch (onoff) {
  228.         case 1: haswin_swi(HASWIN_Hourglass_on, ®s);
  229.                 break;
  230.         case 0: haswin_swi(HASWIN_Hourglass_off, ®s);
  231.                 break;
  232.         }
  233.         tmp = hourglass;
  234.         hourglass = onoff;
  235.         return(tmp);
  236. }
  237.  
  238. char *haswin_getcommandline(void) {
  239.         return(haswin_commline);
  240. }
  241.  
  242. int *haswin_getstarttime(void) {
  243.         return(haswin_starttime);
  244. }
  245.  
  246. /*
  247.  *      create a standard information window.  We do the following to
  248.  *      find a window.
  249.  *      1)      if a window called "information" is available use it.
  250.  *      2)      if "information" can be found in the currently open
  251.  *              template file use it.
  252.  *      3)      if "information" can be found in HASWINs own template file
  253.  *              use it.
  254.  *      We then setup icons 0-3 to be name, purpose, author and version of
  255.  *      the program.
  256.  */
  257. int haswin_setinfowindow(char *name, char *purpose, char *author, char *version) {
  258.         window  *win;
  259.         icon    *ic;
  260.         int     oldflags;
  261.  
  262.         if ((win=haswin_findwindowname("information")) == 0) {
  263.                 oldflags = haswin_flags & HASWIN_FLAGS_VERBOSE;
  264.                 haswin_flags &= ~HASWIN_FLAGS_VERBOSE;
  265.                 if ((win=haswin_loadwindow("information", 0)) == 0) {
  266.                         haswin_pushtemplate(HASWIN_TEMPLATEFILE);
  267.                         if ((win=haswin_loadwindow("information", 0)) == 0) {
  268.                                 haswin_poptemplate();
  269.                                 haswin_flags |= oldflags;
  270.                                 return(HASWIN_FALSE);
  271.                         }
  272.                         haswin_poptemplate();
  273.                 }
  274.                 haswin_flags |= oldflags;
  275.         }
  276.         if (name) {
  277.                 if ((ic=haswin_findiconhandle(win, 0)) != 0)
  278.                         haswin_seticondata(ic, name);
  279.         }
  280.         if (purpose) {
  281.                 if ((ic=haswin_findiconhandle(win, 1)) != 0)
  282.                         haswin_seticondata(ic, purpose);
  283.         }
  284.         if (author) {
  285.                 if ((ic=haswin_findiconhandle(win, 2)) != 0)
  286.                         haswin_seticondata(ic, author);
  287.         }
  288.         if (version) {
  289.                 if ((ic=haswin_findiconhandle(win, 3)) != 0)
  290.                         haswin_seticondata(ic, version);
  291.         }
  292.         haswin_flags |= HASWIN_FLAGS_INFOWIN;
  293.         return(HASWIN_TRUE);
  294. }
  295.  
  296.