Author Topic: Getting wid from filename  (Read 8463 times)

byates

  • Senior Community Member
  • Posts: 106
  • Hero Points: 8
Getting wid from filename
« on: August 10, 2008, 04:47:18 AM »
Given a filename, does anyone know how to check to see if the file is open for editing, and if so, what the wid is?  Also, I need to do this from a DLL.  Alternatively, given a buffer id, how does one determine an associated window id?

Thanks

hs2

  • Senior Community Member
  • Posts: 2761
  • Hero Points: 292
Re: Getting wid from filename
« Reply #1 on: August 10, 2008, 10:26:58 AM »
Hi byates, have a look here for an example, how to do something with all open buffers. Of course instead of doing sth. with a particular buffer you can retrieve the p_window_id, p_buf_name, p_modify, p_ReadOnly, etc.
Alternatively buf_match() could be useful in your case.
It seems that you can do the same by using vsNextBuffer, vsBufGetName/vsPropGet* from a DLL, but I can't give any better hint related to the DLL interface.
HS2
« Last Edit: August 10, 2008, 10:34:26 AM by hs2 »

byates

  • Senior Community Member
  • Posts: 106
  • Hero Points: 8
Re: Getting wid from filename
« Reply #2 on: August 10, 2008, 07:41:37 PM »
Here is what I ended up doing, just for future reference:

NOTE: This code is called from a DLL

Code: [Select]
TempReturnCode = vsOpenTempView(fileNameStr.c_str(),&TempWinId,&OrgWinId,"+b",NULL);
if ( TempReturnCode == 0 )
{
// SlickEdit has the file open, so read current buffer from SE.
BufferSize = vsPropGetI(TempWinId,VSP_RBUFSIZE);
pBufferPtr = (tdBytePtr)vsAlloc(BufferSize+1);
BytesRead=vsGetRText(TempWinId,BufferSize,0,(char*)pBufferPtr);
vsDeleteTempView(TempWinId,0);
}
else
{
// SlickEdit does not have the file open, so try to load if from disk.
                ...
}


hs2

  • Senior Community Member
  • Posts: 2761
  • Hero Points: 292
Re: Getting wid from filename
« Reply #3 on: August 10, 2008, 11:22:50 PM »
Ahh - that's much better. And and a good DLL code example for me :) Seems to be easier than I thought...
Could you post/mail a SE DLL skeleton you've developed ? Although there might be sth. in the docs I prefer real life examples ;)
Thanks, HS2

byates

  • Senior Community Member
  • Posts: 106
  • Hero Points: 8
Re: Getting wid from filename
« Reply #4 on: August 12, 2008, 09:07:43 PM »
HS2,

Give me a little while to clean it up, and I'll send you a copy...

Brent

HansNet

  • Community Member
  • Posts: 8
  • Hero Points: 0
Re: Getting wid from filename
« Reply #5 on: January 06, 2009, 08:03:10 AM »
Could you send me a copy too please ?
I hope it's a VB.NET dll ? ... because that's what I'm looking for.

chrisant

  • Senior Community Member
  • Posts: 1410
  • Hero Points: 131
Re: Getting wid from filename
« Reply #6 on: January 06, 2009, 08:48:08 PM »
Here is a sample SE DLL with full source code.  It is unmanaged C++, though.

byates

  • Senior Community Member
  • Posts: 106
  • Hero Points: 8
Re: Getting wid from filename
« Reply #7 on: January 06, 2009, 10:55:06 PM »
I found that the previously posted method has some problems side-effects due to the switching from active buffer to temp buffer and back.  This method works much better.  The speed seems OK as well.  I tested it with dozens of open buffers and have no issues.

Code: [Select]
// Function prototypes
string                          SE_GetCurrentBufferName(int wid = 0);
tdS32Bit                        SE_FindBufferByName(const string& fileNameStr);

// Function implementations

//   +-------------------------+
// --|  SE_FindBufferByName()  |-----------------------------------------------
//   +-------------------------+

/**
 * Searches through all the MDI windows to find a match for a given filename.
 * 
 * @param fileNameStr Name of the buffer to find
 *
 * @return Matching window ID or -1 if no match found.
 */
int
SE_FindBufferByName(const string& fileNameStr)
    {
    int wid;
    string BufferNameStr;
   
    // Traverse all SlickEdit objects.
    for ( wid=1;wid<=vsLastWindowID();wid++ )
        {
        // Only check for valid window objects.
        if ( vsIsWindowValid(wid) )
            {
            // IF this is an editor control object AND
            //      this an MDI child editor control AND
            //      it is not the HIDDEN WINDOW
            if ( vsPropGetI(wid,VSP_HASBUFFER) &&
                 vsPropGetI(wid,VSP_MDICHILD) &&
                 (wid != VSWID_HIDDEN) )
                {
                BufferNameStr = SE_GetCurrentBufferName(wid);
                if ( fileNameStr == BufferNameStr )
                    {
                    return( wid );
                    }
                }
            }
        }
    return( -1 );
    }

//   +-----------------------------+
// --|  SE_GetCurrentBufferName()  |-------------------------------------------
//   +-----------------------------+

/**
 *
 * @return Name (full path) of the current buffer.
 */
string
SE_GetCurrentBufferName(int wid)
    {
    string BufferNameStr;
    static char buffername[MAXFILENAME];
    vsPropGetZ(wid,VSP_BUFNAME,buffername,MAXFILENAME);
    BufferNameStr.assign(buffername);
    return( BufferNameStr );
    }


« Last Edit: January 06, 2009, 11:06:18 PM by byates »

byates

  • Senior Community Member
  • Posts: 106
  • Hero Points: 8
Re: Getting wid from filename
« Reply #8 on: January 06, 2009, 10:59:51 PM »
Just for fun 8), here is the function I use to read a file from either the open SlickEdit buffer (if there is one) or from disk.  It uses the two functions from above.

Code: [Select]
// Function prototype
int                             SE_ReadFile(const string& fileNameStr, tdBytePtr& pBufferPtr);

// Function implementation

//   +-----------------+
// --|  SE_ReadFile()  |-------------------------------------------------------
//   +-----------------+

/**
 * Reads the contents of the file specified into the memory buffer specified.  If the file is
 * currently loaded in SlickEdit then the slickedit buffer is used otherwise the file
 * is loaded from disk.
 * <BR><B>
 * NOTE: Caller must free buffer using vsFree.
 * </B>
 * @param fileNameStr          Name of the file to load or SE buffer.
 * @param pBufferPtr           Buffer to filled with data (must be freed using vsFree).
 *
 * @return Number of bytes stored in the buffer.
 */
int
SE_ReadFile(const string& fileNameStr,
            tdBytePtr& pBufferPtr)
    {
    tdS32Bit BytesRead;
    int BufferSize;
    int WinId;

    // Check to see if the current buffer is the one we want...
    if ( SE_GetCurrentBufferName() == fileNameStr )
        {
        // SlickEdit has the file open, so read current buffer from SE.
        BufferSize = vsPropGetI(0,VSP_RBUFSIZE);
        pBufferPtr = (tdBytePtr)vsAlloc(BufferSize+1);
        BytesRead=vsGetRText(0,BufferSize,0,(char*)pBufferPtr);
        return( BytesRead );
        }
   
    // Not current buffer, so check to see if SE has a buffer with the file contents.
    WinId = SE_FindBufferByName(fileNameStr);
    if ( WinId >= 0 )
        {
        // SlickEdit has the file open, so read current buffer from SE.
        BufferSize = vsPropGetI(WinId,VSP_RBUFSIZE);
        pBufferPtr = (tdBytePtr)vsAlloc(BufferSize+1);
        BytesRead=vsGetRText(WinId,BufferSize,0,(char*)pBufferPtr);
        return( BytesRead );
        }
   
    // SlickEdit does not have the file open, so try to load if from disk.
    if ( !fs::exists(fileNameStr) )
        {
        pBufferPtr = NULL;
        return( 0 );
        }

    // Allocate a buffer big enough for the entire file.
    // I use the boost library for file system stuff: namespace fs = boost::filesystem;
    BufferSize = (int)fs::file_size(fileNameStr);
    pBufferPtr = (tdBytePtr)vsAlloc(BufferSize+1);
    if ( pBufferPtr == NULL )
        {
        return( 0 );
        }
    // Read the file into the buffer.
    int fh=vsFileOpen(fileNameStr.c_str(),0);
    if ( fh<0 )
        {
        // Unable to read from the file.
        vsFree(pBufferPtr);
        pBufferPtr = NULL;
        return( 0 );
        }
    BytesRead = vsFileRead(fh,pBufferPtr,BufferSize);
    vsFileClose(fh);
    return( BytesRead );
    }

« Last Edit: January 06, 2009, 11:08:56 PM by byates »