home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / DUMPINI.CMD < prev    next >
OS/2 REXX Batch file  |  1995-03-28  |  4KB  |  125 lines

  1. EXTPROC CEnvi2
  2. /*********************************************************
  3.  *** DumpINI - Dump profile settings from a .INI file. ***
  4.  *** ver.1     This is a CEnvi2 example for using the   ***
  5.  ***           Profile.lib Cmm library .               ***
  6.  *********************************************************/
  7.  
  8. #include <Profile.lib>
  9.  
  10. main(argc,argv)
  11. {
  12.    if ( argc < 2 ||  !strcmp(argv[1],"/?")  ||  !stricmp(argv[1],"/help")  || 4 < argc )
  13.       Instructions();
  14.    else {
  15.       // Get parameters from input; default to NULL if parameter not supplied
  16.       IniSpec = argv[1];
  17.       AppSpec = 2 < argc ? argv[2] : NULL ;
  18.       KeySpec = 3 < argc ? argv[3] : NULL ;
  19.  
  20.       hini = OpenIni(IniSpec);
  21.       if ( NULL == AppSpec ) {
  22.          // display for ALL apps in this ini
  23.          AppCount = PrfGetApplicationNames(hini,AppList);
  24.          for ( i = 0; i < AppCount; i++ )
  25.          DisplayAppIni(hini,AppList[i],KeySpec);
  26.       } else {
  27.          // display for only one app
  28.          DisplayAppIni(hini,AppSpec,KeySpec);
  29.       }
  30.       CloseIni(hini);
  31.    }
  32.    return(EXIT_SUCCESS);
  33. }
  34.  
  35. DisplayAppIni(hini,AppName,KeySpec)
  36. {
  37.    printf("\n<%s>\n",AppName);
  38.    if ( NULL == KeySpec ) {
  39.       // display for ALL keys in this App
  40.       KeyCount = PrfGetKeyNames(hini,AppName,KeyList);
  41.       for ( i = 0; i < KeyCount; i++ )
  42.          DisplayKeyIni(hini,AppName,KeyList[i]);
  43.    } else {
  44.       // display for only one key
  45.       DisplayKeyIni(hini,AppName,KeySpec);
  46.    }
  47. }
  48.  
  49. DisplayKeyIni(hini,AppName,KeyName)
  50. {
  51.    printf(" <%s>\n",KeyName);
  52.    // Get size of data
  53.    if ( !PrfQueryProfileSize(hini,AppName,KeyName,datalen) || !datalen )
  54.       printf("\aKey \"%s\" not found!\n",KeyName);
  55.    else {
  56.       // make buffer 3 times this big, just to be sure
  57.       BLObSize(data,datalen *= 3);
  58.       if ( !PrfQueryProfileData(hini,AppName,KeyName,data,datalen) || !datalen )
  59.          printf("\aKey \"%s\" not found!\n",KeyName);
  60.       else
  61.          DisplayBinaryData(data,datalen);
  62.    }
  63. }
  64.  
  65. DisplayBinaryData(buf,buflen)
  66. {
  67.    Unprintables = "\a\b\t\r\n\032\033"
  68.    for ( offset = 0; 0 < (count = min(buflen,16)); buf += count, buflen -= count ) {
  69.       // indent each line
  70.       printf("    ")
  71.       // display hex value for each number
  72.       for ( i = 0; i < count; i++ )
  73.          printf("%02X ",buf[i])
  74.       // fill in any extra gaps if count < 16
  75.       while( i++ < 16 )
  76.          printf("   ")
  77.       // display ascii value for each printable character
  78.       // substitute a period for unprintable characters
  79.       memcpy(str,buf,count);
  80.       str[count] = 0; // string must be null-terminated
  81.       while( (UnprintableIndex = strcspn(str,Unprintables)) < count )
  82.          str[UnprintableIndex] = '.';
  83.       printf("   %s\n",str)
  84.    }
  85. }
  86.  
  87. #define  OS2_INI     "OS2"
  88. #define  OS2SYS_INI  "OS2SYS"
  89.  
  90. OpenIni(Spec)
  91. {
  92.    if ( !strcmpi(Spec,OS2_INI) ) return HINI_USERPROFILE;
  93.    if ( !strcmpi(Spec,OS2SYS_INI) ) return HINI_SYSTEMPROFILE;
  94.    if ( NULL == (hini = PrfOpenProfile(Info().hab,Spec)) ) {
  95.       printf("\aCannot open profile filespec \"%s\"\n",Spec);
  96.       exit(EXIT_FAILURE);
  97.    }
  98.    return hini;
  99. }
  100.  
  101. CloseIni(hini)
  102. {
  103.    if ( HINI_USERPROFILE != hini  &&  HINI_SYSTEMPROFILE != hini )
  104.       PrfCloseProfile(hini);
  105. }
  106.  
  107. Instructions()
  108. {
  109.    printf("\n")
  110.    printf("DumpINI - Display profile settings in a .INI file\n")
  111.    printf("\n")
  112.    printf("SYNTAX: DumpINI <FileSpec> [ApplicationName [KeyName]]\n")
  113.    printf("\n")
  114.    printf("Where: FileSpec - Name of the INI file.  For the OS2 system file use the\n")
  115.    printf("                  special name \"%s\" and for OS2 user file use \"%s\".\n",OS2SYS_INI,OS2_INI)
  116.    printf("       ApplicationName - Application number under which the settings are saved\n")
  117.    printf("                  If not supplied then list ALL applications\n")
  118.    printf("       KeyName - Name of the KEY for this setting; defaults to ALL keys\n")
  119.    printf("\n")
  120.    printf("Example: To display all objects known to the workplace shell:\n");
  121.    printf("         DUMPINI OS2 PM_Workplace:Location\n");
  122.    printf("\n");
  123. }
  124.  
  125.