"Fossies" - the Fresh Open Source Software Archive

Member "epstool-3.08/srcwin/wgsver.c" (10 Jun 2005, 8735 Bytes) of package /linux/misc/old/ghost/ghostgum/epstool-3.08-os2.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /* Copyright (C) 2001-2003, Ghostgum Software Pty Ltd.  All rights reserved.
    2  *
    3  * Permission is hereby granted, free of charge, to any person
    4  * obtaining a copy of this software and associated documentation
    5  * files (the "Software"), to deal in the Software without
    6  * restriction, including without limitation the rights to use, copy,
    7  * modify, merge, publish, distribute, sublicense, and/or sell copies
    8  * of the Software, and to permit persons to whom the Software is
    9  * furnished to do so, subject to the following conditions:
   10  *
   11  * The above copyright notice and this permission notice shall be
   12  * included in all copies or substantial portions of the Software.
   13  *
   14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
   17  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
   18  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
   19  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
   21  * SOFTWARE.
   22  */
   23 
   24 /* 
   25  * This file is part of GSview.  The above notice applies only 
   26  * to this file.  It does NOT apply to any other file in GSview
   27  * unless that file includes the above copyright notice.
   28  */
   29 
   30 /* $Id: wgsver.c,v 1.1.2.4 2003/07/14 08:03:09 ghostgum Exp $ */
   31 /* Obtain details of copies of Ghostscript installed under Windows */
   32 
   33 /* To compile as a demo program, define DUMP_GSVER */
   34 /* #define DUMP_GSVER */
   35 
   36 #include <windows.h>
   37 #include <stdio.h>
   38 #include <stdio.h>
   39 #include <stdlib.h>
   40 #include "wgsver.h"
   41 
   42 /* Ghostscript may be known in the Windows Registry by
   43  * the following names.
   44  */
   45 #define GS_PRODUCT_AFPL "AFPL Ghostscript"
   46 #define GS_PRODUCT_ALADDIN "Aladdin Ghostscript"
   47 #define GS_PRODUCT_GPL "GPL Ghostscript"
   48 #define GS_PRODUCT_GNU "GNU Ghostscript"
   49 
   50 /* Get Ghostscript versions for given product.
   51  * Store results starting at pver + 1 + offset.
   52  * Returns total number of versions in pver.
   53  */
   54 static int get_gs_versions_product(int *pver, int offset, 
   55     const char *gs_productfamily)
   56 {
   57     HKEY hkey;
   58     DWORD cbData;
   59     HKEY hkeyroot;
   60     char key[256];
   61     int ver;
   62     char *p;
   63     int n = 0;
   64 
   65     sprintf(key, "Software\\%s", gs_productfamily);
   66     hkeyroot = HKEY_LOCAL_MACHINE;
   67     if (RegOpenKeyExA(hkeyroot, key, 0, KEY_READ, &hkey) == ERROR_SUCCESS) {
   68     /* Now enumerate the keys */
   69     cbData = sizeof(key) / sizeof(char);
   70     while (RegEnumKeyA(hkey, n, key, cbData) == ERROR_SUCCESS) {
   71         n++;
   72         ver = 0;
   73         p = key;
   74         while (*p && (*p!='.')) {
   75         ver = (ver * 10) + (*p - '0')*100;
   76         p++;
   77         }
   78         if (*p == '.')
   79         p++;
   80         if (*p) {
   81         ver += (*p - '0') * 10;
   82         p++;
   83         }
   84         if (*p)
   85         ver += (*p - '0');
   86         if (n + offset < pver[0])
   87         pver[n+offset] = ver;
   88     }
   89     }
   90     return n+offset;
   91 }
   92 
   93 /* Query registry to find which versions of Ghostscript are installed.
   94  * Return version numbers in an integer array.   
   95  * On entry, the first element in the array must be the array size 
   96  * in elements.
   97  * If all is well, TRUE is returned.
   98  * On exit, the first element is set to the number of Ghostscript
   99  * versions installed, and subsequent elements to the version
  100  * numbers of Ghostscript.
  101  * e.g. on entry {5, 0, 0, 0, 0}, on exit {3, 550, 600, 596, 0}
  102  * Returned version numbers may not be sorted.
  103  *
  104  * If Ghostscript is not installed at all, return FALSE
  105  * and set pver[0] to 0.
  106  * If the array is not large enough, return FALSE 
  107  * and set pver[0] to the number of Ghostscript versions installed.
  108  */
  109 BOOL get_gs_versions(int *pver)
  110 {
  111     int n;
  112     if (pver == (int *)NULL)
  113         return FALSE;
  114 
  115     n = get_gs_versions_product(pver, 0, GS_PRODUCT_AFPL);
  116     n = get_gs_versions_product(pver, n, GS_PRODUCT_ALADDIN);
  117     n = get_gs_versions_product(pver, n, GS_PRODUCT_GPL);
  118     n = get_gs_versions_product(pver, n, GS_PRODUCT_GNU);
  119 
  120     if (n >= pver[0]) {
  121     pver[0] = n;
  122     return FALSE;   /* too small */
  123     }
  124 
  125     if (n == 0) {
  126     pver[0] = 0;
  127     return FALSE;   /* not installed */
  128     }
  129     pver[0] = n;
  130     return TRUE;
  131 }
  132 
  133  
  134 /*
  135  * Get a named registry value.
  136  * Key = hkeyroot\\key, named value = name.
  137  * name, ptr, plen and return values are the same as in gp_getenv();
  138  */
  139 
  140 int 
  141 gp_getenv_registry(HKEY hkeyroot, const char *key, const char *name, 
  142     char *ptr, int *plen)
  143 {
  144     HKEY hkey;
  145     DWORD cbData, keytype;
  146     BYTE b;
  147     LONG rc;
  148     BYTE *bptr = (BYTE *)ptr;
  149 
  150     if (RegOpenKeyExA(hkeyroot, key, 0, KEY_READ, &hkey)
  151     == ERROR_SUCCESS) {
  152     keytype = REG_SZ;
  153     cbData = *plen;
  154     if (bptr == (BYTE *)NULL)
  155         bptr = &b;  /* Registry API won't return ERROR_MORE_DATA */
  156             /* if ptr is NULL */
  157     rc = RegQueryValueExA(hkey, (char *)name, 0, &keytype, bptr, &cbData);
  158     RegCloseKey(hkey);
  159     if (rc == ERROR_SUCCESS) {
  160         *plen = cbData;
  161         return 0;   /* found environment variable and copied it */
  162     } else if (rc == ERROR_MORE_DATA) {
  163         /* buffer wasn't large enough */
  164         *plen = cbData;
  165         return -1;
  166     }
  167     }
  168     return 1;   /* not found */
  169 }
  170 
  171 
  172 static BOOL get_gs_string_product(int gs_revision, const char *name, 
  173     char *ptr, int len, const char *gs_productfamily)
  174 {
  175     /* If using Win32, look in the registry for a value with
  176      * the given name.  The registry value will be under the key
  177      * HKEY_CURRENT_USER\Software\AFPL Ghostscript\N.NN
  178      * or if that fails under the key
  179      * HKEY_LOCAL_MACHINE\Software\AFPL Ghostscript\N.NN
  180      * where "AFPL Ghostscript" is actually gs_productfamily
  181      * and N.NN is obtained from gs_revision.
  182      */
  183 
  184     int code;
  185     char key[256];
  186     char dotversion[16];
  187     int length;
  188     DWORD version = GetVersion();
  189 
  190     if (((HIWORD(version) & 0x8000) != 0)
  191       && ((HIWORD(version) & 0x4000) == 0)) {
  192     /* Win32s */
  193     return FALSE;
  194     }
  195 
  196 
  197     sprintf(dotversion, "%d.%02d", 
  198         (int)(gs_revision / 100), (int)(gs_revision % 100));
  199     sprintf(key, "Software\\%s\\%s", gs_productfamily, dotversion);
  200 
  201     length = len;
  202     code = gp_getenv_registry(HKEY_CURRENT_USER, key, name, ptr, &length);
  203     if ( code == 0 )
  204     return TRUE;    /* found it */
  205 
  206     length = len;
  207     code = gp_getenv_registry(HKEY_LOCAL_MACHINE, key, name, ptr, &length);
  208 
  209     if ( code == 0 )
  210     return TRUE;    /* found it */
  211 
  212     return FALSE;
  213 }
  214 
  215 BOOL get_gs_string(int gs_revision, const char *name, char *ptr, int len)
  216 {
  217     if (get_gs_string_product(gs_revision, name, ptr, len, GS_PRODUCT_AFPL))
  218     return TRUE;
  219     if (get_gs_string_product(gs_revision, name, ptr, len, GS_PRODUCT_ALADDIN))
  220     return TRUE;
  221     if (get_gs_string_product(gs_revision, name, ptr, len, GS_PRODUCT_GPL))
  222     return TRUE;
  223     if (get_gs_string_product(gs_revision, name, ptr, len, GS_PRODUCT_GNU))
  224     return TRUE;
  225     return FALSE;
  226 }
  227 
  228 
  229 
  230 /* Set the latest Ghostscript EXE or DLL from the registry */
  231 BOOL
  232 find_gs(char *gspath, int len, int minver, BOOL bDLL)
  233 {
  234     int count;
  235     int *ver;
  236     int gsver;
  237     char buf[256];
  238     char *p;
  239     int i;
  240 
  241     DWORD version = GetVersion();
  242     if ( ((HIWORD(version) & 0x8000)!=0) && ((HIWORD(version) & 0x4000)==0) )
  243     return FALSE;  // win32s
  244 
  245     count = 1;
  246     get_gs_versions(&count);
  247     if (count < 1)
  248     return FALSE;
  249     ver = (int *)malloc((count+1)*sizeof(int));
  250     if (ver == (int *)NULL)
  251     return FALSE;
  252     ver[0] = count+1;
  253     if (!get_gs_versions(ver)) {
  254     free(ver);
  255     return FALSE;
  256     }
  257     gsver = 0;
  258     for (i=1; i<=ver[0]; i++) {
  259     if (ver[i] > gsver)
  260         gsver = ver[i];
  261     }
  262     free(ver);
  263     if (gsver < minver) // minimum version (e.g. for gsprint)
  264     return FALSE;
  265     
  266     if (!get_gs_string(gsver, "GS_DLL", buf, sizeof(buf)))
  267     return FALSE;
  268 
  269     if (bDLL) {
  270     strncpy(gspath, buf, len-1);
  271     return TRUE;
  272     }
  273 
  274     p = strrchr(buf, '\\');
  275     if (p) {
  276     p++;
  277     *p = 0;
  278     strncpy(p, "gswin32c.exe", sizeof(buf)-1-strlen(buf));
  279     strncpy(gspath, buf, len-1);
  280     return TRUE;
  281     }
  282 
  283     return FALSE;
  284 }
  285 
  286 
  287 #ifdef DUMP_GSVER
  288 /* This is an example of how you can use the above functions */
  289 int main(int argc, char *argv[])
  290 {
  291     BOOL flag;
  292     int n;
  293     int ver[10];
  294     int i;
  295     char buf[256];
  296 
  297     if (find_gs(buf, sizeof(buf), 550, TRUE))
  298     printf("Latest GS DLL is %s\n", buf);
  299     if (find_gs(buf, sizeof(buf), 550, FALSE))
  300     printf("Latest GS EXE is %s\n", buf);
  301 
  302     ver[0] = sizeof(ver) / sizeof(int);
  303     flag = get_gs_versions(ver);
  304     printf("Versions: %d\n", ver[0]);
  305 
  306     if (flag == FALSE) {
  307     printf("get_gs_versions failed, need %d\n", ver[0]);
  308     return 1;
  309     }
  310 
  311     for (i=1; i <= ver[0]; i++) {
  312     printf(" %d\n", ver[i]);
  313     if (get_gs_string(ver[i], "GS_DLL", buf, sizeof(buf)))
  314         printf("   GS_DLL=%s\n", buf);
  315     if (get_gs_string(ver[i], "GS_LIB", buf, sizeof(buf)))
  316         printf("   GS_LIB=%s\n", buf);
  317     }
  318     return 0;
  319 }
  320 #endif