home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / opengl / utilities / xglinfo / stringConversion.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  8.8 KB  |  309 lines

  1. /*
  2.  * 
  3.  *           Copyright (c) Digital Equipment Corporation, 1993
  4.  * 
  5.  *                          All Rights Reserved
  6.  * 
  7.  * Permission to use, copy, modify, and distribute  this software and its
  8.  * documentation for any  purpose   and without fee  is  hereby  granted,
  9.  * provided that the above copyright notice appear in all copies and that
  10.  * both  that  copyright  notice  and  this  permission notice appear  in
  11.  * supporting documentation, and that the name of Digital  not be used in
  12.  * advertising or publicity  pertaining to distribution  of the  software
  13.  * without specific, written prior permission.
  14.  * 
  15.  * DIGITAL DISCLAIMS   ALL  WARRANTIES WITH   REGARD   TO  THIS SOFTWARE,
  16.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  17.  * EVENT   SHALL  DIGITAL  BE   LIABLE  FOR  ANY SPECIAL,   INDIRECT   OR
  18.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  19.  * USE, DATA OR PROFITS, WHETHER IN AN ACTION  OF CONTRACT, NEGLIGENCE OR
  20.  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  21.  * PERFORMANCE OF THIS SOFTWARE.
  22.  * 
  23.  */
  24. /*
  25.  * HISTORY
  26.  * $Log: stringConversion.c,v $
  27.  * Revision 1.1.2.3  1993/06/18  15:02:50  John_Dennis
  28.  *     change copyright token to free copyright token
  29.  *     [1993/06/18  14:59:28  John_Dennis]
  30.  *
  31.  * Revision 1.1.2.2  1993/06/16  20:33:32  John_Dennis
  32.  *     various lint clean ups
  33.  *     [1993/06/16  20:13:37  John_Dennis]
  34.  * 
  35.  *     Initial Version by John Dennis
  36.  *     [1993/06/15  20:07:21  John_Dennis]
  37.  * 
  38.  * $EndLog$
  39.  */
  40. /*****************************************************************************/
  41. /******************************** Documentation ******************************/
  42. /*****************************************************************************/
  43.  
  44. /*****************************************************************************/
  45. /******************************* Include Files *******************************/
  46. /*****************************************************************************/
  47.  
  48. #include <stdio.h>
  49. #include <string.h>
  50. #include <stdlib.h>
  51. #include <X11/Xlib.h>
  52. #include <X11/Xutil.h>
  53. #include <GL/gl.h>
  54. #include <GL/glx.h>
  55. #include "global.h"
  56.  
  57. /*****************************************************************************/
  58. /****************************** Internal Defines *****************************/
  59. /*****************************************************************************/
  60.  
  61. /*****************************************************************************/
  62. /************************** Internal Type Definitions ************************/
  63. /*****************************************************************************/
  64.  
  65. typedef struct {
  66.   long Key;
  67.   char *String;
  68. } NumberedStrings;
  69.  
  70.  
  71. /*****************************************************************************/
  72. /**********************  External Function Declarations  *********************/
  73. /*****************************************************************************/
  74.  
  75. /*****************************************************************************/
  76. /**********************  Internal Function Declarations  *********************/
  77. /*****************************************************************************/
  78.  
  79. /*****************************************************************************/
  80. /*************************  External Global Variables  ***********************/
  81. /*****************************************************************************/
  82.  
  83. /*****************************************************************************/
  84. /*************************  Internal Global Variables  ***********************/
  85. /*****************************************************************************/
  86.  
  87. /*****************************************************************************/
  88. /***************************  Internal Functions  ****************************/
  89. /*****************************************************************************/
  90.  
  91. static void
  92. HeapSort(
  93. long     n,
  94. NumberedStrings x[]
  95. )
  96. {
  97.   long i;            /* array index */
  98.   long j;            /* array index */
  99.   long k;            /* array index */
  100.   NumberedStrings y;        /* temp element for swapping */
  101.  
  102.   /*
  103.    * heap sort only works if n is greater than or equal to 3, so if n < 3
  104.    * sort trivally
  105.    */
  106.   if (n == 1)
  107.     return;
  108.   if (n == 2) {
  109.     if (x[0].Key > x[1].Key) {
  110.       y = x[0];
  111.       x[0] = x[1];
  112.       x[1] = y;
  113.     }
  114.     return;
  115.   }
  116.   /* create initial heap */
  117.   for (k = 1; k < n; k++) {
  118.     /* insert x[k] into existing heap of size k-1 */
  119.     i = k;
  120.     y = x[k];        /* y is node to insert */
  121.     j = (i - 1) / 2;        /* j is the father of i */
  122.     while (i >= 1) {
  123.       if (y.Key <= x[j].Key)
  124.     break;
  125.       x[i] = x[j];
  126.       i = j;
  127.       j = (i - 1) / 2;
  128.     }
  129.     x[i] = y;
  130.   }
  131.  
  132.   /*
  133.    * We remove x[0] and place it in its proper position in the array. We then
  134.    * adjust the heap
  135.    */
  136.   for (k = n - 1; k > 0; k--) {
  137.     y = x[k];
  138.     x[k] = x[0];
  139.     /*
  140.      * Readjust the heap of order k-1. Move y down the heap for proper
  141.      * position
  142.      */
  143.     i = 0;
  144.     j = 1;
  145.     if ((k - 1 >= 2) && (x[2].Key > x[1].Key))
  146.       j = 2;
  147.     /* j is the larger son of i in the heap of size k-1 */
  148.     while (j <= k - 1) {
  149.       if (x[j].Key <= y.Key)
  150.     break;
  151.       x[i] = x[j];
  152.       i = j;
  153.       j = (2 * i) + 1;
  154.       if (j + 1 <= k - 1)
  155.     if (x[j + 1].Key > x[j].Key)
  156.       j++;
  157.     }
  158.     x[i] = y;
  159.   }
  160. }
  161.  
  162.  
  163. static char *BinarySearch (
  164.   long Key,
  165.   NumberedStrings *Table,
  166.   long TableSize
  167. )
  168. {
  169.   long Low = 0;
  170.   long High = TableSize - 1;
  171.   long Mid;
  172.   static char ErrString[80];
  173.  
  174.   while (Low <= High) {
  175.     Mid = (Low + High) / 2;
  176.     if (Key == Table[Mid].Key) return (Table[Mid].String);
  177.     if (Key < Table[Mid].Key)
  178.       High = Mid - 1;
  179.     else
  180.       Low = Mid + 1;
  181.   }
  182.   sprintf(ErrString, "Key Unknown %d", Key);
  183.   return (ErrString);
  184. }
  185.  
  186. /*****************************************************************************/
  187. /****************************  Exported Functions  ***************************/
  188. /*****************************************************************************/
  189.  
  190. char *VisualClassName(
  191.   long Key
  192. )
  193. {
  194.   static NumberedStrings Names[] = {
  195.     {StaticGray,    "StaticGray"},
  196.     {GrayScale,        "GrayScale"},
  197.     {StaticColor,    "StaticColor"},
  198.     {PseudoColor,    "PseudoColor"},
  199.     {TrueColor,        "TrueColor"},
  200.     {DirectColor,    "DirectColor"},
  201.   };
  202.   static int Sorted = 0;    /* not sorted yet */
  203.   static const long N = sizeof(Names) / sizeof(NumberedStrings);
  204.  
  205.   if (!Sorted) {
  206.     Sorted = 1;
  207.     HeapSort(N, Names);
  208.   }
  209.   return(BinarySearch(Key, Names, N));
  210. }
  211.  
  212. char *EventMaskName(
  213.   long Key
  214. )
  215. {
  216.   static NumberedStrings Names[] = {
  217.     {NoEventMask,        "NoEvent"},
  218.     {KeyPressMask,        "KeyPress"},
  219.     {KeyReleaseMask,        "KeyRelease"},
  220.     {ButtonPressMask,        "ButtonPress"},
  221.     {ButtonReleaseMask,        "ButtonRelease"},
  222.     {EnterWindowMask,        "EnterWindow"},
  223.     {LeaveWindowMask,        "LeaveWindow"},
  224.     {PointerMotionMask,        "PointerMotion"},
  225.     {PointerMotionHintMask,    "PointerMotionHint"},
  226.     {Button1MotionMask,        "Button1Motion"},
  227.     {Button2MotionMask,        "Button2Motion"},
  228.     {Button3MotionMask,        "Button3Motion"},
  229.     {Button4MotionMask,        "Button4Motion"},
  230.     {Button5MotionMask,        "Button5Motion"},
  231.     {ButtonMotionMask,        "ButtonMotion"},
  232.     {KeymapStateMask,        "KeymapState"},
  233.     {ExposureMask,        "Exposure"},
  234.     {VisibilityChangeMask,    "VisibilityChange"},
  235.     {StructureNotifyMask,    "StructureNotify"},
  236.     {ResizeRedirectMask,    "ResizeRedirect"},
  237.     {SubstructureNotifyMask,    "SubstructureNotify"},
  238.     {SubstructureRedirectMask,    "SubstructureRedirect"},
  239.     {FocusChangeMask,        "FocusChange"},
  240.     {PropertyChangeMask,    "PropertyChange"},
  241.     {ColormapChangeMask,    "ColormapChange"},
  242.     {OwnerGrabButtonMask,    "OwnerGrabButton"},
  243.   };
  244.   static int Sorted = 0;    /* not sorted yet */
  245.   static const long N = sizeof(Names) / sizeof(NumberedStrings);
  246.  
  247.   if (!Sorted) {
  248.     Sorted = 1;
  249.     HeapSort(N, Names);
  250.   }
  251.   return(BinarySearch(Key, Names, N));
  252. }
  253.  
  254. char *ByteOrderName(
  255.   long Key
  256. )
  257. {
  258.   static NumberedStrings Names[] = {
  259.     {LSBFirst,    "LSBFirst"},
  260.     {MSBFirst,    "MSBFirst"},
  261.   };
  262.   static int Sorted = 0;    /* not sorted yet */
  263.   static const long N = sizeof(Names) / sizeof(NumberedStrings);
  264.  
  265.   if (!Sorted) {
  266.     Sorted = 1;
  267.     HeapSort(N, Names);
  268.   }
  269.   return(BinarySearch(Key, Names, N));
  270. }
  271.  
  272. char *WindowFocusName(
  273.   long Key
  274. )
  275. {
  276.   static NumberedStrings Names[] = {
  277.     {None,        "None"},
  278.     {PointerRoot,    "PointerRoot"},
  279.   };
  280.   static int Sorted = 0;    /* not sorted yet */
  281.   static const long N = sizeof(Names) / sizeof(NumberedStrings);
  282.  
  283.   if (!Sorted) {
  284.     Sorted = 1;
  285.     HeapSort(N, Names);
  286.   }
  287.   return(BinarySearch(Key, Names, N));
  288. }
  289.  
  290. char *WindowFocusRevertName(
  291.   long Key
  292. )
  293. {
  294.   static NumberedStrings Names[] = {
  295.     {RevertToNone,        "RevertToNone"},
  296.     {RevertToPointerRoot,    "RevertToPointerRoot"},
  297.     {RevertToParent,        "RevertToParent"},
  298.   };
  299.   static int Sorted = 0;    /* not sorted yet */
  300.   static const long N = sizeof(Names) / sizeof(NumberedStrings);
  301.  
  302.   if (!Sorted) {
  303.     Sorted = 1;
  304.     HeapSort(N, Names);
  305.   }
  306.   return(BinarySearch(Key, Names, N));
  307. }
  308.  
  309.