Ignore:
Timestamp:
Nov 24, 2002, 9:45:05 PM (23 years ago)
Author:
umoeller
Message:

Sources as of 1.0.0.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/helpers/xprf2.c

    r167 r229  
    7777
    7878APIRET xprfCopyKey(HINI hiniSource,       // in: source profile (can be HINI_USER or HINI_SYSTEM)
    79                    PSZ pszSourceApp,      // in: source application
    80                    PSZ pszKey,            // in: source/target key
     79                   PCSZ pszSourceApp,     // in: source application
     80                   PCSZ pszKey,           // in: source/target key
    8181                   PXINI hiniTarget,      // in: target profile opened with xprfOpenProfile
    82                    PSZ pszTargetApp)      // in: target app
     82                   PCSZ pszTargetApp)     // in: target app
    8383{
    8484    ULONG   ulSizeOfData = 0;
    8585    APIRET  arc = NO_ERROR;
    8686
    87     if (PrfQueryProfileSize(hiniSource, pszSourceApp, pszKey, &ulSizeOfData))
     87    if (PrfQueryProfileSize(hiniSource,
     88                            (PSZ)pszSourceApp,
     89                            (PSZ)pszKey,
     90                            &ulSizeOfData))
    8891    {
    8992        PSZ pData = 0;
     
    107110            fflush(stdout);
    108111            if (PrfQueryProfileData(hiniSource,
    109                                     pszSourceApp,
    110                                     pszKey,
     112                                    (PSZ)pszSourceApp,
     113                                    (PSZ)pszKey,
    111114                                    pData,
    112115                                    &ulSizeOfData))
    113116            {
    114                 if (!xprfWriteProfileData(hiniTarget,
    115                                           pszTargetApp,
    116                                           pszKey,
    117                                           pData,
    118                                           ulSizeOfData))
     117                arc = xprfWriteProfileData(hiniTarget,
     118                                           pszTargetApp,
     119                                           pszKey,
     120                                           pData,
     121                                           ulSizeOfData);
     122            }
     123            else
     124                arc = PRFERR_READ;
     125
     126            free(pData);
     127        }
     128        else
     129            arc = ERROR_NOT_ENOUGH_MEMORY;
     130    }
     131    else
     132        arc = PRFERR_DATASIZE;
     133
     134    return arc;
     135}
     136
     137/*
     138 *@@ xprfCopyKey2:
     139 *      copies a single key from an xprf* PXINI to a Prf* HINI.
     140 *      hiniSource must therefore have been opened using
     141 *      xprfOpenProfile.
     142 *
     143 *      This returns 0 (NO_ERROR) if copying succeeded. Otherwise either
     144 *      an OS/2 error code (ERROR_*) or one of the profile error
     145 *      codes defined in prfh.h is returned.
     146 *
     147 *@@added V1.0.0 (2002-09-17) [umoeller]
     148 */
     149
     150APIRET xprfCopyKey2(PXINI hiniSource,   // in: source profile (can be HINI_USER or HINI_SYSTEM)
     151                    PCSZ pszSourceApp,  // in: source application
     152                    PCSZ pszKey,        // in: source/target key
     153                    HINI hiniTarget,    // in: target profile opened with xprfOpenProfile
     154                    PCSZ pszTargetApp)  // in: target app
     155{
     156    ULONG   ulSizeOfData = 0;
     157    APIRET  arc = NO_ERROR;
     158
     159    if (xprfQueryProfileSize(hiniSource, pszSourceApp, pszKey, &ulSizeOfData))
     160    {
     161        PSZ pData = 0;
     162
     163        // copy data
     164        if (ulSizeOfData == 0)
     165        {
     166            // data size == 0: this shouldn't really happen,
     167            // but if it does, we'll just create a NULL string.
     168            // Users have reported that some INI files seem to
     169            // contain those "empty" keys. I don't see how these
     170            // can exist, but they seem to...
     171            pData = (PSZ)malloc(1);
     172            *pData = 0;
     173        }
     174        else
     175            pData = (PSZ)malloc(ulSizeOfData);
     176
     177        if (pData)
     178        {
     179            fflush(stdout);
     180            if (xprfQueryProfileData(hiniSource,
     181                                     pszSourceApp,
     182                                     pszKey,
     183                                     pData,
     184                                     &ulSizeOfData))
     185            {
     186                if (!PrfWriteProfileData(hiniTarget,
     187                                         (PSZ)pszTargetApp,
     188                                         (PSZ)pszKey,
     189                                         pData,
     190                                         ulSizeOfData))
    119191                    arc = PRFERR_WRITE;
    120192            }
     
    135207/*
    136208 *@@ xprfCopyApp:
    137  *      copies a single application from an Prf* HINI to an xprf* PXINI.
     209 *      copies a single application from a Prf* HINI to an xprf* PXINI.
    138210 *      hiniTarget must therefore have been opened using
    139211 *      xprfOpenProfile.
     
    146218 */
    147219
    148 APIRET xprfCopyApp(HINI hiniSource,   // in: source profile (can be HINI_USER or HINI_SYSTEM)
    149                    PSZ pszSourceApp,  // in: source application
    150                    PXINI hiniTarget,  // in: target profile opened with xprfOpenProfile
    151                    PSZ pszTargetApp,  // in: name of pszSourceApp in hiniTarget
    152                    PSZ pszErrorKey)   // out: failing key in case of error; ptr can be NULL
     220APIRET xprfCopyApp(HINI hiniSource,     // in: source profile (can be HINI_USER or HINI_SYSTEM)
     221                   PCSZ pszSourceApp,   // in: source application
     222                   PXINI hiniTarget,    // in: target profile opened with xprfOpenProfile
     223                   PCSZ pszTargetApp,   // in: name of pszSourceApp in hiniTarget
     224                   PSZ pszErrorKey)     // out: failing key in case of error; ptr can be NULL
    153225{
    154226    APIRET arc = NO_ERROR;
     
    167239        {
    168240            // copy this key
    169             arc = xprfCopyKey(hiniSource,
    170                               pszSourceApp,
    171                               pKey2,
    172                               hiniTarget,
    173                               pszTargetApp);
    174             if (arc)
     241            if (arc = xprfCopyKey(hiniSource,
     242                                  pszSourceApp,
     243                                  pKey2,
     244                                  hiniTarget,
     245                                  pszTargetApp))
    175246            {
    176247                // error: copy failing key to buffer
     
    179250                break;
    180251            }
     252
     253            pKey2 += strlen(pKey2)+1;
     254        } // end while (*pKey2 != 0)
     255
     256        free (pszKeysList);
     257    }
     258    else
     259        arc = PRFERR_KEYSLIST;
     260
     261    return arc;
     262}
     263
     264/*
     265 *@@ xprfCopyApp2:
     266 *      copies a single application from an xprf* PXINI to a Prf* HINI.
     267 *      hiniTarget must therefore have been opened using
     268 *      xprfOpenProfile.
     269 *
     270 *      This calls xprfCopyKey2 for each key in the application.
     271 *
     272 *      This returns 0 (NO_ERROR) if copying succeeded. Otherwise either
     273 *      an OS/2 error code (ERROR_*) or one of the profile error
     274 *      codes defined in prfh.h is returned.
     275 *
     276 *@@added V1.0.0 (2002-09-17) [umoeller]
     277 */
     278
     279APIRET xprfCopyApp2(PXINI hiniSource,   // in: source profile (can be HINI_USER or HINI_SYSTEM)
     280                    PCSZ pszSourceApp,  // in: source application
     281                    HINI hiniTarget,    // in: target profile opened with xprfOpenProfile
     282                    PCSZ pszTargetApp,  // in: name of pszSourceApp in hiniTarget
     283                    PSZ pszErrorKey)    // out: failing key in case of error; ptr can be NULL
     284{
     285    APIRET arc = NO_ERROR;
     286    PSZ pszKeysList = NULL;
     287
     288    if (pszErrorKey)
     289        *pszErrorKey = 0;
     290
     291    if (!(arc = xprfQueryKeysForApp(hiniSource,
     292                                    pszSourceApp,
     293                                    &pszKeysList)))
     294    {
     295        PSZ pKey2 = pszKeysList;
     296
     297        while (*pKey2 != 0)
     298        {
     299            // copy this key
     300            if (arc = xprfCopyKey2(hiniSource,
     301                                   pszSourceApp,
     302                                   pKey2,
     303                                   hiniTarget,
     304                                   pszTargetApp))
     305            {
     306                // error: copy failing key to buffer
     307                if (pszErrorKey)
     308                    strcpy(pszErrorKey, pKey2);
     309                break;
     310            }
     311
    181312            pKey2 += strlen(pKey2)+1;
    182313        } // end while (*pKey2 != 0)
     
    208339
    209340APIRET xprfCopyProfile(HINI hOld,           // in: source profile (can be HINI_USER or HINI_SYSTEM)
    210                        PSZ pszNew,          // in: new filename (can be fully qualified)
     341                       PCSZ pszNew,         // in: new filename (can be fully qualified)
    211342                       PFN_PRF_PROGRESS pfnProgressCallback,
    212343                       ULONG ulUser,        // in: passed to pfnProgressCallback
    213344                       ULONG ulCount,       // in: index of INI being copied (0 <= ulCount <= ulMax)
    214                        ULONG ulMax)         // in: maximum index (for progress); 0 means 1 INI, 1 means 2 INIs, ...
     345                       ULONG ulMax,         // in: maximum index (for progress); 0 means 1 INI, 1 means 2 INIs, ...
     346                       PSZ pszFailingApp)   // out: failing app on error
    215347{
    216348    APIRET  arc = NO_ERROR;
     
    218350    ULONG   ulSizeOfAppsList;
    219351
     352    if (pszFailingApp)
     353        *pszFailingApp = 0;
     354
    220355    if (!pszNew)
    221356        arc = ERROR_INVALID_PARAMETER;
    222357    else
    223358    {
    224         DosDelete(pszNew);
     359        DosDelete((PSZ)pszNew);
    225360
    226361        // open new profile
    227         arc = xprfOpenProfile(pszNew,
    228                               &pxiniNew);
    229 
    230         // get size of applications list
    231         if (arc == NO_ERROR)
    232         {
     362        if (!(arc = xprfOpenProfile(pszNew,
     363                                    &pxiniNew)))
     364        {
     365            // get size of applications list
    233366            if (!PrfQueryProfileSize(hOld, NULL, NULL, &ulSizeOfAppsList))
    234367                arc = PRFERR_APPSLIST;
     
    256389                {
    257390                    CHAR szErrorKey[1000];
     391
    258392                    // copy application (this will call prfhCopyKey in turn)
    259                     arc = xprfCopyApp(hOld,
    260                                       pApp2,
    261                                       pxiniNew,
    262                                       pApp2,
    263                                       szErrorKey);
    264 
    265                     if (pfnProgressCallback)
     393                    if (arc = xprfCopyApp(hOld,
     394                                          pApp2,
     395                                          pxiniNew,
     396                                          pApp2,
     397                                          szErrorKey))
     398                    {
     399                        if (pszFailingApp)
     400                            strhncpy0(pszFailingApp, pApp2, CCHMAXPATH);
     401                    }
     402                    else if (pfnProgressCallback)
    266403                    {
    267404                        ULONG ulNow2, ulMax2;
     
    325462APIRET xprfSaveINIs(HAB hab,               // in:  anchor block
    326463                    PFN_PRF_PROGRESS pfnProgressCallback,
    327                     ULONG ulUser)
     464                    ULONG ulUser,
     465                    PSZ pszFailingINI,
     466                    PSZ pszFailingApp,
     467                    PSZ pszFailingKey)
    328468{
    329469    PRFPROFILE Profiles;
     
    400540
    401541        if (arc == NO_ERROR)
     542        {
     543            if (pszFailingINI)
     544                strcpy(pszFailingINI,
     545                       szSysNew);
     546
    402547            arc = xprfCopyProfile(HINI_SYSTEM,
    403548                                  szSysNew,              // new filename
    404549                                  pfnProgressCallback,
    405                                   ulUser, 0, 1);
     550                                  ulUser,
     551                                  0,
     552                                  1,
     553                                  pszFailingApp);
     554        }
     555
    406556        /*
    407557         * create OS2SYS.XFL:
     
    410560
    411561        if (arc == NO_ERROR)
     562        {
     563            if (pszFailingINI)
     564                strcpy(pszFailingINI,
     565                       szUserNew);
     566
    412567            arc = xprfCopyProfile(HINI_USER,
    413568                                  szUserNew,              // new filename
    414569                                  pfnProgressCallback,
    415                                   ulUser, 1, 1);
     570                                  ulUser,
     571                                  1,
     572                                  1,
     573                                  pszFailingApp);
     574        }
    416575    }
    417576
     
    453612    {
    454613        // finally, replace system profiles
    455         arc = DosMove(szSysNew, Profiles.pszSysName);
    456         if (arc == NO_ERROR)
    457             arc = DosMove(szUserNew, Profiles.pszUserName);
     614        if (!(arc = DosMove(szSysNew, Profiles.pszSysName)))
     615        {
     616            if (arc = DosMove(szUserNew, Profiles.pszUserName))
     617            {
     618                if (pszFailingINI)
     619                    strcpy(pszFailingINI,
     620                           szUserNew);
     621            }
     622        }
     623        else
     624        {
     625            if (pszFailingINI)
     626                strcpy(pszFailingINI,
     627                       szSysNew);
     628        }
    458629    }
    459630
     
    468639// testing
    469640
    470 /* BOOL _Optlink fnCallback(ULONG ulUser, ULONG ulNow, ULONG ulMax)
     641#ifdef __BUILD_XPRF2_MAIN__
     642
     643BOOL _Optlink fnCallback(ULONG ulUser, ULONG ulNow, ULONG ulMax)
    471644{
    472645    printf("\r done %03d%%", ulNow * 100 / ulMax);
     
    476649int main(int argc, char* argv[])
    477650{
    478     if (argc != 3)
    479         printf("Syntax: xprf2 <source.ini> <target.ini>\n");
    480     else
    481     {
    482         HAB hab = WinInitialize(0);
    483 
    484         // HINI hiniSource = PrfOpenProfile(hab, argv[1]);
    485         // if (hiniSource)
    486         {
    487             APIRET arc = xprfCopyProfile(hiniSource,
    488                                          argv[2],
    489                                          fnCallback,
    490                                          0, 0, 0);
    491             xprfSaveProfiles(hab, NULL, 0);
    492 //             if (arc)
    493 
    494 
    495    //              printf("xprfCopyProfile returned %d.\n", arc);
    496         }
    497         // else
    498             // printf("Cannot open %s\n", argv[1]);
    499 
    500         WinTerminate(hab);
    501     }
    502 
    503     return (0);
    504 } */
    505 
     651    APIRET  arc = 2;
     652
     653    HAB     hab = WinInitialize(0);
     654
     655    CHAR    szFailingINI[CCHMAXPATH] = "",
     656            szFailingApp[CCHMAXPATH] = "",
     657            szFailingKey[CCHMAXPATH] = "";
     658
     659    arc = xprfSaveINIs(hab,
     660                       NULL,
     661                       0,
     662                       szFailingINI,
     663                       szFailingApp,
     664                       szFailingKey);
     665
     666    printf("xprfCopyProfile returned %d ('%s', '%s', '%s'.\n",
     667           arc,
     668           szFailingINI,
     669           szFailingApp,
     670           szFailingKey);
     671
     672    WinTerminate(hab);
     673
     674    return arc;
     675}
     676
     677#endif
     678
Note: See TracChangeset for help on using the changeset viewer.