home *** CD-ROM | disk | FTP | other *** search
- /*==============================================================================
- REP_MISC.C
- Report Ease miscellaneous functions.
-
- ReportEase Plus
- Sub Systems, Inc.
- ReportEase Plus, Copyright (c) 1993, Sub Systems, Inc. All Rights Reserved.
- 159 Main Street, #8C, Stoneham, MA 02180
- (617) 438-8901.
-
- Software License Agreement (1993)
- ----------------------------------
- This license agreement allows the purchaser the right to modify the
- source code to incorporate in their application.
- The target application must not be distributed as a standalone report writer
- or a mail merge product.
- Sub Systems, Inc. reserves the right to prosecute anybody found to be
- making illegal copies of the executable software or compiling the source
- code for the purpose of selling there after.
-
- ===============================================================================*/
- #include "windows.h"
-
- #if defined (_WIN32)
- #if !defined(WIN32)
- #define WIN32
- #endif
- #endif
- #if !defined(WIN32)
- #include "print.h"
- #endif
-
- #include "stdio.h"
- #include "stdlib.h"
- #include "ctype.h"
- #include "fcntl.h"
- #include "io.h"
- #include "sys\types.h"
- #include "sys\stat.h"
- #include "string.h"
- #include "dos.h"
- #include "setjmp.h"
-
- #include "rep.h"
-
- #define PREFIX extern
- #include "rep1.h"
-
- /*****************************************************************************
- RepGetParent:
- Get the parent window handle. Needed by RVB.VBX
- ******************************************************************************/
- HWND WINAPI _export RepGetParent()
- {
- if (SessionType=='F') return FormArg.hParentWnd;
- else return RepArg.hParentWnd;
- }
-
- /******************************************************************************
- GetFileLength:
- Get the size of the given file in number of bytes. This function returns
- -1 if an error occurs
- *******************************************************************************/
- long GetFileLength(LPSTR FileName)
- {
- HFILE iFile;
- long len=-1L;
-
- if ((iFile=_lopen(FileName,OF_READ))==HFILE_ERROR) return len;
-
- if (HFILE_ERROR==(len=_llseek(iFile,0,2))) return (long)-1; // position at the end of the file
-
- _lclose(iFile);
-
- return len;
- }
-
- /*****************************************************************************
- GetSystemDateTime:
- Get the system date and time
- ******************************************************************************/
- void GetSystemDateTime()
- {
- #if defined (WIN32)
- SYSTEMTIME DateTime;
-
- GetSystemTime(&DateTime);
- SystemDate=(long)DateTime.wYear*10000L+(long)DateTime.wMonth*100L+(long)DateTime.wDay;
- wsprintf(SystemTime,"%02d:%02d:%02d",DateTime.wHour,DateTime.wMinute,DateTime.wSecond);
-
- #else
- union REGS reg;
- union REGS oreg;
- struct SREGS sreg;
-
- //***************** get system date time **********************************
- // get date
- reg.h.ah=0x2a;
- segread(&sreg);
- sreg.es=sreg.ds; // a valid value for ES
- int86x(0x21,®,&oreg,&sreg);
-
- SystemDate=(long)oreg.x.cx*10000L+(long)oreg.h.dh*100L+(long)oreg.h.dl;
-
- // get time
- reg.h.ah=0x2c;
- segread(&sreg);
- sreg.es=sreg.ds; // a valid value for ES
- int86x(0x21,®,&oreg,&sreg);
- wsprintf(SystemTime,"%02d:%02d:%02d",oreg.h.ch,oreg.h.cl,oreg.h.dh);
- #endif
-
- }
-
- /*****************************************************************************
- StrTrim:
- Remove spaces from the left and right of a NULL terminated string.
- ******************************************************************************/
- void StrTrim(LPSTR string)
- {
- rTrim(string);lTrim(string);
- }
-
- /******************************************************************************
- rTrim:
- Remove spaces on the right of a string.
- *****************************************************************************/
- void rTrim(LPSTR string)
- {
- int i,TempLen;
-
- TempLen=lstrlen(string);
- for (i=TempLen-1;i>=0 && string[i] == ' ';i--);
- string[i+1] = '\0';
- }
-
- /******************************************************************************
- lTrim:
- Trim initial spaces from the string.
- *******************************************************************************/
- void lTrim(LPSTR string)
- {
- int i,TempLen,BeginPoint;char TempStr[LINE_WIDTH+2];
-
- TempLen=lstrlen(string);
- for (i=0;i < TempLen && string[i] == ' ';i++);
- BeginPoint=i;
- for (i=BeginPoint;i<TempLen;i++) TempStr[i-BeginPoint]=string[i];
- TempStr[TempLen-BeginPoint] = '\0';
- lstrcpy(string,TempStr);
- }
-
- /*******************************************************************************
- lPad:
- Insert spaces before the string such that the string length becomes
- as specified.
- length=Final length;
- *******************************************************************************/
- lPad(LPSTR string,int length)
- {
- int i,len,spaces;
-
- len=lstrlen(string);
- if (len>=length) return TRUE;
-
- spaces=length-len;
-
- FarMoveOl(string,&string[spaces],len+1);
- for (i=0;i<spaces;i++) string[i]=' ';
-
- return TRUE;
- }
-
- /*******************************************************************************
- rPad:
- Insert spaces after the string such that the string length becomes
- as specified.
- length=Final length;
- *******************************************************************************/
- rPad(LPSTR string,int length)
- {
- int i,len,spaces;
-
- len=lstrlen(string);
- if (len>=length) return TRUE;
-
- spaces=length-len;
-
- for (i=0;i<spaces;i++) string[len+i]=' ';
- string[len+i]=0;
- return TRUE;
- }
- /******************************************************************************
- lstrupr:
- Converts a far string to upper case.
- *******************************************************************************/
- LPSTR lstrupr(LPSTR string)
- {
- int i,len;
-
- len=lstrlen(string);
-
- for (i=0;i<len;i++) {
- string[i]=toupper(string[i]);
- }
- return string;
- }
-
- /******************************************************************************
- lstrlwr:
- Converts a far string to lower case.
- *******************************************************************************/
- LPSTR lstrlwr(LPSTR string)
- {
- int i,len;
-
- len=lstrlen(string);
-
- for (i=0;i<len;i++) {
- string[i]=tolower(string[i]);
- }
- return string;
- }
-
- /******************************************************************************
- lstrstr:
- This routine returns the location of the second string within the
- first string.
- *******************************************************************************/
- LPSTR lstrstr(LPSTR string1,LPSTR string2)
- {
- int len1,len2,ScanLen,i;
-
- len1=lstrlen(string1);
- len2=lstrlen(string2);
-
- if (len1<len2) return NULL; // first string too small to contain the second string
-
- ScanLen=len1-len2;
-
- for (i=0;i<=ScanLen;i++) if (StringBegins(&string1[i],string2)) return &string1[i];
-
- return NULL;
- }
-
- /******************************************************************************
- StringBegin:
- This routine returns a TRUE value of the first string begins with
- the second string.
- *******************************************************************************/
- BOOL StringBegins(LPSTR string1,LPSTR string2)
- {
- int len1,len2,i;
-
- len1=lstrlen(string1);
- len2=lstrlen(string2);
-
- if (len1<len2) return FALSE; // first string too small to contain the second string
-
- for (i=0;i<len2;i++) if (string1[i]!=string2[i]) return FALSE;
-
- return TRUE;
- }
-
- /******************************************************************************
- AssignNumVal:
- Extract a numeric value from the dialog item. Display error if a non-
- numeric value is encountered and return a false result.
- *******************************************************************************/
- BOOL AssignNumVal(HWND hWin,int ControlId,int *pInt)
- {
- BOOL ErrorFlag;
-
- *pInt=GetDlgItemInt(hWin,ControlId,&ErrorFlag,(BOOL) 0);
- if (!ErrorFlag) {
- MessageBox(hWin,"Must Enter a Valid Number",NULL,MB_OK);
- SetFocus(GetDlgItem(hWin,ControlId));
- return (FALSE);
- }
- return TRUE;
- }
-
- /******************************************************************************
- CheckDlgValue:
- This routine checks if the user entered a numeric value within the given
- range. If a value is found outside the range, the routine displays
- an error message, sets the focus on the item, and returns a false value.
- *******************************************************************************/
- BOOL CheckDlgValue(HWND hDlg,char type,int id,double BeginRange,double EndRange)
- {
- double value;
- BOOL flag=TRUE;
- char string[100];
-
- if (type=='I') {
- value=(double)GetDlgItemInt(hDlg,id,&flag,0);
- if (!flag) goto ERROR_FOUND;
- }
- else {
- GetDlgItemText(hDlg,id,string,100);
- value=atof(string);
- }
-
- if (value<BeginRange || value>EndRange) goto ERROR_FOUND;
-
- return TRUE;
-
- ERROR_FOUND:
- MessageBox(hDlg,"Invalid Value, Please Re-enter","Value Out of Range",MB_OK);
- SetFocus(GetDlgItem(hDlg,id));
- return FALSE;
- }
-
- /******************************************************************************
- CurrentPrinter:
- Get information (name,driver,port) for the currently selected printer.
- The function returns FALSE if the current printer not set.
- ******************************************************************************/
- BOOL CurrentPrinter(LPSTR name,LPSTR driver,LPSTR port)
- {
- int len;
- char ext[5];
-
- if (!ParseProfileString("windows","device", name,driver,port)) {
- MessageBox(hFrParentWnd,"Unable to Access the Current Printer!",NULL,MB_OK);
- return FALSE;
- }
-
- // add .drv extension to the driver name, if necessary
- len=lstrlen(driver);
- if (len<=4) lstrcat(driver,".drv");
- else {
- lstrcpy(ext,&driver[len-4]);
- strupr(ext);
- if (strcmp(ext,".DRV")!=0) lstrcat(driver,".DRV");
- }
-
- return TRUE;
- }
-
- /******************************************************************************
- ParseProfileString:
- This routine retrieves the profile string for the given application
- and key value. It then parses the string and returns first 3 strings
- delimited by comma characters.
- ******************************************************************************/
- BOOL ParseProfileString(LPSTR appl,LPSTR key, LPSTR string1, LPSTR string2, LPSTR string3)
- {
- LPSTR temp,str1=0,str2=0,str3=0;
- char string[100];
-
- string1[0]=string2[0]=string3[0]=0; // initialize the output strings
-
- if (!GetProfileString(appl,key,(LPSTR) "",string,99)) return FALSE;
-
- temp=str1=string; // beginning pointer for the scan
-
- while(*temp) { // scan until a NULL is found
- if (*temp==',') { // a delimited found
- *temp=0; // NULL terminate the previous string, and advance the pointer
- temp++;
- while (*temp==' ') temp=AnsiNext(temp); // skip leading spaces
- if (!str2) str2=temp; // Initiate the driver string if not found yet
- else if (!str3) str3=temp; // else it is the last string, i.e. the port string
- else break;
- }
- else temp=AnsiNext(temp); // advance the pointer
- }
-
- if (str1) lstrcpy(string1,str1); // copy the strings to the user specified area
- if (str2) lstrcpy(string2,str2);
- if (str3) lstrcpy(string3,str3);
-
- return TRUE;
- }
-
- /******************************************************************************
- ValidateDate:
- Validate a date in a string and convert to its numeric value (YYYYMMDD).
- The input date is in a quoted string.
- ******************************************************************************/
- int ValidateDate(LPSTR InDate,long far *NumValue)
- {
- char string[20],delim=0,CurChar;
- int i,j,len,mm,dd,yy,TotalDelims;
- long temp;
-
- len=lstrlen(InDate);
- if (len<8 || len>12) return FALSE; // invalid date
-
- TotalDelims=0;
-
- j=0;
- mm=dd=yy=0;
- for(i=1;i<(len-1);i++) { // omit beginnning and ending quotes
- CurChar=InDate[i];
- if (CurChar>='0' && CurChar<='9') {
- string[j]=CurChar;
- j++;
- }
- else if (strchr("-/,",CurChar)) {
- if (j==0 || j>2) return FALSE; // not a valid date format
- if (TotalDelims>2) return FALSE; // not a valid date format
- if (TotalDelims==1 && delim!=CurChar) return FALSE;
- string[j]=0;
- if (TotalDelims==0) {
- if (FormHdr.DateFormat==0) mm=atoi(string);
- else dd=atoi(string);
- }
- else {
- if (FormHdr.DateFormat==0) dd=atoi(string);
- else mm=atoi(string);
- }
- j=0;
- delim=CurChar;
- TotalDelims++;
- }
- else return FALSE; // invalid character
- }
- if (j==0 || j>4) return FALSE; // not a valid date format
- string[j]=0;
- yy=atoi(string);
-
- // validate the date
- (*NumValue)=-1; // marks an invalid date
- if (mm<1 || mm>12) return TRUE;
- if (dd<1 || dd>31) return TRUE;
- if ((mm==4 || mm==6 || mm==9 || mm==11) && dd>30) return TRUE;
- if (mm==2) { // check leap year
- if (yy==(yy/4)*4) {
- if (dd>29) return TRUE;
- }
- else if (dd>28) return TRUE;
- }
-
- //********** convert to the YYYYMMDD numeric value **********
- if (yy<=99) yy+=1900;
- temp=yy;
- temp=(temp*10000L)+mm*100+dd;
- (*NumValue)=temp;
-
- return TRUE;
- }
-
- /******************************************************************************
- DoubleToStr:
- This routine converts a double value to a string. The second argument
- provides the digits after the decimal point.
- The function returns the pointer to the third argument after storing
- the result in the third argument.
- *******************************************************************************/
- LPSTR DoubleToStr(double nbr,int dec,LPSTR out)
- {
- long i,len,BeforeDec,AfterDec,multiply;
- char string[20];
- double dAfterDec;
-
- out[0]=0;
-
- //************* apply sign *******
- if (nbr<0) {
- lstrcat(out,"-");
- nbr=-nbr;
- }
-
- BeforeDec = (long) nbr; // number before the decimal point
-
- //****** append the digits after the decimal point ****
- if (dec>0) {
- nbr=nbr-(double)BeforeDec;
- multiply=1;
- for (i=0;i<dec;i++) multiply=multiply*10;
- dAfterDec=nbr*(double)multiply;
- AfterDec=(long)dAfterDec;
- nbr=dAfterDec-(double)AfterDec;
- if (nbr>=.5) AfterDec++;
- if (AfterDec>=multiply) { // overflow
- BeforeDec++;
- AfterDec=0;
- }
-
- //************* make the out *****************
- if (BeforeDec>0) {
- ltoa(BeforeDec,string,10);
- lstrcat(out,string);
- }
- lstrcat(out,".");
-
- ltoa(AfterDec,string,10);
- len=strlen(string);
- for (i=len;i<dec;i++) lstrcat(out,"0"); // first pad with necessary space
- lstrcat(out,string); // then append the digits
- }
- else if (BeforeDec>0) {
- ltoa(BeforeDec,string,10);
- lstrcat(out,string);
- }
-
- return out;
- }
-
- /******************************************************************************
- Copy a specified number of bytes from the source to the destination location.
- Both the source and destination locations are specified by using far pointers.
-
- When source and destination memory overlaps, use FarMoveOl function for
- a proper trasfer.
-
- Small/Medium Model Note: This function will not work if compiled
- as an EXECUTABLE under small/medium model using the Borland compiler.
- This is because Borland 'C' does not have a fmemmove library function.
- *******************************************************************************/
- void FarMove(void far * src,void far * dest, UINT count)
- {
- #if defined(WIN32)
- memmove(dest,src,count);
- #else
- WORD i,SrcOff,DestOff,SrcSeg,DestSeg;
- char huge *SrcPtr,huge *DestPtr;
-
- if (count==0) return; // nothing to move
-
- SrcSeg=HIWORD((DWORD)(src)); // source segment
- DestSeg=HIWORD((DWORD)(dest)); // destination segment
- SrcOff=LOWORD((DWORD)(src)); // source offset
- DestOff=LOWORD((DWORD)(dest)); // destination offset
-
- // use _fmemmove function if source and destination do not wrap
- if ((SrcOff+count)>SrcOff && (DestOff+count)>DestOff) _fmemmove(dest,src,count);
- else { // move one by one if source of destination wrap
- SrcPtr=(char huge *)src;
- DestPtr=(char huge *)dest;
-
- if ( (GetSelectorBase(SrcSeg)+SrcOff)
- > (GetSelectorBase(DestSeg)+DestOff) ){ // source at higher address
- for (i=0;i<count;i++) DestPtr[i]=SrcPtr[i];
- }
- else { // src at lower address, move in reverse order
- for (i=count-1;i!=0xFFFF;i--) DestPtr[i]=SrcPtr[i];
- }
- }
-
- #endif
-
- return;
-
- }
-
- /******************************************************************************
- Copy a specified number of bytes from the source to the destination location.
- Both the source and destination locations are specified by using far pointers.
- *******************************************************************************/
- void FarMoveOl(void far * src,void far * dest, UINT count)
- {
- FarMove(src,dest,count); // current version of FarMove takes care of overlapping
- return;
- }
-
- /******************************************************************************
- FarStringIndex:
- This function returns the position of the second string within the first
- string. The position is an index from the beginning of the first string.
- If the string is located, the return value will range from 0 to length of
- first string minus the length of the second string.
- If the second string could not be located, the return value is equal to
- the length of the first string.
- ******************************************************************************/
- UINT FarStringIndex(LPSTR string1,LPSTR string2)
- {
- UINT i,j,k,len1,len2,SearchLen;
- char FirstChar;
-
- len1=lstrlen(string1);
- len2=lstrlen(string2);
- if (len2>len1) return len1; // the second string too big to fit within the first string
-
- FirstChar=string2[0]; // first character of the second string
-
- i=0;
- do {
- SearchLen=len1-len2-i+1; // length of the string to search
- j=FarCharIndex(&string1[i],FirstChar,SearchLen);
- if (j==SearchLen) return len1; // first character of the second string in the first string not found
-
- for (k=1;k<len2;k++) if (string1[i+j+k]!=string2[k]) goto CONTINUE_LOOP;
- return i+j;
-
- CONTINUE_LOOP:
- i=i+j+1; // repeat after the first character match location
- } while (i<=(len1-len2));
-
- return len1; // the string was not located
- }
-
- /******************************************************************************
- FarCharIndex:
- This function returns the position of the specified character within the
- given string. The position is an index from the beginning of the string.
- If the character is found, the return value will range from 0 to SearchLen
- minus 1 (index of the character).
- If the character could not be located, the return value is equal to
- SearchLen.
- ******************************************************************************/
- UINT FarCharIndex(LPSTR string,char chr, UINT SearchLen)
- {
- UINT i;
-
- for (i=0;i<SearchLen;i++) if (string[i]==chr) return i;
- return i;
- }
-
- /******************************************************************************
- FarMemSet:
- This function every byte of the given far buffer to the specified character.
- ******************************************************************************/
- void FarMemSet(void far *ptr,char chr, UINT count)
- {
- UINT i;
- LPSTR lptr;
-
- lptr=(LPSTR)ptr;
-
- for (i=0;i<count;i++) lptr[i]=chr;
- }
-
- /******************************************************************************
- OurPrintf:
- This routine formats and display a given set of arguments. The format
- is given by the first argument. The argument 2 to n contain the
- arguments for the specified format. The function uses MessageBox to
- display the formatted string.
- ******************************************************************************/
- OurPrintf(LPSTR fmt,...)
- {
- LPSTR ArgStart;
- char string[256];
-
- ArgStart=(LPSTR) &fmt; // pointer to first argument
- ArgStart=&ArgStart[4]; // go past the first argument
- wvsprintf(string,fmt,ArgStart);
- if (FindWindow("DBWin",NULL)) { // debug window open
- lstrcat(string,"\n");
- OutputDebugString(string); // send to the debug terminal
- }
- else MessageBox(NULL,string,NULL,MB_OK);
- return TRUE;
- }
-
-