home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
cidsam.zip
/
HLRFIO.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-06-28
|
20KB
|
478 lines
/*********************************/
/* NOTE */
/* */
/* This sample code has been */
/* provided by IBM. It is not */
/* warranted for any particular */
/* use or purpose. */
/* */
/* IBM releases this code into */
/* the public domain. You may */
/* use it, modify it, or */
/* incorporate it into other */
/* products without restriction. */
/*********************************/
/*********************************************************************/
/* */
/* MODULE NAME: hlrfio.c */
/* */
/* DESCRIPTIVE NAME: Higher level (than rfio.c) routines for */
/* Response File I/O */
/* */
/* LINKAGE: These are linkable subroutines. They require */
/* CID_findfile() and CID_log() be linked in also. */
/* */
/*********************************************************************/
#pragma page (1)
/*
A higher-level interface to the rfio package. For those who are
satisfied with the error handling and logging implemented here,
and who
a) Use Include (NOT case sensitive) as a keyword to
indicate include files.
b) Are happy with sequential processing of includes
the response file interface can be reduced to four calls:
UINT RFOpen(char *pszFilename)
which opens the file named filename as a response file.
It returns 0 for success, 1 for failure.
UINT RFGetNextKeyword(char **ppszKeyword, char **ppszValue,
UINT *puiType)
which stores pointers to the next keyword and value, and stores
an indicator of the type of value (string or list) (RFLIST and
RFSTRING respectively).
Do not issue a free() against *ppxzKeyword or *ppszValue.
If you need a copy of the keyword and/or value, make one.
The values will be erased and the space they take up will be
freed on the next call to RFGetNextKeyword().
This function returns RF0 as long as everything is OK,
RFEOF when the last keyword and value have already been returned,
or RFERR when a fatal error has occured.
UINT RFGetNextKwdInList(char **ppszStart, char **ppszKeyword,
char **ppszValue, UINT *puiType)
which works just like RF_Get_Next_Kwd_In_List();
Like RFGetNextKeyword, it returns RF0 as long as everything is
OK, RFEOF when the last keyword and value have already been
returned, or RFERR when a fatal error has occured.
UINT RFClose(void)
which closes a response file. RFClose is not required unless the
caller wants to free resources (memory and file handles) in use
by rfio. A call to RFOpen automatically closes down any previously
open Response File, so there is no need to call RFClose() between
Response Files. This function always returns 0.
char *RFCopyString(char *string)
which makes a *persistent* copy of a string value
VOID *RFCopyList(void *from)
which makes a *persistant* copy of a list.
VOID *RFMergeLists(void *list1, void *list2)
which merges the values in two lists. The algorithm is to examine
each keyword in list1. If there is a corresponding keyword in list2,
then the _value_ from list2 is assigned to the keyword in the
merged list. Any unmatched keywords in either list appear in the
merged list. If list1 and list2 have a keyword to which imbedded
lists are assigned, then the imbedded lists are *not* merged; the
list2 imbedded list is assigned to the keyword.
char *RFCopyString(char *string)
which makes a persistent copy of a string value
UINT RFSizeOfList(void *list)
which returns the size in bytes of the list whose address is passed.
The entire length, including terminating null, in included in the
value returned.
*/
#pragma page (1)
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#define INCL_BASE
#include <os2.h>
#include "rfioe.h"
#include "hlrfio.h"
/****************************************************************************/
/* G L O B A L V A R I A B L E S */
/****************************************************************************/
RFISP rfisp = (RFISP)NULL; /* pointer to the imbed file chain */
RFHANDLE rfh;
char *pszRFtempkw = NULL;
char *pszRFtempval = NULL;
char *pszRFLtempkw = NULL;
char *pszRFLtempval = NULL;
/****************************************************************************/
/* F U N C T I O N D E C L A R A T I O N */
/****************************************************************************/
extern void CID_Log(UINT uiMsgno, ...);
#pragma page (1)
/****************************************************************************/
/* R F O p e n */
/****************************************************************************/
UINT RFOpen(char *pszFilename)
{
int rc;
if (rfisp) /* If there is an open file already */
RFClose(); /* Close it out */
if ( (rfisp = RF_Create_Root_ISE()) == (RFISP)NULL)
{
CID_Log(HRF_OOS_ERROR, NULL);
return(RFHERR);
}
else
if ( (rfh = RF_Open_File(pszFilename, rfisp, &rc)) == NULL )
{
CID_Log(HRF_OPEN_MAIN_FILE_ERROR, pszFilename, NULL);
free(rfisp);
rfisp = (RFISP)NULL;
return(RFHERR);
}
else
{
pszRFtempkw = NULL;
pszRFtempval = NULL;
return(RFH0);
}
}
#pragma page (1)
/****************************************************************************/
/* R F G e t N e x t K e y w o r d */
/****************************************************************************/
UINT RFGetNextKeyword(char **ppszKw, char **ppszVal, UINT *puiType)
{
int rc;
char caNumbuf[17];
int iRFtemptype;
if (pszRFtempkw) /* free up memory from last call */
{ /* @U1a */
free(pszRFtempkw);
pszRFtempkw = NULL; /* @U1a */
} /* @U1a */
if (pszRFtempval)
{ /* @U1a */
free(pszRFtempval);
pszRFtempval = NULL; /* @U1a */
} /* @U1a */
while(1)
{
rc = RF_Get_Next_Kwd_In_File(rfh, &pszRFtempkw, &pszRFtempval,
&iRFtemptype);
switch(rc)
{
case RFSYNTAX: CID_Log(HRF_SYNTAX_ERROR,
itoa(iRFtemptype, caNumbuf, 10),
RF_Get_Current_Filename(rfisp),
pszRFtempval, NULL);
return(RFHERR);
break;
case RFOOS: CID_Log(HRF_OOS_ERROR, NULL);
return(RFHERR);
break;
case RFERR: CID_Log(HRF_DOSIO_ERROR,
RF_Get_Current_Filename(rfisp),
iRFtemptype, NULL);
return(RFHERR);
break;
case RFEOF: if ( (rfh = RF_Close_File(rfh, rfisp)) == NULL)
return(RFHEOF);
break;
case RF0: if (stricmp(pszRFtempkw, "include") == 0)
{
rfh = RF_Open_File(pszRFtempval, rfisp, &rc);
if (!rfh)
{
CID_Log(HRF_OPEN_IMBED_FILE_ERROR,
pszRFtempval, NULL);
return(RFHERR);
}
}
else
{
*ppszKw = pszRFtempkw;
*ppszVal = pszRFtempval;
*puiType = iRFtemptype;
return(RFH0);
}
break;
default: CID_Log(HRF_BAD_GETNEXT_RC,
itoa(rc, caNumbuf, 10), NULL);
return(RFHERR);
break;
} /* End SWITCH on rc */
}
}
#pragma page (1)
/****************************************************************************/
/* R F G e t N e x t K e y w o r d I n L i s t */
/****************************************************************************/
UINT RFGetNextKwdInList(char **ppszStart, char **ppszKeyword,
char **ppszValue, UINT *puiType)
{
char *p;
char caNumbuf[17];
int rc;
int iRFLtemptype;
if (pszRFLtempkw) /* free up memory from last call */
{ /* @U1a */
free(pszRFLtempkw); /* @U1c */
pszRFLtempkw = NULL; /* @U1a */
} /* @U1a */
if (pszRFLtempval)
{ /* @U1a */
free(pszRFLtempval); /* @U1c */
pszRFLtempval = NULL; /* @U1a */
} /* @U1a */
p = *ppszStart;
rc = RF_Get_Next_Kwd_In_List(&p, &pszRFLtempkw, &pszRFLtempval,
&iRFLtemptype);
switch (rc)
{
case RFSYNTAX: CID_Log(HRF_LIST_SYNTAX_ERROR,
itoa(iRFLtemptype, caNumbuf, 10),
RF_Get_Current_Filename(rfisp),
pszRFLtempval, NULL);
return(RFHERR);
break;
case RFOOS: CID_Log(HRF_LIST_OOS_ERROR, NULL);
return(RFHERR);
break;
case RFERR: CID_Log(HRF_LIST_DOSIO_ERROR,
RF_Get_Current_Filename(rfisp),
iRFLtemptype, NULL);
return(RFHERR);
break;
case RFEOF: return(RFHEOF);
break;
case RF0: *ppszStart = p;
*ppszKeyword = pszRFLtempkw;
*ppszValue = pszRFLtempval;
*puiType = iRFLtemptype;
return(RFH0);
break;
default: CID_Log(HRF_BAD_GETNEXT_RC,
itoa(rc, caNumbuf, 10), NULL);
return(RFHERR);
break;
}
}
#pragma page (1)
/****************************************************************************/
/* R F C l o s e */
/****************************************************************************/
UINT RFClose()
{ /* Close it out */
if (pszRFLtempkw) /* free up memory from last call */
{ /* @C1a */
free(pszRFLtempkw); /* @C1c */
pszRFLtempkw = NULL; /* @C1a */
} /* @C1a */
if (pszRFLtempval) /* @C1a */
{ /* @C1a */
free(pszRFLtempval); /* @C1c */
pszRFLtempval = NULL; /* @C1a */
} /* @C1a */
if (pszRFtempkw) /* @C1a */
{ /* @C1a */
free(pszRFtempkw); /* @C1a */
pszRFtempkw = NULL; /* @C1a */
} /* @C1a */
if (pszRFtempval) /* @C1a */
{ /* @C1a */
free(pszRFtempval); /* @C1a */
pszRFtempval = NULL; /* @C1a */
} /* @C1a */
while (RF_Close_File(rfh, rfisp));
free(rfisp);
rfisp = (RFISP)NULL;
return(RFH0);
}
#pragma page (1)
/****************************************************************************/
/* R F C o p y S t r i n g */
/****************************************************************************/
/****************************************************************************/
/* This routine makes a permanent copy of the string that is passed to it */
/* and returns the address of the string. */
/****************************************************************************/
char *RFCopyString(char *model)
{
char *p;
/* Allocate memory for the */
p = (char *)malloc(strlen(model) + 1); /* permanent copy */
if (p) /* If memory was allocated */
strcpy(p, model); /* make the copy. */
return(p);
}
/****************************************************************************/
/* R F C o p y L i s t */
/****************************************************************************/
void * RFCopyList(void *from)
{
unsigned int listsize;
char *p;
char lastchar;
if (!from) /* get out if a bad value was passed */
return(NULL);
lastchar = ' ';
/* malloc enough space for the list, and copy @C1a*/
/* the list into the malloced space. @C1a*/
listsize = RFSizeOfList(from);
p = malloc(listsize);
if (p)
memcpy((void *)p, from, listsize);
else
CID_Log(HRF_OOS_ERROR, NULL);
return(p);
}
/****************************************************************************/
/* R F M e r g e L i s t s */
/****************************************************************************/
/* We don't bother to sort. Lists will seldom be long enough to make it */
/* worth while. */
/****************************************************************************/
void *RFMergeLists(void *list1, void *list2)
{
char *start1, *start2, *startm;
char *keyword1, *keyword2, *keywordm;
char *value1, *value2, *valuem;
void *mergedList;
UINT type1, type2, typem;
UINT merged_size, found;
char lastchar;
/* Get worst-case size of combined lists */
merged_size = RFSizeOfList(list1) + RFSizeOfList(list2);
/* Allocate enough memory for the worst case. */
mergedList = (void *)malloc(merged_size);
if (!mergedList) /* Bail out if no memory is available. */
return(NULL);
startm = mergedList; /* This pointer will increase as we fill the */
/* merged list. */
lastchar = ' '; /* First, copy in all of list2. */
for (start2 = list2; *start2 || lastchar; *startm++ = *start2++)
lastchar = *start2;
start2 -= 3; /* back up over )00 */
start1 = list1; /* Now, pick up anything unmatched from list1 */
while (RFGetNextKwdInList(&start1, &keyword1, &value1, &type1) == RFH0)
{
keywordm = RFCopyString(keyword1);
if (type1 == RFHLIST)
{
typem = RFHLIST;
valuem = RFCopyList(value1);
}
else
{
typem = RFHSTRING;
valuem = RFCopyString(value1);
}
start2 = list2; /* Try to match w/ a kwd in list 2 */
found = 0;
while (RFGetNextKwdInList(&start2,&keyword2,&value2,&type2) == RFH0)
{
if (stricmp(keywordm, keyword2) == 0) /* match found */
{
found = 1;
break;
}
}
if (!found)
{
strcpy(startm, keywordm);
strcat(startm,"=");
startm += (strlen(keywordm) + 1);
if (typem == RFHSTRING)
{
strcpy(startm, valuem);
startm += (strlen(valuem) + 1);
}
else
{
lastchar = ' ';
for (start2 = valuem; *start2 || lastchar; *startm++ = *start2)
lastchar = *start2++;
}
}
free(keywordm);
free(valuem);
}
*startm = '\0'; /* Put in the terminating (double) null. */
/* Make a copy of the list with the correct*/
/* size (we had assumed worst-case before) */
startm = RFCopyList(mergedList);
free(mergedList); /* Free the worst-case space. */
return(startm); /* Return pointer to merged list. */
}
/****************************************************************************/
/* R F S i z e O f L i s t */
/****************************************************************************/
UINT RFSizeOfList(void *list)
{
UINT size = 0;
char *p, c;
c = 0;
p = (char *)list;
while (1) /* Look for two consecutive null bytes. */
{ /* Everything up to and including that is part of */
size++; /* the list. */
if (!*p && !c)
return(size);
c = *p++;
}
}