KNowing if a file changes

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
wxNewbi
Knows some wx things
Knows some wx things
Posts: 26
Joined: Mon Nov 15, 2004 11:27 pm
Location: London UK
Contact:

KNowing if a file changes

Post by wxNewbi » Fri Feb 18, 2005 10:03 am

does anyone know of a way I can tell if a file I have opened in my GUI has been changed by another progam?

thanks
Keep it real, Keep it free, Keep it GPL

mjs
Experienced Solver
Experienced Solver
Posts: 93
Joined: Wed Feb 09, 2005 3:53 am
Contact:

Post by mjs » Fri Feb 18, 2005 11:56 am

I think the only way to detect a file change is to query the

- file modification time (http://www.wxwidgets.org/manuals/2.5.3/ ... cationtime)
- size (http://www.wxwidgets.org/manuals/2.5.3/ ... tml#wxfile)

The size can be queried (AFAIK) using a wxFile::SeekEnd.

EDIT: Or does somebody know an easier way?

Regards,
Mark

wxNewbi
Knows some wx things
Knows some wx things
Posts: 26
Joined: Mon Nov 15, 2004 11:27 pm
Location: London UK
Contact:

Post by wxNewbi » Fri Feb 18, 2005 12:02 pm

Thanks mjs

that was exactly what I needed.
Keep it real, Keep it free, Keep it GPL

User avatar
ABX
Can't get richer than this
Can't get richer than this
Posts: 810
Joined: Mon Sep 06, 2004 1:43 pm
Location: Poznan, Poland
Contact:

Post by ABX » Fri Feb 18, 2005 12:16 pm

mjs wrote:EDIT: Or does somebody know an easier way?
I don't know what's the status of wxFAM, but see http://lists.wxwidgets.org/cgi-bin/ezml ... :sss:63777

ABX
CVS Head, 2.8.X
wxMSW, wxWinCE, wxPalmOS, wxOS2, wxMGL, bakefile
gcc 3.2.3, bcc 5.51, dmc 8.48, ow 1.6, vc 7.1, evc 3/4, pods 1.2

xee
Earned a small fee
Earned a small fee
Posts: 10
Joined: Sat Feb 19, 2005 9:26 pm

Post by xee » Sun Feb 20, 2005 8:58 am

I think it's be better(more precise) to use some kind of a hash function, like md5 or sha1. for the contents of the file. but it'd depend on file size, so if you're checking for a large file, it'll be slower.
here's a link for the implementation of MD5 in C++ http://www.codeproject.com/cpp/cmd5.asp

a better approach would be to use the three methods, modification date, then size, then hash function.

caseyodonnell
Knows some wx things
Knows some wx things
Posts: 31
Joined: Fri Sep 10, 2004 1:03 pm
Location: Troy, NY
Contact:

Another MD5 Implementation

Post by caseyodonnell » Mon Feb 21, 2005 6:52 am

xee wrote: here's a link for the implementation of MD5 in C++ http://www.codeproject.com/cpp/cmd5.asp
Here is a wxVersion of an MD5 class...you'll still need md5c.c, md5.h and global.h as a part of the standard MD5 distrobution.

The .H file:

Code: Select all

//////////////////////////////////////////////////////////////////////
//	Name:          wxMD5.h
//	Purpose:       wxMD5 Class
//	Author:        Casey O'Donnell
//	Creator:       Derived from the RSA Data Security, Inc.
//                       MD5 Message-Digest Algorithm
//                       See Internet RFC 1321
//                       Copyright (C) 1991 - 1992
//                       RSA Data Security, Inc.  Created 1991
//	Created:       07/02/2003
//	Last modified: 07/02/2003
//	Licence:       wxWindows license
//////////////////////////////////////////////////////////////////////

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

#ifndef _WXMD5_H__
#define _WXMD5_H__

#ifdef __GNUG__
    #pragma interface "wxMD5.h"
#endif

#include "wx/string.h"

class WXDLLEXPORT wxMD5  
{
public:
	wxMD5();
	wxMD5(const wxString& szText);
	virtual ~wxMD5();

	// Other Methods
	void SetText(const wxString& szText);

	const wxString GetDigest();

	// Static Methods
	static const wxString GetDigest(const wxString& szText);

protected:
	bool		m_bCalculatedDigest;
	wxUint8		m_arrDigest[16];
	wxChar		m_pszDigestString[33];
	wxString	m_szText;

private:
};

#endif // _WXMD5_H__
The .CPP file:

Code: Select all

//////////////////////////////////////////////////////////////////////
//	Name:          wxMD5.cpp
//	Purpose:       wxMD5 Class
//	Author:        Casey O'Donnell
//	Creator:       See Internet RFC 1321
//                       Copyright (C) 1991 - 1992
//                       RSA Data Security, Inc.  Created 1991
//	Created:       07/02/2003
//	Last modified: 07/02/2003
//	Licence:       wxWindows license
//////////////////////////////////////////////////////////////////////

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

#ifdef __GNUG__
    #pragma implementation "wxMD5.h"
#endif

// for compilers that support precompilation, includes "wx.h"
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

// for all others
#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif

#include "wxMD5.h"
#include "MD5.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

wxMD5::wxMD5()
{
	m_bCalculatedDigest = false;
	m_pszDigestString[32] = '\0';
}

wxMD5::wxMD5(const wxString& szText)
{
	m_bCalculatedDigest = false;
	m_pszDigestString[32] = '\0';
	m_szText = szText;
}

wxMD5::~wxMD5()
{
}

//////////////////////////////////////////////////////////////////////
// Other Methods
//////////////////////////////////////////////////////////////////////

void wxMD5::SetText(const wxString& szText)
{
	m_bCalculatedDigest = false;
	m_szText = szText;
}

const wxString wxMD5::GetDigest()
{
	if(m_bCalculatedDigest)
	{
		const wxString szRetVal = m_pszDigestString;
		return szRetVal;
	}
	else if(m_szText.IsEmpty())
	{
		return wxT("");
	}
	else
	{
		MD5_CTX md5Context;
		MD5Init(&md5Context);

		MD5Update(&md5Context, (unsigned char*)(m_szText.c_str()), m_szText.Len());
		MD5Final(m_arrDigest, &md5Context);

		int j = 0;
		for(int i = 0; i < 16; i++)
		{
			sprintf(&m_pszDigestString[j], "%02x", m_arrDigest[j]);
			j += 2;
		}

		const wxString szRetVal = m_pszDigestString;

		return szRetVal;
	}
}

//////////////////////////////////////////////////////////////////////
// Static Methods
//////////////////////////////////////////////////////////////////////

const wxString wxMD5::GetDigest(const wxString& szText)
{
	wxMD5 md5(szText);

	return md5.GetDigest();
}

Laurent22300
Knows some wx things
Knows some wx things
Posts: 30
Joined: Sun Sep 07, 2008 9:38 am
Contact:

Post by Laurent22300 » Wed Nov 12, 2008 4:32 pm

Thanks for this useful class :) I made some minor changes to it so that it can be used with a unicode version of wxWidgets. Here it is for those who may need it:

Code: Select all

//////////////////////////////////////////////////////////////////////
//      Name:          wxMD5.h
//      Purpose:       wxMD5 Class
//      Author:        Casey O'Donnell
//      Creator:       Derived from the RSA Data Security, Inc.
//                       MD5 Message-Digest Algorithm
//                       See Internet RFC 1321
//                       Copyright (C) 1991 - 1992
//                       RSA Data Security, Inc.  Created 1991
//      Created:       07/02/2003
//      Last modified: 07/02/2003
//      Licence:       wxWindows license
//////////////////////////////////////////////////////////////////////

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

#ifndef _WXMD5_H__
#define _WXMD5_H__

#ifdef __GNUG__
    #pragma interface "wxMD5.h"
#endif

#include "wx/string.h"

class WXDLLEXPORT wxMD5 
{
public:
        wxMD5();
        wxMD5(const wxString& szText);
        virtual ~wxMD5();

        // Other Methods
        void SetText(const wxString& szText);

        const wxString GetDigest();

        // Static Methods
        static const wxString GetDigest(const wxString& szText);

protected:
        bool        m_bCalculatedDigest;
        wxUint8  m_arrDigest[16];
        char    m_pszDigestString[33];
        wxString        m_szText;

private:
};

#endif // _WXMD5_H__ 

Code: Select all

//////////////////////////////////////////////////////////////////////
//      Name:          wxMD5.cpp
//      Purpose:       wxMD5 Class
//      Author:        Casey O'Donnell
//      Creator:       See Internet RFC 1321
//                       Copyright (C) 1991 - 1992
//                       RSA Data Security, Inc.  Created 1991
//      Created:       07/02/2003
//      Last modified: 07/02/2003
//      Licence:       wxWindows license
//////////////////////////////////////////////////////////////////////

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

#ifdef __GNUG__
    #pragma implementation "wxMD5.h"
#endif

// for compilers that support precompilation, includes "wx.h"
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

// for all others
#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif

#include "wxMD5.h"
#include "MD5.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

wxMD5::wxMD5()
{
        m_bCalculatedDigest = false;
        m_pszDigestString[32] = '\0';
}

wxMD5::wxMD5(const wxString& szText)
{
        m_bCalculatedDigest = false;
        m_pszDigestString[32] = '\0';
        m_szText = szText;
}

wxMD5::~wxMD5()
{
}

//////////////////////////////////////////////////////////////////////
// Other Methods
//////////////////////////////////////////////////////////////////////

void wxMD5::SetText(const wxString& szText)
{
        m_bCalculatedDigest = false;
        m_szText = szText;
}

const wxString wxMD5::GetDigest()
{
        if(m_bCalculatedDigest)
        {
          const wxString szRetVal(m_pszDigestString, wxConvUTF8);
                return szRetVal;
        }
        else if(m_szText.IsEmpty())
        {
                return wxT("");
        }
        else
        {
                MD5_CTX md5Context;
                MD5Init(&md5Context);

                MD5Update(&md5Context, (unsigned char*)(m_szText.c_str()), m_szText.Len());
                MD5Final(m_arrDigest, &md5Context);

                int j = 0;
                for(int i = 0; i < 16; i++)
                {
                  sprintf(&m_pszDigestString[j], "%02x", m_arrDigest[j]);
                        j += 2;
                }

                const wxString szRetVal(m_pszDigestString, wxConvUTF8);

                return szRetVal;
        }
}

//////////////////////////////////////////////////////////////////////
// Static Methods
//////////////////////////////////////////////////////////////////////

const wxString wxMD5::GetDigest(const wxString& szText)
{
        wxMD5 md5(szText);

        return md5.GetDigest();
}
[/code]

Post Reply