Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

felisandria

Visual Source Safe automation in C++

This topic is 5685 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Anyone tried this? I can''t even get the database to instantiate properly, and the microsoft docs really rather suck. I''ve been searching the web but can''t find any decent examples in C++ (plenty in VB, apparently). -fel

Share this post


Link to post
Share on other sites
Advertisement
I used it in VB at work, but never in C++. Shouldn''t the two be almost identical, apart from being written in different languages?

Share this post


Link to post
Share on other sites
I wrote a little wrapper class to encapsulate my VSS needs in a tool I wrote. Maybe it''ll help you out?

XSourceSafe.h

  
// XSourceSafe.h: interface for the XSourceSafe class.

//

//////////////////////////////////////////////////////////////////////


#if !defined(AFX_XSOURCESAFE_H__5A5402FF_C0C3_45E9_9D4C_57F4B6BD53A5__INCLUDED_)
#define AFX_XSOURCESAFE_H__5A5402FF_C0C3_45E9_9D4C_57F4B6BD53A5__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


#define EF_ONLYCHECKEDOUT 0x01
#define EF_ONLYCHECKEDIN 0x02

class XSourceSafe
{
public:
XSourceSafe( const string& strDBName );
virtual ~XSourceSafe();
public:
bool Get( const string& strFileName );
bool CheckOut( const string& strFileName, bool bReplace =true );
bool CheckIn( const string& strFileName );
bool SetWorkingProject( const string& strProjName );
bool GetWorkingProject( string& strProjName ) const;
bool EnumerateFiles( stringList& slFiles, int iRecurse =0, int iFlags =0 );
protected:
class XSourceSafeImp;
XSourceSafeImp* pImp;
};

#endif // !defined(AFX_XSOURCESAFE_H__5A5402FF_C0C3_45E9_9D4C_57F4B6BD53A5__INCLUDED_)



XSourceSafe.cpp

  
// XSourceSafe.cpp: implementation of the XSourceSafe class.

//

//////////////////////////////////////////////////////////////////////


#include "stdafx.h"
#include "XSourceSafe.h"

#include "ssapi.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif


//////////////////////////////////////////////////////////////////////

// Implementation

//////////////////////////////////////////////////////////////////////

class XSourceSafe::XSourceSafeImp
{
friend class XSourceSafe;
protected:
XSourceSafeImp( const string& strDBName );
~XSourceSafeImp();
protected:
bool Get( const string& strFileName );
bool CheckOut( const string& strFileName, bool bReplace );
bool CheckIn( const string& strFileName );
bool SetWorkingProject( const string& strProjName );
bool GetWorkingProject( string& strProjName ) const;
bool EnumerateFiles( stringList& slFiles, int iRecurse, int iFlags );
protected:
bool m_bOpen;
IVSSDatabase m_VSSDatabase;
string m_strCurProj;
};


//////////////////////////////////////////////////////////////////////

// Construction/Destruction

//////////////////////////////////////////////////////////////////////

XSourceSafe::XSourceSafe( const string& strDBName )
{
pImp = new XSourceSafe::XSourceSafeImp( strDBName );
}

XSourceSafe::~XSourceSafe()
{
delete pImp;
}

bool XSourceSafe::Get( const string& strFileName )
{
return pImp->Get( strFileName );
}

bool XSourceSafe::CheckOut( const string& strFileName, bool bReplace /*=true*/ )
{
return pImp->CheckOut( strFileName, bReplace );
}

bool XSourceSafe::CheckIn( const string& strFileName )
{
return pImp->CheckIn( strFileName );
}

bool XSourceSafe::SetWorkingProject( const string& strProjName )
{
return pImp->SetWorkingProject( strProjName );
}

bool XSourceSafe::GetWorkingProject( string& strProjName ) const
{
return pImp->GetWorkingProject( strProjName );
}

bool XSourceSafe::EnumerateFiles( stringList& slFiles, int iRecurse /*=0*/, int iFlags /*=0*/
)
{
return pImp->EnumerateFiles( slFiles, iRecurse, iFlags );
}


//////////////////////////////////////////////////////////////////////

// Construction/Destruction

//////////////////////////////////////////////////////////////////////

XSourceSafe::XSourceSafeImp::XSourceSafeImp( const string& strDBName )
{
m_bOpen = false;
if( m_VSSDatabase.CreateDispatch("SourceSafe") ) {
m_VSSDatabase.Open( strDBName.c_str(), "", "");
m_bOpen = true;
}
}

XSourceSafe::XSourceSafeImp::~XSourceSafeImp()
{
m_VSSDatabase.ReleaseDispatch();
m_bOpen = false;
}

bool XSourceSafe::XSourceSafeImp::SetWorkingProject( const string& strProjName )
{
if( !m_bOpen ) {
return false;
}

try {
IVSSItem pItem = m_VSSDatabase.GetVSSItem( strProjName.c_str(), false );

if( pItem.GetType() == VSSITEM_PROJECT ) {
m_strCurProj = strProjName;
return true;
}
}
catch( ... ) {
}

return false;
}

bool XSourceSafe::XSourceSafeImp::GetWorkingProject( string& strProjName ) const
{
if( !m_bOpen ) {
return false;
}

strProjName = m_strCurProj;

return true;
}

static void getFileList( const string& base, IVSSItem& vssProj, stringList& slFiles, int iRecurse, int iFlags )
{
IVSSItems pItems = vssProj.GetItems( FALSE );

long i, count = pItems.GetCount();
for( i = 0; i < count; i++) {
IVSSItem pItem = pItems.GetItem( COleVariant( (long)( i + 1 ) ) );

// if iRecurse == 0, list all directories as files

if( pItem.GetType() == VSSITEM_PROJECT && iRecurse ) {
getFileList( base, pItem, slFiles, iRecurse - 1, iFlags );
} else {
if( pItem.GetType() != VSSITEM_PROJECT ) {
if( ( iFlags & EF_ONLYCHECKEDOUT ) && !pItem.GetIsCheckedOut() ) {
continue;
}
if( ( iFlags & EF_ONLYCHECKEDIN ) && pItem.GetIsCheckedOut() ) {
continue;
}
}
string spec = (LPCTSTR)pItem.GetLocalSpec();
slFiles.push_back( spec.substr( base.length() + 1 ) );
}
}
}

bool XSourceSafe::XSourceSafeImp::EnumerateFiles( stringList& slFiles, int iRecurse, int iFlags )
{
if( !m_bOpen ) {
return false;
}

try {
IVSSItem vssProj = m_VSSDatabase.GetVSSItem( m_strCurProj.c_str(), 0 );
string base = (LPCTSTR)vssProj.GetLocalSpec();

getFileList( base, vssProj, slFiles, iRecurse, iFlags );
}
catch( ... ) {
return false;
}

return true;
}

bool XSourceSafe::XSourceSafeImp::Get( const string& strFileName )
{
string filespec = m_strCurProj + string("
/") + strFileName;

try {
IVSSItem vssItem = m_VSSDatabase.GetVSSItem( CString(filespec.c_str()), 0 );

vssItem.Get( NULL, VSSFLAG_RECURSYES );
}
catch( ... ) {
return false;
}

return true;
}

bool XSourceSafe::XSourceSafeImp::CheckOut( const string& strFileName, bool bReplace )
{
string filespec = m_strCurProj + string("
/") + strFileName;

try {
IVSSItem vssItem = m_VSSDatabase.GetVSSItem( CString(filespec.c_str()), 0 );

if( bReplace ) {
vssItem.Checkout( NULL, NULL, VSSFLAG_REPREPLACE|VSSFLAG_CHKEXCLUSIVEYES|VSSFLAG_RECURSYES );
} else {
vssItem.Checkout( NULL, NULL, VSSFLAG_REPSKIP|VSSFLAG_CHKEXCLUSIVEYES|VSSFLAG_RECURSYES );
}
}
catch( ... ) {
return false;
}

return true;
}

bool XSourceSafe::XSourceSafeImp::CheckIn( const string& strFileName )
{
string filespec = m_strCurProj + string("
/") + strFileName;

try {
IVSSItem vssItem = m_VSSDatabase.GetVSSItem( CString(filespec.c_str()), 0 );

if( vssItem.GetType() == VSSITEM_PROJECT ) {
vssItem.Checkin( NULL, NULL, VSSFLAG_UPDUPDATE|VSSFLAG_RECURSYES );
} else {
if( vssItem.GetIsDifferent( NULL ) ) {
vssItem.Checkin( NULL ,NULL, VSSFLAG_UPDUPDATE );
} else {
vssItem.UndoCheckout( NULL, 0 );
}
}
}
catch( ... ) {
return false;
}

return true;
}


I''d be happy to answer questions, etc.
-scott

Share this post


Link to post
Share on other sites
Even if you don''t have questions, let me know how it worked out for you. The code didn''t get any official use - our art team(for whom the tool was written) is still resisting the idea of source control for their work, not matter how dumbed down the interface is - so, while my little tests seemed to work, it never got the full-scale testing it probably needs.

Best of luck with it.
-scott

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!