home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mlelib.zip / mlelib / mlefile.cpp < prev    next >
C/C++ Source or Header  |  1993-06-07  |  8KB  |  324 lines

  1. /*************************************************************************
  2. *
  3. * filename        : mlefile.cpp
  4. *
  5. * description    : move text between MLE and filesystem
  6. *
  7. * methods        : [none]
  8. *
  9. * functions        : LoadFile        - load a file to the MLE
  10. *                  SaveFile        - copy the MLE-contents to the filesystem
  11. *                  ValidateFilename    - check if a file already exists
  12. *                  CopyTextToBuffer    - copy MLE-contents to a buffer
  13. *
  14. * APIs            : DosOpen                    DosClose
  15. *                   DosQueryFileInfo            DosAllocMem
  16. *                   DosFreeMem                WinSendMsg
  17. *                    DosRead                    DosWrite
  18. *                  WinMessageBox
  19. *
  20. * Used Classes    : [none]
  21. *
  22. * copyright (C) 1993 Jörg Caumanns (caumanns@cs.tu-berlin.de)
  23. *
  24. *************************************************************************/#define INCL_DOSMEMMGR
  25. #define INCL_WINDIALOGS
  26. #define INCL_WINPOINTERS
  27. #define INCL_WINMLE
  28. #define INCL_DOSFILEMGR
  29. #include <os2.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32.  
  33. extern VOID FatalError(HWND, CHAR*);
  34.  
  35. /*************************************************************************
  36. *
  37. * Name    : LoadFile
  38. *
  39. * Descr.: Load a file into the MLE
  40. *
  41. * APIs    : DosOpen                    DosClose
  42. *         DosQueryFileInfo            DosAllocMem
  43. *         DosFreeMem                WinSendMsg
  44. *          DosRead
  45. *
  46. * Param.: HWND    hwnd        - parent window handle
  47. *          HWND  hwndMLE        - MLE window handle
  48. *          CHAR  *pszFile    - file name
  49. *
  50. * Return: BOOL fSuccess
  51. *
  52. *************************************************************************/
  53. BOOL LoadFile(HWND hwnd, HWND hwndMLE, CHAR *pszFile)    {
  54.     VOID *pvBuf;
  55.     HFILE hf;
  56.     IPT   lOffset = 0;
  57.     ULONG cbRead, cbCopied, ulAction;
  58.  
  59.     /*
  60.     * open requested file
  61.     */
  62.     if(DosOpen(pszFile,
  63.                &hf,
  64.                &ulAction, 0,
  65.                FILE_NORMAL,
  66.                FILE_OPEN | FILE_CREATE,
  67.                OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE,
  68.                NULL))    {
  69.         FatalError(hwnd, "can't open requested file");
  70.         return FALSE;
  71.         }
  72.  
  73.     /*
  74.     * Get the length of the file
  75.     */
  76.     FILESTATUS fileStatus;
  77.     if (DosQueryFileInfo(hf, 1, (PVOID)&fileStatus,    sizeof(FILESTATUS)))    {
  78.         DosClose(hf);
  79.         FatalError(hwnd, "can't read requested file!");
  80.         return FALSE;
  81.         }
  82.  
  83.     if(fileStatus.cbFileAlloc == 0)    {
  84.         return TRUE;
  85.         }
  86.  
  87.     WinSetPointer(HWND_DESKTOP,
  88.                     WinQuerySysPointer(HWND_DESKTOP, SPTR_WAIT, FALSE));
  89.     /*
  90.     * Allocate a buffer for the file
  91.     */
  92.     if(DosAllocMem((PPVOID)&pvBuf, (ULONG)fileStatus.cbFileAlloc,
  93.                                         PAG_READ | PAG_WRITE | PAG_COMMIT))    {
  94.         DosClose(hf);
  95.         FatalError(hwnd, "can't allocate buffer to read requested file!");
  96.         return FALSE;
  97.         }
  98.  
  99.     /*
  100.     * Read in the file
  101.     */
  102.     if(DosRead(hf, (char*)pvBuf, fileStatus.cbFileAlloc, &cbRead))    {
  103.         DosClose(hf);
  104.         FatalError(hwnd, "can't read requested file!");
  105.         return FALSE;
  106.         }
  107.  
  108.     /*
  109.     * Set the file into the MLE
  110.     */
  111.     WinSendMsg(hwndMLE, MLM_SETIMPORTEXPORT, MPFROMP((PBYTE)pvBuf),
  112.                                           MPFROMSHORT(fileStatus.cbFileAlloc));
  113.  
  114.     lOffset = 0;
  115.     WinSendMsg(hwndMLE, MLM_IMPORT, MPFROMP(&lOffset),
  116.                                         MPFROMSHORT(fileStatus.cbFileAlloc));
  117.  
  118.     /*
  119.     * Reset the changed flag
  120.     */
  121.     WinSendMsg(hwndMLE, MLM_SETCHANGED, MPFROMSHORT((BOOL)FALSE), NULL);
  122.  
  123.     DosFreeMem(pvBuf);
  124.     DosClose(hf);
  125.  
  126.     WinSetPointer(HWND_DESKTOP,
  127.                     WinQuerySysPointer(HWND_DESKTOP, SPTR_ARROW, FALSE));
  128.  
  129.     return TRUE;
  130.     }
  131.  
  132.  
  133. /*************************************************************************
  134. *
  135. * Name    : SaveFile
  136. *
  137. * Descr.: Save the contents of a MLE to a file
  138. *
  139. * APIs    : DosOpen                    DosClose
  140. *         DosWrite                    DosAllocMem
  141. *         DosFreeMem                WinSendMsg
  142. *
  143. * Param.: HWND    hwnd        - parent window handle
  144. *          HWND  hwndMLE        - MLE window handle
  145. *          CHAR  *pszFile    - file name
  146. *
  147. * Return: BOOL fSuccess
  148. *
  149. *************************************************************************/
  150. BOOL SaveFile(HWND hwnd, HWND hwndMLE, CHAR *pszFile)    {
  151.     VOID *pvBuf;
  152.     HFILE hf;
  153.     IPT   lOffset = 0;
  154.     ULONG cbWritten, cbCopied, ulAction;
  155.     ULONG cbExport, ulFileLen;
  156.  
  157.     /*
  158.     * open the file for writing
  159.     */
  160.     if(DosOpen(pszFile,
  161.                &hf,
  162.                &ulAction, 0,
  163.                FILE_NORMAL,
  164.                FILE_OPEN | FILE_CREATE,
  165.                OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYNONE,
  166.                NULL))    {
  167.         FatalError(hwnd, "can't open requested file");
  168.         return FALSE;
  169.         }
  170.  
  171.     /*
  172.     * Get the length of the file
  173.     */
  174.     ulFileLen = (ULONG)WinSendMsg(hwndMLE, MLM_QUERYTEXTLENGTH, MPVOID, MPVOID);
  175.     if(!ulFileLen)    {
  176.         DosClose(hf);
  177.         return TRUE;
  178.         }
  179.  
  180.     /*
  181.     * Allocate a buffer for the file
  182.     */
  183.     if(DosAllocMem((PPVOID)&pvBuf, ulFileLen,
  184.                                         PAG_READ | PAG_WRITE | PAG_COMMIT))    {
  185.         DosClose(hf);
  186.         FatalError(hwnd, "can't allocate buffer to write requested file!");
  187.         return FALSE;
  188.         }
  189.  
  190.     /*
  191.     * Get the file from the MLE
  192.     */
  193.     cbExport = ulFileLen;
  194.     WinSendMsg(hwndMLE, MLM_SETIMPORTEXPORT, MPFROMP((PBYTE)pvBuf),
  195.                                              MPFROMLONG(cbExport));
  196.  
  197.     /*
  198.     * Export MLE starting at offset 0
  199.     */
  200.     lOffset = 0;
  201.     WinSendMsg(hwndMLE, MLM_EXPORT, MPFROMP(&lOffset), MPFROMLONG(&cbExport));
  202.  
  203.     /*
  204.     * Write the file
  205.     */
  206.     if(DosWrite(hf, pvBuf, ulFileLen, &cbWritten))    {
  207.         DosClose(hf);
  208.         FatalError(hwnd, "can't write requested file!");
  209.         return FALSE;
  210.         }
  211.  
  212.     /*
  213.     * Reset the changed flag
  214.     */
  215.     WinSendMsg(hwndMLE, MLM_SETCHANGED, MPFROMSHORT((BOOL)FALSE), NULL);
  216.  
  217.     DosFreeMem(pvBuf);    DosClose(hf);
  218.     return TRUE;
  219.     }
  220.  
  221.  
  222. /*************************************************************************
  223. *
  224. * Name    : ValidateFilename
  225. *
  226. * Descr.: Check if a file exists
  227. *
  228. * APIs    : DosOpen                    DosClose
  229. *          WinMessageBox
  230. *
  231. * Param.: HWND    hwnd        - parent window handle
  232. *          CHAR  *pszFile    - file name
  233. *
  234. * Return: BOOL fValid
  235. *
  236. *************************************************************************/
  237. BOOL ValidateFilename(HWND hwnd, CHAR *pszFile)    {
  238.     HFILE hf;
  239.     ULONG ulAction;
  240.     SHORT sResponce;
  241.  
  242.     if(DosOpen(pszFile,
  243.                &hf,
  244.                &ulAction, 0,
  245.                FILE_NORMAL,
  246.                FILE_OPEN | FILE_CREATE,
  247.                OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYNONE,
  248.                NULL))    {
  249.         FatalError(hwnd, "can't open requested file");
  250.         return FALSE;
  251.         }
  252.  
  253.     DosClose(hf);
  254.  
  255.     /*
  256.     * If file exists, ask if we want to overwrite it
  257.     */
  258.     CHAR szQuery[256];
  259.     if(ulAction == FILE_EXISTED)    {
  260.         sprintf(szQuery, "%s already exists! Do you want to overwrite it?", pszFile);
  261.         sResponce = WinMessageBox(HWND_DESKTOP, hwnd, szQuery, "File Exists",
  262.                                                     0, MB_QUERY | MB_YESNO);
  263.  
  264.        if(sResponce == MBID_NO)
  265.           return FALSE;
  266.         }
  267.  
  268.     WinSetWindowText(hwnd, pszFile);
  269.     return TRUE;
  270.     }
  271.  
  272.  
  273. /*************************************************************************
  274. *
  275. * Name    : CopyTextToBuffer
  276. *
  277. * Descr.: Copy MLE-contents to a buffer
  278. *
  279. * APIs    : WinSendMsg            DosAllocMem
  280. *
  281. * Param.: HWND    hwnd        - parent window handle
  282. *          HWND  hwnd        - MLE window handle
  283. *          CHAR  **ppszBuffer- buffer's address
  284. *
  285. * Return: BOOL fValid
  286. *
  287. *************************************************************************/
  288. BOOL CopyTextToBuffer(HWND hwnd, HWND hwndMLE, CHAR **ppszBuffer)    {
  289.  
  290.     /*
  291.     * Get the length of the text
  292.     */
  293.     ULONG ulFileLen = (ULONG)WinSendMsg(hwndMLE,
  294.                                         MLM_QUERYTEXTLENGTH,
  295.                                         MPVOID, MPVOID);
  296.     if(!ulFileLen)    {
  297.         *ppszBuffer = NULL;
  298.         return TRUE;
  299.         }
  300.  
  301.     /*
  302.     * Allocate a buffer for the file
  303.     */
  304.     if(DosAllocMem((PPVOID)ppszBuffer, ulFileLen,
  305.                    PAG_READ | PAG_WRITE | PAG_COMMIT))    {
  306.         FatalError(hwnd, "can't allocate buffer to write requested file!");
  307.         return FALSE;
  308.         }
  309.  
  310.     /*
  311.     * Set export buffer
  312.     */
  313.     ULONG cbExport = ulFileLen;
  314.     WinSendMsg(hwndMLE, MLM_SETIMPORTEXPORT, MPFROMP((PBYTE)*ppszBuffer),
  315.                                              MPFROMLONG(cbExport));
  316.  
  317.     /*
  318.     * Export MLE starting at offset 0
  319.     */
  320.     IPT lOffset = 0;
  321.     WinSendMsg(hwndMLE, MLM_EXPORT, MPFROMP(&lOffset), MPFROMLONG(&cbExport));
  322.     return TRUE;
  323.     }
  324.