home *** CD-ROM | disk | FTP | other *** search
/ cyber.net 2 / cybernet2.ISO / qtw111 / pviewer / common.c < prev    next >
C/C++ Source or Header  |  1994-01-11  |  13KB  |  413 lines

  1.  
  2. // ---------------------------------------------------------------------
  3. //
  4. // Common.c - Common Routines - QuickTime for Windows
  5. //
  6. //            Version 1.0
  7. //
  8. //            (c) 1988-1992 Apple Computer, Inc. All Rights Reserved.
  9. //
  10. // ---------------------------------------------------------------------
  11.  
  12.  
  13. // Includes
  14. // --------
  15. #include <Windows.H>  // Required by Windows
  16. #include <WindowsX.H> // Required by Windows
  17.  
  18. #include <dos.h>    // Standard "C" header
  19. #include <errno.h>  // Standard "C" header
  20. #include <stdarg.h> // Standard "C" header
  21. #include <stdlib.h> // Standard "C" header
  22. #include <stdio.h>  // Standard "C" header
  23.  
  24. #include "Common.H" // Interface to Common.C
  25.  
  26.  
  27. // Globals
  28. // -------
  29. static struct
  30.    {char szOutOfMemory[COMMON_STRING_MAX]; // Out of memory message
  31.     WORD idNoMemMsg;                       // Non memory message id
  32.    } g;
  33.  
  34.  
  35. // Region Code Table
  36. // --------------------------------------------------------------------
  37. // Ordinal position represents corresponding Mac region code
  38. // --------------------------------------------------------------------
  39. static char* pszRegion[] = {
  40.    "enu", // 0
  41.    "fra", // 1
  42.    "eng", // 2
  43.    "deu", // 3
  44.    "ita", // 4
  45.    "nld", // 5
  46.    "fra", // 6
  47.    "sve", // 7
  48.    "esn", // 8
  49.    "dan", // 9
  50.    "ptg", // 10
  51.    "frc", // 11
  52.    "nor", // 12
  53.    "---", // 13
  54.    "---", // 14
  55.    "eng", // 15
  56.    "---", // 16
  57.    "fin", // 17
  58.    "fra", // 18
  59.    "deu", // 19
  60.    "---", // 20
  61.    "isl", // 21
  62.    ""     // Last entry must be NULL
  63.   };
  64.  
  65.  
  66. // Function: CommonAlloc - Allocate memory
  67. // --------------------------------------------------------------------
  68. // Parameters: LONG lBytes;            /* Number of bytes to allocate */
  69. //
  70. // Returns:    VOID FAR *pvMemory;     /* Memory block */
  71. // --------------------------------------------------------------------
  72. VOID FAR * FAR CommonAlloc (LONG lBytes)
  73.  
  74. // Perform simple allocation for now
  75.  
  76. {
  77.     HGLOBAL  hmem; // Memory handle
  78.  
  79.     if( hmem = GlobalAlloc( GHND, (DWORD) lBytes ))
  80.         return GlobalLock( hmem );
  81.     else
  82.         return NULL;
  83. }
  84.  
  85.  
  86. // Function: CommonFormatMessage - Format Message
  87. // --------------------------------------------------------------------
  88. // Parameters: HINSTANCE hResources;   /* Resource-only DLL handle */
  89. //             WORD idMsg;             /* ID of message in resource file */
  90. //             LPSTR lpszMsg;          /* Area in which to build message */
  91. //
  92. // Returns:    LPSTR lpszMsg;
  93. // --------------------------------------------------------------------
  94. LPSTR FAR CommonFormatMessage
  95.     (HINSTANCE hResources, WORD idMsg, LPSTR lpszMsg, ...)
  96.  
  97. // Define function data
  98.  
  99. {
  100.     char szFormat[COMMON_STRING_MAX]; // Message format
  101.     va_list pArgs;                    // String substitution arguments
  102.  
  103.     // Load the message string
  104.  
  105.     LoadString (hResources, idMsg, szFormat, sizeof (szFormat));
  106.  
  107.     // Build the message text
  108.  
  109.     va_start (pArgs, lpszMsg);
  110.     wvsprintf (lpszMsg, szFormat, (LPSTR) pArgs);
  111.     va_end (pArgs);
  112.  
  113.     // Return to caller
  114.  
  115.     return lpszMsg;
  116.  
  117. }
  118.  
  119.  
  120. // Function: CommonFree - Free memory allocated by CommonAlloc
  121. // --------------------------------------------------------------------
  122. // Parameters: VOID FAR *pvMemory;     /* Memory to free */
  123. //
  124. // Returns:    None
  125. // --------------------------------------------------------------------
  126. VOID FAR CommonFree (VOID FAR *pvMemory)
  127.  
  128. // Perform simple free for now
  129.  
  130. {
  131.     HGLOBAL   hmem; //Memory handle
  132.  
  133.     if( hmem = (HGLOBAL) LOWORD( GlobalHandle( SELECTOROF( pvMemory )))) {
  134.         GlobalUnlock( hmem );
  135.         GlobalFree( hmem );
  136.     }
  137.  
  138.     return;
  139. }
  140.  
  141.  
  142. // Function: CommonGetDirectoryOfModule - Get Path of Module
  143. // --------------------------------------------------------------------
  144. // Parameters: HINSTANCE hInstance;      /* Application's instance handle */
  145. //             LPSTR lpPath;             /* pointer to path buffer */
  146.  
  147. // Returns:    LPSTR lpPath;
  148. // --------------------------------------------------------------------
  149. LPSTR FAR CommonGetDirectoryOfModule (HINSTANCE hInstance, LPSTR lpPath)
  150.  
  151. {
  152.     char  szBuffer[CCHMAXPATH]; // Temporary buffer
  153.     LPSTR lpszTemp;             // Temporary pointer
  154.  
  155.     // Clear the buffer
  156.  
  157.     szBuffer[0] = '\0';
  158.  
  159.     // Get the fully qualified .EXE name
  160.  
  161.     if (!GetModuleFileName (hInstance, szBuffer, sizeof (szBuffer)))
  162.         return (LPSTR) NULL;
  163.  
  164.     // Look backwards until we find the last backslash
  165.  
  166.     lpszTemp = szBuffer + lstrlen(szBuffer);
  167.  
  168.     while ((lpszTemp > szBuffer) && (*lpszTemp != '\\'))
  169.         lpszTemp = AnsiPrev (szBuffer, lpszTemp);
  170.     if (*lpszTemp != '\\')
  171.         return (LPSTR) NULL;
  172.  
  173.     // We delete the last backslash; we now have the path specification
  174.  
  175.     *lpszTemp = '\0';
  176.     lstrcpy (lpPath, AnsiUpper (szBuffer));
  177.  
  178.     // Return to caller
  179.  
  180.     return lpPath;
  181.  
  182. }
  183.  
  184.  
  185. // Function: CommonGetLocalizedHelpFile - Get Help File Name
  186. // --------------------------------------------------------------------
  187. // Parameters: LPSTR lpszAppl;           /* Application's root name */
  188. //             LPSTR lpszName;           /* Area in which to build name */
  189. //             HINSTANCE hInstance;      /* Application's instance handle */
  190. //
  191. // Returns:    LPSTR pszName;
  192. // --------------------------------------------------------------------
  193. LPSTR FAR CommonGetLocalizedHelpFile
  194.     (LPSTR lpszAppl, LPSTR lpszName, HINSTANCE hInstance)
  195.  
  196. // Define function data
  197.  
  198. {
  199.     char   szBuffer[CCHMAXPATH];  // Temporary buffer
  200.     char   szApplDir[CCHMAXPATH]; // Application's directory
  201.     char   szLanguage[4];         // Language code
  202.     struct _find_t  fileinfo;     // Temp file info
  203.  
  204.     // Clear the area in which the name will be built
  205.  
  206.     lpszName[0] = '\0';
  207.  
  208.     // Get the application's directory
  209.  
  210.     if (!CommonGetDirectoryOfModule (hInstance, (LPSTR) szApplDir))
  211.         return lpszName;
  212.  
  213.     // Look for the localized help file in the ..\HELP directory
  214.  
  215.     GetProfileString( "intl", "sLanguage", "enu", szLanguage, sizeof(szLanguage));
  216.     wsprintf( szBuffer, "%s\\..\\help\\%s%s.hlp",
  217.         (LPSTR) szApplDir, (LPSTR) lpszAppl, (LPSTR) szLanguage );
  218.     if (_dos_findfirst (szBuffer, _A_NORMAL, &fileinfo) == 0) {
  219.         lstrcpy (lpszName, AnsiUpper (szBuffer));
  220.         return lpszName;
  221.     }
  222.  
  223.     // If it can't be found, look for the localized help
  224.     // file in the application's directory
  225.  
  226.     wsprintf( szBuffer, "%s\\%s%s.hlp",
  227.         (LPSTR) szApplDir, (LPSTR) lpszAppl, (LPSTR) szLanguage );
  228.     if (_dos_findfirst (szBuffer, _A_NORMAL, &fileinfo) == 0) {
  229.         lstrcpy (lpszName, AnsiUpper (szBuffer));
  230.         return lpszName;
  231.     }
  232.  
  233.     // If it can't be found, look for the US English help
  234.     // file in the ..\HELP directory
  235.  
  236.     wsprintf( szBuffer, "%s\\..\\help\\%senu.hlp",
  237.         (LPSTR) szApplDir, (LPSTR) lpszAppl );
  238.     if (_dos_findfirst (szBuffer, _A_NORMAL, &fileinfo) == 0) {
  239.         lstrcpy (lpszName, AnsiUpper (szBuffer));
  240.         return lpszName;
  241.     }
  242.  
  243.     // If it can't be found, look for the US English help
  244.     // file in the application's directory
  245.  
  246.     wsprintf( szBuffer, "%s\\%senu.hlp", (LPSTR) szApplDir, (LPSTR) lpszAppl );
  247.     if (_dos_findfirst (szBuffer, _A_NORMAL, &fileinfo) == 0) {
  248.         lstrcpy (lpszName, AnsiUpper (szBuffer));
  249.         return lpszName;
  250.     }
  251.  
  252.     // Otherwise, return NULL to the caller
  253.  
  254.     return lpszName;
  255.  
  256. }
  257.  
  258.  
  259. // Function: CommonGetLocalizedResources - Load Resource-Only DLL
  260. // --------------------------------------------------------------------
  261. // Parameters: LPSTR lpszAppl;         Application's root name
  262. //             HINSTANCE hInstance;    Application's instance handle
  263. //             WORD  idNoMemMsg;       id of no memory message
  264. //
  265. // Returns:    HINSTANCE hResource;    handle to resource instance
  266. // --------------------------------------------------------------------
  267. HINSTANCE FAR CommonGetLocalizedResources
  268.     (LPSTR lpszAppl, HINSTANCE hInstance, WORD idNoMemMsg )
  269.  
  270. // Def