need help converting C code

BlitzMax Forums/BlitzMax Programming/need help converting C code

Gillissie(Posted 2009) [#1]
I wrote this code and compiled it to a DLL for use in Blitz3D. However, I don't know how to get it converted for BlitzMax usage. Can someone please help me? It's simply a function to return a file's modify date in various formats.

#include <windows.h>
#include <stdio.h>

#define BBDECL extern "C" _declspec(dllexport)
#define BBCALL _stdcall


BBDECL const char * BBCALL FileModifyDate(const char *strFilepath,const int intFormat)
{
	char* strValue = "";
	char strTemp[32] = "";
	char strMinutesTemp[3] = "";
	char strSecondsTemp[3] = "";
	char strMinutes[3] = "0";
	char strSeconds[3] = "0";

	WIN32_FIND_DATA w32fd;
	HANDLE fh = FindFirstFile(strFilepath,&w32fd);
	if (fh != INVALID_HANDLE_VALUE)
	{
		FILETIME FileTime = w32fd.ftLastWriteTime;
		FILETIME LocalFileTime;
		SYSTEMTIME SystemTime;

		if (FileTimeToLocalFileTime(&FileTime,&LocalFileTime) == 0)
			return "";

		if (FileTimeToSystemTime(&LocalFileTime,&SystemTime) == 0)
			return "";

		sprintf(strMinutesTemp,"%d",SystemTime.wMinute);
		sprintf(strSecondsTemp,"%d",SystemTime.wSecond);

		// Pad single-digit minutes and seconds with 0's.
		if (strlen(strMinutesTemp) == 1)
			strcat(strMinutes,strMinutesTemp);
		else
			strcpy(strMinutes,strMinutesTemp);

		if (strlen(strSecondsTemp) == 1)
			strcat(strSeconds,strSecondsTemp);
		else
			strcpy(strSeconds,strSecondsTemp);

		switch (intFormat)
		{
			case 0:	// "day/month/year hour:minute:seconds"
				sprintf(strTemp,"%d/%d/%d %d:%s:%s",SystemTime.wMonth,SystemTime.wDay,SystemTime.wYear,SystemTime.wHour,strMinutes,strSeconds);
				break;
			case 1:	// "day/month/year"
				sprintf(strTemp,"%d/%d/%d",SystemTime.wMonth,SystemTime.wDay,SystemTime.wYear);
				break;
			case 2:	// "hour:minute:seconds"
				sprintf(strTemp,"%d:%s:%s",SystemTime.wHour,strMinutes,strSeconds);
				break;
			case 3:	// time in seconds since midnight
				int intSeconds = 3600 * SystemTime.wHour + 60 * SystemTime.wMinute + SystemTime.wSecond;
				sprintf(strTemp,"%d",intSeconds);
				break;
		}
		strcpy(strValue,strTemp);

		FindClose(fh);
	}

	return strValue;
}



Gillissie(Posted 2009) [#2]
I just noticed the BlitzMax function named FileTime(). However, I don't know how to convert the return value of that to something readable. Anyone know?


Gillissie(Posted 2009) [#3]
Nevermind, I found this http://www.blitzbasic.com/Community/posts.php?topic=71517#799590

Is it possible to delete your own thread around here?


Gabriel(Posted 2009) [#4]
For future reference, you don't need to convert C code at all in order to use it with BlitzMax. Assuming it compiles with MinGW, you can just import it, and declare the function(s) in an extern block and it will be linked right into your project. I won't go into all that since you've already found a valid solution this time, but there is plenty of information around on the forum should you wish to do this for anything else.


Gillissie(Posted 2009) [#5]
Gabriel, that's what I meant by convert. I first tried the Extern thing, but it failed miserably. I probably really don't know what I'm doing there. The only example in the docs doesn't require any C headers. Do you know of any other examples? I may need to convert some other B3D dll's and I'd rather have them compile right into my Max program instead.

Is that TV3D module related to this thread, or is it your signature?


Brucey(Posted 2009) [#6]
Get rid of BBDECL and BBCALL and it should compile in okay... (or even define then as nothing for a GCC build?)
const char * FileModifyDate(....

...assuming the rest of the C is valid under GCC.
Not sure if strValue is leak free or not though.


Ian C(Posted 2009) [#7]
Never mind leak free; it's not nasal demon free!!

That C code is nasty old undefined behaviour as you are trying to modify a string constant (the memory strValue is pointing to).


Brucey(Posted 2009) [#8]
Yeah, I'd be more inclined to use stat_ directly...

If you look at the source for FileTime :
	Local mode,size,mtime,ctime
	If stat_( path,mode,size,mtime,ctime ) Return 0

mtime and ctim are probably the ones you are most interested in.
I believe the time_t struct (that's the real param type for mtime and ctime) is simply an Int representing a unix time (from Jan 1970).


Yan(Posted 2009) [#9]
Slightly off topic, but I have this...
Strict

Import Pub.StdC

Local filePath$ = RequestFile("Choose a file to examine...", "All Files:*.*")

Print
Print filePath$
Print "Created  : " + FileTimeString(filePath$, , False)
Print "Modified : " + FileTimeString(filePath$)

End


Function FileTimeString$(path$, format$="%A %d %B %Y - %H:%M:%S (%Z)", modifiedTime=True)
	Local mode, size, mtime, ctime, lTime@[256]
	
	FixPath(path$)
	
	If stat_(path$, mode, size, mtime, ctime) Then Return

	If modifiedTime
		strftime_(lTime@, lTime@.length, format$, localtime_(Varptr(mtime)))
	Else
		strftime_(lTime@, lTime@.length, format$, localtime_(Varptr(ctime)))
	EndIf
	
	Return String.FromCString(lTime@)
End Function
...if it's of any use?