home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / SGROUP2.ZIP / SGROUP.C < prev    next >
C/C++ Source or Header  |  1991-01-08  |  5KB  |  222 lines

  1. #define INCL_WIN
  2. #define INCL_WINPROGRAMLIST
  3. #define INCL_DOS
  4. #include <os2.h>
  5.  
  6. #include <mt\search.h>
  7. #include <mt\stdio.h>
  8. #include <mt\conio.h>
  9. #include <mt\stdlib.h>
  10. #include <mt\string.h>
  11.  
  12. #include "sgroup.h"
  13.  
  14. CHAR  GroupName[BIG_STRING];
  15.  
  16. int    CompareProgs(const void * Elem1, const void * Elem2);
  17. void   ParseParms(SHORT Count, PSZ Arguments[]);
  18. SHORT  ParmType           (PSZ);
  19. void   ShowVersionInfo    (void);
  20. void   ErrorAndDie        (PSZ, USHORT);
  21. void   UserError          (PSZ);
  22. void   SortGroup          (PSZ pName);
  23. void   SortByHandle       (HPROGRAM hSort, HPROGRAM hOld);
  24.  
  25. void main (int argv, char *argc[])
  26.   {
  27.   HAB hab;
  28.   
  29.   ShowVersionInfo();
  30.   hab = WinInitialize(0);
  31.   
  32.   ParseParms(argv, argc);
  33.   SortGroup(GroupName);
  34.   
  35.   WinTerminate(hab);
  36.   DosExit(EXIT_PROCESS, 0);
  37.   }
  38.  
  39. void ShowVersionInfo(void)
  40.   {
  41.   printf("\nSGroup Version %s: 1990, Mike Donnelly\n\n", SGROUP_VERSION);
  42.   }
  43.   
  44. void ParseParms(SHORT Count, PSZ Arguments[])
  45.   {
  46.   BOOL      GotFile  = FALSE;
  47.   BOOL      NeedHelp = FALSE;
  48.   PSZ       Argument;
  49.   CHAR      LastChar;
  50.   
  51.   if (Count == 1)
  52.     NeedHelp = TRUE;
  53.  
  54.   while ( *(++Arguments) )
  55.     {
  56.     Argument = *(Arguments);
  57.     LastChar = *(Argument + (strlen(Argument) - 1) );
  58.  
  59.     switch ( ParmType(Argument) )
  60.       {
  61.       case PARM_HELP:
  62.       case PARM_BAD:
  63.         NeedHelp = TRUE;
  64.         break;
  65.         
  66.       case PARM_STRING:
  67.         GotFile = TRUE;
  68.         strcpy(GroupName, Argument);
  69.         break;
  70.       
  71.       default:
  72.         ErrorAndDie("Internal casing error at ParseParms", 1);
  73.         break;
  74.       }
  75.     }
  76.     
  77.   if (NeedHelp == TRUE)
  78.     {
  79.     printf("Usage: SGROUP groupname\n");
  80.     DosExit(EXIT_PROCESS, 0);
  81.     }
  82.     
  83.   if (GotFile == FALSE)
  84.     UserError("No groupname specified.");
  85.  
  86.   }
  87.  
  88. SHORT ParmType(PSZ Parm)
  89.   {
  90.   SHORT Type = PARM_NONE;
  91.   
  92.   if ( (*Parm == '-') ||
  93.        (*Parm == '/') )
  94.     {
  95.     switch ( toupper( *(Parm + 1)) )
  96.       {
  97.       case '?':
  98.       case 'H':
  99.         Type = PARM_HELP;
  100.         break;
  101.         
  102.       default:
  103.         Type = PARM_BAD;
  104.         break;
  105.       }
  106.     }
  107.   else
  108.     Type = PARM_STRING;
  109.     
  110.   return(Type);
  111.   }
  112.   
  113. void UserError(PSZ Text)
  114.   {
  115.   printf("SGROUP: %s\n", Text);
  116.   DosExit(EXIT_PROCESS, 1);
  117.   }
  118.   
  119. void ErrorAndDie(PSZ Message, USHORT Error)
  120.   {
  121.   printf("SGROUP: %s error %u\n", Message, Error);
  122.   DosExit(EXIT_PROCESS, Error);
  123.   }
  124.   
  125.   
  126. void SortGroup(PSZ pName)
  127.   {
  128.   BOOL         GotIt = FALSE;
  129.   USHORT       Index;
  130.   ULONG        BufSize;
  131.   ULONG        Titles;
  132.   PPROGDETAILS pDetails;
  133.   PPROGTITLE   pGroups = NULL;
  134.   PPROGTITLE   pBase;
  135.   HPROGRAM     hgroupOld;
  136.   HPROGRAM     hgroupNew;
  137.   
  138.   BufSize = PrfQueryProgramTitles(HINI_USERPROFILE, SGH_ROOT, pGroups,
  139.               0L, &Titles);
  140.               
  141.   pGroups = malloc( (USHORT) BufSize);
  142.   pBase   = pGroups;
  143.   
  144.   PrfQueryProgramTitles(HINI_USERPROFILE, SGH_ROOT, pGroups, BufSize, &Titles);
  145.   
  146.   for (Index = 0; Index < (USHORT) Titles; Index++)
  147.     {
  148.     if ( stricmp(pGroups->pszTitle, pName) == 0)
  149.       {
  150.       GotIt = TRUE;
  151.       break;
  152.       }
  153.       
  154.     pGroups++;
  155.     }
  156.     
  157.   if (GotIt == FALSE)
  158.     UserError("No such group.");
  159.     
  160.   BufSize = PrfQueryDefinition(HINI_USERPROFILE, pGroups->hprog, NULL, 0L);
  161.   
  162.   pDetails = malloc( (USHORT) BufSize);
  163.   
  164.   PrfQueryDefinition(HINI_USERPROFILE, pGroups->hprog, pDetails, BufSize);
  165.  
  166.   strcpy(pDetails->pszTitle, "Temp");
  167.   free(pDetails);
  168.   
  169.   PrfChangeProgram(HINI_USERPROFILE, pGroups->hprog, pDetails);
  170.   hgroupNew = PrfCreateGroup(HINI_USERPROFILE, pGroups->pszTitle,
  171.                  SHE_VISIBLE | SHE_UNPROTECTED);
  172.    
  173.   hgroupOld = pGroups->hprog;
  174.                
  175.   free(pBase);
  176.   SortByHandle(hgroupNew, hgroupOld);
  177.   }
  178.   
  179. void SortByHandle(HPROGRAM hSort, HPROGRAM hOld)
  180.   {
  181.   USHORT  Index;
  182.   ULONG   BufSize;
  183.   ULONG   Titles;
  184.   PPROGDETAILS pDetails;
  185.   PPROGTITLE   pProgs = NULL;
  186.   PPROGTITLE   pBase;
  187.  
  188.   BufSize = PrfQueryProgramTitles(HINI_USERPROFILE, hOld, pProgs,
  189.               0L, &Titles);
  190.               
  191.   pProgs  = malloc( (USHORT) BufSize);
  192.   pBase   = pProgs;
  193.   
  194.   PrfQueryProgramTitles(HINI_USERPROFILE, hOld, pProgs, BufSize, &Titles);
  195.   
  196.   pProgs = pBase;
  197.   qsort(pBase, (USHORT) Titles, sizeof(PROGTITLE), CompareProgs);
  198.   
  199.   for (Index = 0; Index < (USHORT) Titles; Index++)
  200.     {
  201.     printf("P: %02u --> %s\n", Index, pProgs->pszTitle);
  202.     BufSize = PrfQueryDefinition(HINI_USERPROFILE, pProgs->hprog, 0, 0L);
  203.     
  204.     pDetails = malloc( (USHORT) BufSize);
  205.     PrfQueryDefinition(HINI_USERPROFILE, pProgs->hprog, pDetails, BufSize);
  206.     PrfAddProgram(HINI_USERPROFILE, pDetails, hSort);
  207.     free(pDetails);
  208.     
  209.     pProgs++;
  210.     }
  211.   pProgs = pBase;
  212.   PrfDestroyGroup(HINI_USERPROFILE, hOld);
  213.   
  214.   free(pBase);
  215.   }            
  216.   
  217. int CompareProgs(const void * Elem1, const void * Elem2)
  218.   {
  219.   return( strcmp( ( (PPROGTITLE) Elem1)->pszTitle,
  220.                   ( (PPROGTITLE) Elem2)->pszTitle) );
  221.   }
  222.