home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional Developers Kit 1992 November / Disc01 / Disc01.mdf / os2tk20 / c / samples / print / prtprof.c__ / PRTPROF.C
Encoding:
C/C++ Source or Header  |  1992-07-15  |  12.0 KB  |  389 lines

  1. /****************************************************************************
  2.  * OS/2 Sample Print Application PRTSAMP
  3.  *
  4.  * File name: prtprof.c
  5.  *
  6.  * Description:  In this module are all the functions needed to save and
  7.  *               restore the application defaults to/from the PRTSAMP.INI.
  8.  *
  9.  *               This source file contains the following functions:
  10.  *
  11.  *               GetProfile(pmp)
  12.  *               SaveProfile(pmp)
  13.  *               SaveVersion(pmp)
  14.  *               SaveProfileOnly(pmp)
  15.  *               SaveDriverData(pmp)
  16.  *               PutProfileInfo(pmp)
  17.  *               GetProfileInfo(pmp)
  18.  *
  19.  * Concepts:   profile file
  20.  *
  21.  * API's:      PrfQueryProfileData
  22.  *             PrfWriteProfileData
  23.  *             PrfQueryProfileSize
  24.  *             WinQueryWindowPos
  25.  *             WinQueryWindowUShort
  26.  *
  27.  *    Files    :  OS2.H, PRTSAMP.H, PRTSDLG.H, PMASSERT.H
  28.  *
  29.  *  Copyright (C) 1991 IBM Corporation
  30.  *
  31.  *      DISCLAIMER OF WARRANTIES.  The following [enclosed] code is
  32.  *      sample code created by IBM Corporation. This sample code is not
  33.  *      part of any standard or IBM product and is provided to you solely
  34.  *      for  the purpose of assisting you in the development of your
  35.  *      applications.  The code is provided "AS IS", without
  36.  *      warranty of any kind.  IBM shall not be liable for any damages
  37.  *      arising out of your use of the sample code, even if they have been
  38.  *      advised of the possibility of such damages.                                                    *
  39.  ****************************************************************************/
  40.  
  41. /* os2 includes */
  42. #define INCL_WINSTDFILE
  43. #define INCL_WINSTDFONT
  44. #define INCL_WINWINDOWMGR
  45. #define INCL_WINSHELLDATA
  46. #define INCL_GPITRANSFORMS
  47. #define INCL_SPL
  48. #define INCL_SPLDOSPRINT
  49. #include <os2.h>
  50.  
  51. /* c language includes */
  52. #include <stdio.h>
  53. #include <stdlib.h>
  54. #include <string.h>
  55. #include <ctype.h>
  56. #include <stddef.h>
  57. #include <process.h>
  58. #include <memory.h>
  59. #include <sys\types.h>
  60. #include <sys\stat.h>
  61.  
  62. /* application includes */
  63. #include "prtsamp.h"
  64. #include "prtsdlg.h"
  65. #include "pmassert.h"
  66.  
  67. static const LONG lProgramVersion = 2000L;
  68.  
  69. static const PSZ pszApplication = "PRTSAMP";
  70. static const PSZ pszProfileKey = "Profile";
  71. static const PSZ pszVersionKey = "Version";
  72. static const PSZ pszDriverDataKey = "DriverData";
  73.  
  74. VOID GetProfileInfo(PMAIN_PARM pmp);
  75. VOID PutProfileInfo(PMAIN_PARM pmp);
  76. BOOL SaveDriverData(PMAIN_PARM pmp);
  77. BOOL SaveProfileOnly(PMAIN_PARM pmp);
  78. BOOL SaveVersion(PMAIN_PARM pmp);
  79.  
  80. /***************************************************************************
  81.  * Name    : GetDefaults
  82.  *
  83.  * Description:  Get the application defaults from OS2.INI (HINI_USERPROFILE)
  84.  *
  85.  * Concepts:  profile file
  86.  *
  87.  * API's:      PrfQueryProfileData
  88.  *             PrfWriteProfileData
  89.  *             PrfQueryProfileSize
  90.  *
  91.  * Parameters:  pmp = a pointer to the application main data structure
  92.  *
  93.  * Return:   [none]
  94.  *
  95.  ***************************************************************************/
  96. VOID GetProfile(PMAIN_PARM pmp)
  97. {
  98.    LONG    lCount, lVersion;
  99.    BOOL    fNeedDefaults;
  100.  
  101.    fNeedDefaults = TRUE;
  102.    lVersion = 0;
  103.    pmp->pDriverData = (PDRIVDATA)NULL;
  104.    pmp->cbDriverDataLen = 0;
  105.  
  106.    /*
  107.     * First determine if the profile information in OS2.INI matches
  108.     * the version number of this program.  If so, we will understand
  109.     * the format of the data.  Otherwise, just use default values.
  110.     */
  111.    lCount = sizeof(LONG);
  112.  
  113.    if (!PrfQueryProfileData(
  114.            HINI_USERPROFILE,
  115.            pszApplication,
  116.            pszVersionKey,
  117.            (PVOID)&lVersion,
  118.            (PULONG)&lCount) ||
  119.        lCount != sizeof(LONG))
  120.    {
  121.        ;
  122.    }
  123.    else if (lVersion != lProgramVersion)
  124.    {
  125.        /*
  126.         * The profile data is the wrong version, so we need to clean
  127.         * it up. This deletes all keys under the application name.
  128.         */
  129.        PrfWriteProfileData(
  130.            HINI_USERPROFILE,
  131.            pszApplication,
  132.            (PSZ)NULL,
  133.            (PVOID)NULL,
  134.            0L);
  135.    }
  136.    else
  137.    {
  138.        /*
  139.         * We have a matching version number, so we can proceed to
  140.         * read the rest of the profile information.
  141.         */
  142.  
  143.        /*
  144.         * Look for the "Profile" information. We know the size of this
  145.         * data so we're going to read as many bytes as we need. If for
  146.         * some reason there's more info than we need that's all right
  147.         * because we're relying on the lVersion to tell us we understand
  148.         * the data.
  149.         */
  150.        lCount = sizeof(PRTSAMP_PROFILE);
  151.  
  152.        if (PrfQueryProfileData(
  153.                HINI_USERPROFILE,
  154.                pszApplication,
  155.                pszProfileKey,
  156.                (PVOID)&pmp->Profile,
  157.                (PULONG)&lCount) &&
  158.            lCount == sizeof(PRTSAMP_PROFILE))
  159.        {
  160.            fNeedDefaults = FALSE;
  161.  
  162.            /*
  163.             * We found our profile data, which includes what
  164.             * print object we want to use. Now look for the
  165.             * "DriverData" data we want to use for this
  166.             * print object.
  167.             */
  168.            if (PrfQueryProfileSize(
  169.                    HINI_USERPROFILE,
  170.                    pszApplication,
  171.                    pszDriverDataKey,
  172.                    (PULONG)&lCount) &&
  173.                lCount >= sizeof(DRIVDATA))
  174.            {
  175.                /*
  176.                 * Found the size of the driver data. It can
  177.                 * only make sense if it is at least as big
  178.                 * as the DRIVDATA structure. Allocate some
  179.                 * space for it, and read it in.
  180.                 */
  181.                pmp->pDriverData = (PDRIVDATA)malloc(lCount);
  182.                if (pmp->pDriverData)
  183.                {
  184.                    pmp->cbDriverDataLen = lCount;
  185.  
  186.                    if (!PrfQueryProfileData(
  187.                            HINI_USERPROFILE,
  188.                            pszApplication,
  189.                            pszDriverDataKey,
  190.                            (PVOID)pmp->pDriverData,
  191.                            (PULONG)&lCount) ||
  192.                        lCount != pmp->cbDriverDataLen)
  193.                    {
  194.                        /*
  195.                         * Didn't find any driver data or the size
  196.                         * was wrong. Back out of our allocation.
  197.                         */
  198.                        free(pmp->pDriverData);
  199.                        pmp->pDriverData = (PDRIVDATA)NULL;
  200.                        pmp->cbDriverDataLen = 0;
  201.                    }
  202.                }
  203.            }
  204.        }
  205.  
  206.    } /* end handle correct profile version info */
  207.  
  208.    if (fNeedDefaults)
  209.    {
  210.        memset((PVOID)&pmp->Profile, 0, sizeof(PRTSAMP_PROFILE));
  211.    }
  212.    else
  213.    {
  214.        GetProfileInfo(pmp);
  215.    }
  216. }  /*  end of GetProfile() */
  217.  
  218. /**************************************************************************
  219.  * Name:     SaveProfile
  220.  *
  221.  * Description: saves profile data PRTSAMP_PROFILE Application defaults
  222.  *
  223.  * API's:  [none]
  224.  *
  225.  * Parameters:  pmp = a pointer to the application main data structure
  226.  *
  227.  * Return:  [none]
  228.  *
  229.  ***************************************************************************/
  230. VOID SaveProfile(PMAIN_PARM pmp)
  231. {
  232.     PutProfileInfo(pmp);
  233.     SaveVersion(pmp);
  234.     if (SaveProfileOnly(pmp))
  235.         SaveDriverData(pmp);
  236.  
  237.     return;
  238. } /* end of SaveProfile() */
  239.  
  240. /**************************************************************************
  241.  * Name:     SaveVersion
  242.  *
  243.  * Description: saves program version to user profile
  244.  *
  245.  * API's:  PrfWriteProfileData
  246.  *
  247.  * Parameters:  pmp = a pointer to the application main data structure
  248.  *
  249.  * Return:  value from call to PrfWriteProfileData
  250.  *
  251.  ***************************************************************************/
  252. BOOL SaveVersion(PMAIN_PARM pmp)
  253. {
  254.     return PrfWriteProfileData(
  255.         HINI_USERPROFILE,
  256.         pszApplication,
  257.         pszVersionKey,
  258.         (PVOID)&lProgramVersion,
  259.         (ULONG)sizeof(lProgramVersion));
  260. } /*  end of SaveVersion()  */
  261.  
  262. /**************************************************************************
  263.  * Name :    SaveProfileOnly
  264.  *
  265.  * Description: saves program profile to user profile
  266.  *
  267.  * API's:  PrfWriteProfileData
  268.  *
  269.  * Parameters:  pmp = a pointer to the application main data structure
  270.  *
  271.  * Return:  value from call to PrfWriteProfileData
  272.  *
  273.  ***************************************************************************/
  274. BOOL SaveProfileOnly(PMAIN_PARM pmp)
  275. {
  276.     return PrfWriteProfileData(
  277.         HINI_USERPROFILE,
  278.         pszApplication,
  279.         pszProfileKey,
  280.         (PVOID)&pmp->Profile,
  281.         (ULONG)sizeof(pmp->Profile));
  282. } /* end of SaveProfileOnly()  */
  283.  
  284. /**************************************************************************
  285.  * Name :    SaveDriverData
  286.  *
  287.  * Description: saves driver data to user profile
  288.  *
  289.  * API's:  PrfWriteProfileData
  290.  *
  291.  * Parameters:  pmp = a pointer to the application main data structure
  292.  *
  293.  * Return:  value from call to PrfWriteProfileData
  294.  *
  295.  ***************************************************************************/
  296. BOOL SaveDriverData(PMAIN_PARM pmp)
  297. {
  298.    return pmp->pDriverData == (PDRIVDATA)NULL ? FALSE :
  299.         PrfWriteProfileData(
  300.             HINI_USERPROFILE,
  301.             pszApplication,
  302.             pszDriverDataKey,
  303.             (PVOID)pmp->pDriverData,
  304.             (ULONG)pmp->cbDriverDataLen);
  305. }  /*  end of SaveDriverData()  */
  306.  
  307. /**************************************************************************
  308.  * Name :    PutProfileInfo
  309.  *
  310.  * Description:  writes data to the profile file
  311.  *
  312.  * API's:  WinQueryWindowPos
  313.  *         WinQueryWindowUShort
  314.  *
  315.  * Parameters:  pmp = a pointer to the application main data structure
  316.  *
  317.  * Return:  [none]
  318.  *
  319.  ***************************************************************************/
  320. VOID PutProfileInfo(PMAIN_PARM pmp)
  321. {
  322.    SWP     swpPos;
  323.  
  324.    memset((PVOID)&pmp->Profile, 0, sizeof(PRTSAMP_PROFILE));
  325.  
  326.    /*
  327.     * Query window position and size.
  328.     */
  329.    WinQueryWindowPos(pmp->hwndFrame, &swpPos);
  330.  
  331.    if ((swpPos.fl & SWP_MAXIMIZE) != 0)
  332.    {
  333.        pmp->Profile.fMaximized = TRUE;
  334.        pmp->Profile.cy = WinQueryWindowUShort(pmp->hwndFrame, QWS_CYRESTORE);
  335.        pmp->Profile.cx = WinQueryWindowUShort(pmp->hwndFrame, QWS_CXRESTORE);
  336.        pmp->Profile.y = WinQueryWindowUShort(pmp->hwndFrame, QWS_YRESTORE);
  337.        pmp->Profile.x = WinQueryWindowUShort(pmp->hwndFrame, QWS_XRESTORE);
  338.    }
  339.    else
  340.    {
  341.        pmp->Profile.fMaximized = FALSE;
  342.        pmp->Profile.cy = swpPos.cy;
  343.        pmp->Profile.cx = swpPos.cx;
  344.        pmp->Profile.y = swpPos.y;
  345.        pmp->Profile.x = swpPos.x;
  346.    }
  347.  
  348.    pmp->Profile.fxPointSize = pmp->fontdlg.fxPointSize;
  349.    memcpy((PVOID)&pmp->Profile.fAttrs,
  350.           (PVOID)&pmp->fontdlg.fAttrs,
  351.           sizeof(pmp->Profile.fAttrs));
  352.  
  353.    pmp->Profile.ulNextMode = pmp->ulMode;
  354.    strcpy(pmp->Profile.szNextFilename, pmp->szFilename);
  355.  
  356.    strcpy(pmp->Profile.achQueueName, pmp->achQueueName);
  357.    strcpy(pmp->Profile.achDriverName, pmp->achDriverName);
  358.  
  359.    return;
  360. }  /*  end of PutProfileInfo()  */
  361.  
  362. /**************************************************************************
  363.  * Function: GetProfileInfo
  364.  *
  365.  * Description:  reads data which had been saved in the profile
  366.  *
  367.  * API's:  [none]
  368.  *
  369.  * Parameters:  pmp = a pointer to the application main data structure
  370.  *
  371.  * Result: [none]
  372.  *
  373.  ***************************************************************************/
  374. VOID GetProfileInfo(PMAIN_PARM pmp)
  375. {
  376.    pmp->fontdlg.fxPointSize = pmp->Profile.fxPointSize;
  377.    memcpy((PVOID)&pmp->fontdlg.fAttrs,
  378.           (PVOID)&pmp->Profile.fAttrs,
  379.           sizeof(pmp->Profile.fAttrs));
  380.  
  381.    pmp->ulNextMode = pmp->Profile.ulNextMode;
  382.    strcpy(pmp->szNextFilename, pmp->Profile.szNextFilename);
  383.    strcpy(pmp->achQueueName, pmp->Profile.achQueueName);
  384.    strcpy(pmp->achDriverName, pmp->Profile.achDriverName);
  385.  
  386.    return;
  387. }  /*  end of GetProfileInfo()  */
  388. /***************************  End of prtprof.c ****************************/
  389.