home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / smart21b.zip / SAMPLES / SLINK / LINKFILE.C < prev    next >
C/C++ Source or Header  |  1993-03-01  |  12KB  |  386 lines

  1. /*************************************************************
  2. linkfile.c
  3.  
  4. **************************************************************/
  5. #include "link.h"
  6.  
  7. /************ External GLOBALS *******************************/
  8.  
  9. extern CHAR szFileName[CCHMAXPATH];
  10. extern CHAR szXRName[MAXXRNAME+1];
  11. extern USHORT usRetXRType;
  12. extern BOOL FILE_ISOPEN;
  13. extern BOOL FILE_CHANGED;
  14. extern BOOL COMMAND_LINE_FILE;
  15. extern HHXRP hhp;
  16. extern CHAR *pAlloc,*szEditBuf,*szAscii,*szScratch;
  17. extern HOLDFXR *pHoldFXR;
  18. extern DELETELIST *pDelList;
  19. extern XRDATA ConvTable[XRTABLESIZE];
  20.  
  21. #ifdef OPEN
  22. /*************************************************************/
  23. /*
  24.  * Function name: OpenFile()
  25.  *
  26.  * Calls: WriteXRs(), Free_FXRList()
  27.  */
  28. BOOL OpenFile(HWND hwnd,USHORT usMode)
  29. {
  30.    CHAR szOldFile[CCHMAXPATH];
  31.    USHORT usRet;
  32.  
  33.    mystrcpy(szOldFile,szFileName);  /* Save name of the currently open file */
  34.  
  35.    if(usMode != ARGFILE)          /* It isn't the command line file */
  36.    {
  37.       if(!WinDlgBox(HWND_DESKTOP, /* Get the file name from the user */
  38.                     hwnd,
  39.                     OpenFileProc,
  40.                     NULL,
  41.                     IDD_OPENBOX,
  42.                     NULL))
  43.       {
  44.     if (WinDlgBox(HWND_DESKTOP,
  45.              hwndFrame,    /* handle of the owner         */
  46.              OpenDlg,      /* dialog procedure address    */
  47.              NULLHANDLE,   /* location of dialog resource */
  48.              IDD_OPEN,     /* resource identifier         */
  49.              NULL)) {      /* application-specific data   */
  50.          mystrcpy(szFileName,szOldFile); /* They canceled, restore old file */
  51.          return(FILE_ISOPEN);
  52.       }
  53.    if(FILE_CHANGED) /* Give them a chance to save modifications */
  54.    {
  55.       usRet=WinMessageBox(HWND_DESKTOP,hwnd,
  56.                           "The current file has been changed.  Do you \
  57. wish to save the changes before proceeding?",
  58.                           "Warning",NULL,MB_YESNOCANCEL | MB_ICONQUESTION);
  59.       switch(usRet)
  60.       {
  61.          case MBID_YES:
  62.             WriteXRs(hwnd);
  63.             break;
  64.          case MBID_CANCEL:
  65.             return FILE_ISOPEN;
  66.       }
  67.    }
  68.  
  69.    if(FILE_ISOPEN) /* Free up everything associated with the current file */
  70.    {
  71.       Free_FXRList(pHoldFXR,pDelList);
  72.       FILE_ISOPEN = FALSE;
  73.    }
  74.  
  75.    if(QueryXRs(hwnd,szFileName)) /* We were successful */
  76.    {
  77.       HOLDFXR *pFXR=pHoldFXR;
  78.  
  79.       FILE_ISOPEN = TRUE;
  80.       FILE_CHANGED = FALSE;
  81.  
  82.       WinSendDlgItemMsg(hwnd, IDD_LBOX, LM_DELETXRLL,0L,0L); /* Fill L-box */
  83.  
  84.       while(pFXR)
  85.       {
  86.          WinSendDlgItemMsg(hwnd, IDD_LBOX, LM_INSERTITEM,
  87.                            MPFROM2SHORT(LIT_END,0),
  88.                            MPFROMP(pFXR->szName));
  89.          pFXR = pFXR->next;
  90.       }
  91.    }
  92.    else /* We couldn't query the XRs */
  93.    {
  94.       *szFileName = '\000';
  95.       WinSetDlgItemText(hwnd,IDD_FNAME,szFileName);
  96.       return(FILE_ISOPEN = FALSE);
  97.    }
  98.    WinSetDlgItemText(hwnd,IDD_FNAME,szFileName);
  99.    pDelList = NULL;
  100.    return(TRUE);
  101. }
  102. #endif
  103.  
  104. /*
  105.  * Function name: OpenFileProc()
  106.  *
  107.  * Calls: FillDirListBox(), FillFileListBox(), ParseFileName()
  108.  */
  109.  
  110. MRESULT EXPENTRY OpenFileProc(HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  111. {
  112.    static CHAR szCurrentPath[CCHMAXPATH],szBuffer[CCHMAXPATH];
  113.    CHAR        szParsedPath[CCHMAXPATH];
  114.    SHORT       sSelect;
  115.  
  116.    switch(msg)
  117.    {
  118.       case WM_INITDLG:
  119.          FillDirListBox(hwnd,szCurrentPath);
  120.          FillFileListBox(hwnd);
  121.          WinSendDlgItemMsg(hwnd, IDD_FILEEDIT,EM_SETTEXTLIMIT,
  122.                                  MPFROM2SHORT(260,0),NULL);
  123.              WinSendDlgItemMsg(hwndFrame,  (ULONG) FID_MENU,
  124.                       (ULONG) MM_SETITEMATTR,
  125.                      MPFROM2SHORT(IDM_SHOW, TRUE),
  126.                      MPFROM2SHORT(MIA_CHECKED,MIA_CHECKED));
  127.          return 0L;
  128.  
  129.       case WM_CONTROL:
  130.          if(SHORT1FROMMP(mp1) == IDD_DIRLIST ||  /* An lbox item is selected */
  131.             SHORT1FROMMP(mp1) == IDD_FILELIST)
  132.          {
  133.             sSelect = (USHORT) WinSendDlgItemMsg(hwnd, /* Get item->szBuffer */
  134.                                SHORT1FROMMP(mp1),
  135.                                LM_QUERYSELECTION, 0L, 0L);
  136.  
  137.             WinSendDlgItemMsg(hwnd, SHORT1FROMMP(mp1),
  138.                               LM_QUERYITEMTEXT,
  139.                               MPFROM2SHORT(sSelect, sizeof szBuffer),
  140.                               MPFROMP(szBuffer));
  141.          }
  142.  
  143.          switch(SHORT1FROMMP(mp1))
  144.          {
  145.             case IDD_DIRLIST:              /* Item was in the directory lbox */
  146.                switch(SHORT2FROMMP(mp1))
  147.                {
  148.                   case LN_ENTER:           /* Go to the select drive/dir */
  149.                      if(*szBuffer == ' ')
  150.                         DosSelectDisk(*(szBuffer+1) - '@');
  151.                      else
  152.                         DosChDir(szBuffer, 0L);
  153.  
  154.                      FillDirListBox(hwnd, szCurrentPath);
  155.                      FillFileListBox(hwnd);
  156.  
  157.                      WinSetDlgItemText(hwnd, IDD_FILEEDIT, "");
  158.                      return 0L;
  159.                }
  160.                break;
  161.  
  162.             case IDD_FILELIST:             /* Item was in the file lbox */
  163.                switch(SHORT2FROMMP(mp1))
  164.                {
  165.                   case LN_SELECT:          /* Copy name to entry field  */
  166.                      WinSetDlgItemText(hwnd, IDD_FILEEDIT, szBuffer);
  167.                      return 0L;
  168.  
  169.                   case LN_ENTER:           /* Try to query the file */
  170.                      if(ParseFileName(szFileName, szBuffer) != FILE_VALID)
  171.                         return 0; /* Some error, don't finish */
  172.                      WinDismissDlg(hwnd, TRUE);
  173.                      return 0L;
  174.                }
  175.                break;
  176.          }
  177.          break;
  178.  
  179.       case WM_COMMAND:
  180.          switch(COMMANDMSG(&msg)->cmd)
  181.          {
  182.             case DID_OK:            /* Try to query file in the entry field */
  183.                WinQueryDlgItemText(hwnd, IDD_FILEEDIT,
  184.                                    sizeof szBuffer, szBuffer);
  185.  
  186.                switch(ParseFileName(szParsedPath, szBuffer))
  187.                {
  188.                   case FILE_INVALID:    /* Can't open the file */
  189.                      WinAlarm(HWND_DESKTOP, WA_ERROR);
  190.                      FillDirListBox(hwnd, szCurrentPath);
  191.                      FillFileListBox(hwnd);
  192.                      return 0L;
  193.  
  194.                   case FILE_PATH:    /* It was an incomplete path name */
  195.                      mystrcpy(szCurrentPath,szBuffer);
  196.                      FillDirListBox(hwnd, szCurrentPath);
  197.                      FillFileListBox(hwnd);
  198.                      WinSetDlgItemText(hwnd, IDD_FILEEDIT, "");
  199.                      return 0L;
  200.  
  201.                   case FILE_VALID:    /* It was valid */
  202.                      mystrcpy(szFileName, szParsedPath);
  203.                      WinDismissDlg(hwnd, TRUE);
  204.                      return 0L;
  205.                }
  206.                break;
  207.  
  208.             case DID_CANCEL:
  209.                WinDismissDlg(hwnd, FALSE);
  210.                return 0L;
  211.          }
  212.          break;
  213.    }
  214.    return WinDefDlgProc(hwnd, msg, mp1, mp2);
  215. }
  216.  
  217. #ifdef DOS
  218. /*
  219.  * Function name: FillDirListBox()
  220.  *
  221.  * Calls:
  222.  */
  223.  
  224. VOID FillDirListBox(HWND hwnd, CHAR *pcCurrentPath)
  225. {
  226.    static CHAR szDrive [] = "  :";
  227.    FILEFINDBUF findbuf;
  228.    HDIR        hDir = 1;
  229.    SHORT       sDrive;
  230.    USHORT      usDriveNum, usCurPathLen, usSearchCount = 1;
  231.    ULONG       ulDriveMap;
  232.  
  233.    DosQCurDisk(&usDriveNum, &ulDriveMap);
  234.    *pcCurrentPath     = (CHAR) usDriveNum + '@';
  235.    *(pcCurrentPath+1) = ':';
  236.    *(pcCurrentPath+2) = '\\';
  237.    usCurPathLen = CCHMAXPATH;
  238.    DosQCurDir(0, pcCurrentPath + 3, &usCurPathLen);
  239.  
  240.    WinSetDlgItemText(hwnd, IDD_PATH, pcCurrentPath);
  241.    WinSendDlgItemMsg(hwnd, IDD_DIRLIST, LM_DELETXRLL, NULL, NULL);
  242.  
  243.    for(sDrive = ('A'-'A'); sDrive <= ('Z'-'A'); sDrive++)
  244.    {
  245.       if(ulDriveMap & (1L << sDrive))
  246.       {
  247.          *(szDrive+1) = (CHAR) sDrive + 'A';
  248.  
  249.          WinSendDlgItemMsg(hwnd, IDD_DIRLIST, LM_INSERTITEM,
  250.                            MPFROM2SHORT(LIT_END, 0),
  251.                            MPFROMP(szDrive));
  252.       }
  253.    }
  254.    DosFindFirst("*", &hDir, FILE_DIRECTORY | FILE_ALL, &findbuf,
  255.                 sizeof findbuf, &usSearchCount, 0L);
  256.    while(usSearchCount)
  257.    {
  258.       if((findbuf.attrFile & FILE_DIRECTORY) &&
  259.          (findbuf.achName[0] != '.' || findbuf.achName[1]))
  260.  
  261.          WinSendDlgItemMsg(hwnd, IDD_DIRLIST, LM_INSERTITEM,
  262.                            MPFROM2SHORT(LIT_SORTASCENDING, 0),
  263.                            MPFROMP(findbuf.achName));
  264.  
  265.       if(DosFindNext(hDir, &findbuf, sizeof findbuf, &usSearchCount))
  266.          break;
  267.    }
  268. }
  269.  
  270.  
  271. /* This routine is called by OpenFileProc to fill the file list box */
  272.  
  273. /*
  274.  * Function name: FillFileListBox()
  275.  *
  276.  */
  277.  
  278. VOID FillFileListBox(HWND hwnd)
  279. {
  280.    FILEFINDBUF findbuf;
  281.    HDIR        hDir = 1;
  282.    USHORT      usSearchCount = 1; /* Read 1 entry at a time */
  283.  
  284.    WinSendDlgItemMsg(hwnd, IDD_FILELIST, LM_DELETXRLL, NULL, NULL);
  285.  
  286.    DosFindFirst("*", &hDir, FILE_ALL, &findbuf, sizeof findbuf,
  287.                        &usSearchCount, 0L);
  288.    while(usSearchCount)
  289.    {
  290.       WinSendDlgItemMsg(hwnd, IDD_FILELIST, LM_INSERTITEM,
  291.                         MPFROM2SHORT(LIT_SORTASCENDING, 0),
  292.                         MPFROMP(findbuf.achName));
  293.  
  294.       if(DosFindNext(hDir, &findbuf, sizeof findbuf, &usSearchCount))
  295.          break;
  296.    }
  297. }
  298. #endif
  299.  
  300. /*
  301.  * Function name: ParseFileName()
  302.  *
  303.  */
  304.  
  305. SHORT ParseFileName(CHAR *pcOut, CHAR *pcIn)
  306. {
  307.    CHAR   *pcLastSlash, *pcFileOnly ;
  308.    ULONG  ulDriveMap ;
  309.    USHORT usDriveNum, usDirLen = CCHMAXPATH;
  310.  
  311.    strupr(pcIn);  /* Does NOT handle extended chars, should use DosCaseMap */
  312.  
  313.    if(*pcIn == '\000')  /* If string is empty, return FILE_PATH */
  314.  
  315.       return FILE_PATH;
  316.  
  317.    /* Get drive from input string or use current drive */
  318.  
  319.    if(*(pcIn+1) == ':') /* Yup, they specified a drive */
  320.    {
  321.       if(DosSelectDisk(*pcIn - '@')) /* Change to selected drive */
  322.          return FILE_INVALID;
  323.       pcIn += 2;
  324.    }
  325.    DosQCurDisk(&usDriveNum, &ulDriveMap); /* Get current drive */
  326.  
  327.    *pcOut++ = (CHAR) usDriveNum + '@'; /* Build drive letter */
  328.    *pcOut++ = ':';
  329.    *pcOut++ = '\\';
  330.  
  331.    if(*pcIn == '\000') /* If rest of the string is empty, return FILE_PATH */
  332.       return FILE_PATH;
  333.  
  334.    /* Search for the last backslash.  If none, it could be a directory. */
  335.  
  336.    if(!(pcLastSlash = strrchr(pcIn, '\\'))) /* No slashes? */
  337.    {
  338.       if(!DosChDir(pcIn, 0L))
  339.          return FILE_PATH;            /* It was a directory */
  340.  
  341.       DosQCurDir(0, pcOut, &usDirLen); /* Get current dir & attach input fn */
  342.  
  343.       if(*(pcOut+strlen(pcOut)-1) != '\\')
  344.          strcat(pcOut++, "\\");
  345.  
  346.       strcat(pcOut, pcIn);
  347.       return FILE_VALID;
  348.    }
  349.  
  350.    /* If the only backslash is at the beginning, change to root */
  351.  
  352.    if(pcIn == pcLastSlash)
  353.    {
  354.       DosChDir("\\", 0L);
  355.  
  356.       if(*(pcIn+1) == '\000')
  357.          return FILE_PATH;
  358.  
  359.       strcpy(pcOut, pcIn+1);
  360.       return FILE_VALID;
  361.    }
  362.  
  363.    /* Attempt to change directory -- Get current dir if OK */
  364.  
  365.    *pcLastSlash = NULL;
  366.  
  367.    if(DosChDir(pcIn, 0L))
  368.       return FILE_INVALID;
  369.  
  370.    DosQCurDir(0, pcOut, &usDirLen);
  371.  
  372.    /* Append input filename if any */
  373.  
  374.    pcFileOnly = pcLastSlash+1;
  375.  
  376.    if(*pcFileOnly == '\000')
  377.       return FILE_PATH;
  378.  
  379.    if(*(pcOut+strlen(pcOut)-1) != '\\')
  380.       strcat(pcOut++, "\\");
  381.  
  382.    strcat(pcOut, pcFileOnly);
  383.    return FILE_VALID;
  384. }
  385. /*****************************************/
  386. /*****************************************/