home *** CD-ROM | disk | FTP | other *** search
/ The Developer Connection…ice Driver Kit for OS/2 3 / DEV3-D1.ISO / devtools / os2tk21j / c / samples / dllapi / dllib.c__ / dllib.c
Encoding:
C/C++ Source or Header  |  1993-03-12  |  9.3 KB  |  209 lines

  1. /*static char *SCCSID = "@(#)dllib.c    6.9 92/02/18";*/
  2. /*==============================================================*\
  3.  *  DLLIB.C - dynamic linking library procedures for the window *
  4.  *      (C) Copyright IBM Corporation 1992.                     *
  5.  *--------------------------------------------------------------*
  6.  *                                                              *
  7.  *  This module contains the Display file Procedures for the    *
  8.  *  user defined DOS called as well as any code they need       *
  9.  *                                                              *
  10.  *--------------------------------------------------------------*
  11.  *                                                              *
  12.  *  This source file contains the following functions:          *
  13.  *                                                              *
  14.  *    SearchFile  (pObj, pFile)                                 *
  15.  *    ReadFileProc(pFileText)                                   *
  16.  *    CalCulProc  (pMathEl)                                     *
  17.  *                                                              *
  18. \*==============================================================*/
  19.  
  20. /*--------------------------------------------------------------*\
  21.  *  Include files, macros, defined constants, and externs       *
  22. \*--------------------------------------------------------------*/
  23. #include "dllib.h"
  24.  
  25. /****************************************************************\
  26.  *                                                              *
  27.  * Name:    SearchFile(pObj, pFile)                             *
  28.  *                                                              *
  29.  * Purpose: This routine searches files which are matching the  *
  30.  *          pattern.                                            *
  31.  *                                                              *
  32.  * Usage:   After files searching, this module will create a    *
  33.  *          linking structure to store the matched files.       *
  34.  *                                                              *
  35.  * Returns: Dependent upon API return code.                     *
  36.  *                                                              *
  37. \****************************************************************/
  38. APIRET SearchFile (POBJSTRUCT pObj, PPFILELIST pFile)
  39. {
  40.    FILEFINDBUF3 findbuf;
  41.    HDIR         hDir = HDIR_CREATE;
  42.    ULONG        ulSearchCount = 1;
  43.    CHAR         szBuf[CCHMAXPATH];
  44.    APIRET       Ret=0;
  45.    PVOID        pvAddress=0;
  46.    PFILELIST    pTail=NULL, pNext=NULL;         /* The last file of list */
  47.                                              /* Change to selected drive */
  48.    Ret = DosSetDefaultDisk((ULONG)(*(pObj->szNewDriver) - '@'));
  49.    if(Ret)
  50.         return (Ret);
  51.                                          /* Change to selected directory */
  52.    memset(szBuf, 0, sizeof(szBuf));
  53.    strcpy(szBuf, pObj->szNewPath);
  54.    if(strlen(szBuf)!= 1 && *(szBuf+strlen(szBuf)-1) == '\\')
  55.        *(szBuf+strlen(szBuf)-1) = '\000';              /* Erase last '\' */
  56.    Ret = DosSetCurrentDir((PSZ)szBuf);
  57.    if(Ret)
  58.          return (Ret);
  59.  
  60.    DosSetMem(*pFile, sizeof(PFILELIST), PAG_READ|PAG_WRITE|PAG_EXECUTE);
  61.  
  62.    while(*pFile != NULL)                 /* Free the File list structure */
  63.    {
  64.       pTail = *pFile;
  65.       *pFile = (*pFile)->pNextFile;
  66.       DosFreeMem(pTail);
  67.    }
  68.    Ret = DosFindFirst((PSZ)pObj->szNewPattern, &hDir, FILE_NORMAL,
  69.                 &findbuf, sizeof(findbuf), &ulSearchCount, FIL_STANDARD);
  70.  
  71.    if(Ret)
  72.    {
  73.       if(Ret == ERROR_NO_MORE_FILES)                    /* No file found */
  74.           WinAlarm(HWND_DESKTOP, WA_ERROR);
  75.  
  76.           WinMessageBox(HWND_DESKTOP,
  77.                    HWND_DESKTOP,
  78.                    "No file is matched this pattern,\ncheck pattern or path.",
  79.                    (PSZ)"Warning !",
  80.                    IDM_MSGBOX,
  81.                    MB_OK | MB_INFORMATION | MB_MOVEABLE);
  82.       return (Ret);
  83.    }
  84.    else
  85.    {
  86.       if(!DosAllocMem(&pvAddress,sizeof(PFILELIST),
  87.                      PAG_READ|PAG_WRITE|PAG_COMMIT))
  88.       {                                      /* Store 1'st file into list */
  89.          memset(szBuf, 0, sizeof(szBuf));
  90.          strcat(szBuf, pObj->szNewDriver);
  91.          strcat(szBuf, pObj->szNewPath);
  92.          strcat(szBuf, findbuf.achName);
  93.          *pFile = (PFILELIST)pvAddress;
  94.          strcpy((*pFile)->szFileName, szBuf);
  95.          (*pFile)->pNextFile = NULL;
  96.       }
  97.    }
  98.    pTail = *pFile;
  99.    while(ulSearchCount)
  100.    {
  101.       Ret=DosFindNext(hDir, &findbuf, sizeof(findbuf), &ulSearchCount);
  102.       if(Ret)                           /* Ret != 0, DosFindNext is fail */
  103.       {
  104.         pNext = *pFile;               /* Protect the memory of file list */
  105.         if(pNext != NULL)
  106.         {
  107.           DosSetMem(pNext,sizeof(PFILELIST), PAG_GUARD|PAG_READ|PAG_WRITE);
  108.         }
  109.         if (Ret != ERROR_NO_MORE_FILES)
  110.            return Ret;                             /* Have error occured */
  111.         else
  112.            return 0;
  113.       }
  114.       memset(szBuf, 0, sizeof(szBuf));        /* More files are searched */
  115.       strcat(szBuf, pObj->szNewDriver);
  116.       strcat(szBuf, pObj->szNewPath);
  117.       strcat(szBuf, findbuf.achName);
  118.                             /* Store all found files to structure pFiles */
  119.       if(!DosAllocMem(&pvAddress,sizeof(PFILELIST),
  120.                      PAG_READ|PAG_WRITE|PAG_COMMIT))
  121.       {                                          /* Store file into list */
  122.          pTail->pNextFile = (PFILELIST)pvAddress;
  123.          pTail = pTail->pNextFile;
  124.          strcpy(pTail->szFileName, szBuf);
  125.          pTail->pNextFile = NULL;
  126.       }
  127.    }
  128.    pNext = *pFile;                    /* Protect the memory of file list */
  129.    if(pNext != NULL)
  130.    {
  131.       DosSetMem(pNext,sizeof(PFILELIST), PAG_GUARD|PAG_READ|PAG_WRITE);
  132.    }
  133.    return 0;                                           /* RETURN_SUCCESS */
  134. }
  135.  
  136. /****************************************************************\
  137.  *                                                              *
  138.  * Name:    ReadFileProc(pFileText)                             *
  139.  *                                                              *
  140.  * Purpose: This routine open and read file which are selected. *
  141.  *                                                              *
  142.  * Usage:   After files read, this module will store data into  *
  143.  *          a buffer.                                           *
  144.  *                                                              *
  145.  * Returns: Dependent upon API return code.                     *
  146.  *                                                              *
  147. \****************************************************************/
  148. APIRET ReadFileProc(PFILEINFO pFileText)
  149. {
  150.    HFILE    FileHandle=0;                               /* Opened file handle */
  151.    ULONG    ulAct=0L;
  152.    APIRET   Ret;
  153.    ULONG    ulRdByte=0L;                                   /* Bytes be read */
  154.  
  155.    Ret = DosOpen((PSZ)pFileText->szFileName,
  156.                  (PHFILE)&FileHandle,
  157.                  &ulAct,
  158.                  0L,                   /* Specify file new size for created */
  159.                  FILE_NORMAL,        /* File attributes for create new file */
  160.                  FILE_OPEN,                        /* Open an existing file */
  161.            OPEN_SHARE_DENYWRITE,/* Open modes: others can open to read only */
  162.                  NULL);                    /* Extended attributes structure */
  163.    if (Ret)
  164.       return (Ret);                                    /* RETURN_ERROR code */
  165.    else
  166.    {
  167.       DosRead(FileHandle, (PVOID)&(pFileText->szBuffer), BUFF_SIZE, &ulRdByte);
  168.       if (ulRdByte == 0L)
  169.          return (APIRET)1;                                  /* RETURN_ERROR */
  170.    }
  171.    if(DosClose(FileHandle))
  172.      return (APIRET)1;                                      /* RETURN_ERROR */
  173.    return (APIRET)0;                                      /* RETURN_SUCCESS */
  174. }
  175.  
  176. /****************************************************************\
  177.  *                                                              *
  178.  * Name:    CalCulProc(pMathEl)                                 *
  179.  *                                                              *
  180.  * Purpose: This routine calculates the result from given       *
  181.  *            operands which are input by user.                 *
  182.  *                                                              *
  183.  * Usage:   After calculated, store the result to Math structure*
  184.  *                                                              *
  185.  * Returns: Dependent upon API return code.                     *
  186.  *                                                              *
  187. \****************************************************************/
  188. APIRET CalCulProc(PMATHELEMENT pMathEl)
  189. {
  190.    double fResultVal=0.0;
  191.  
  192.    switch (*pMathEl->pszOperation) {
  193.    case '+':
  194.       fResultVal = pMathEl->fOperand1 + pMathEl->fOperand2;
  195.       break;
  196.    case '-':
  197.       fResultVal = pMathEl->fOperand1 - pMathEl->fOperand2;
  198.       break;
  199.    case '*':
  200.       fResultVal = pMathEl->fOperand1 * pMathEl->fOperand2;
  201.       break;
  202.    case '/':
  203.       fResultVal = (double)pMathEl->fOperand1 / (double)pMathEl->fOperand2;
  204.       break;
  205.    }                                                           /* endswitch */
  206.    sprintf(pMathEl->szResult, "%f", fResultVal);
  207.    return (APIRET)0;                                      /* RETURN_SUCCESS */
  208. }
  209.