PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Help with function win32 to wince functions

Ok, here is the first of many questions that I will be asking on my epic quest to pot a program that will be revealed when it is nearly done (or at least capable of having a screen shot taken of it.  If I port it completely, then at least the 720 owners, if not all ppc owners are in for a real treat.

Ok, Here are the first two functions I am haveing porblems finding in the hpc2000 sdk
1. access(const char *, int);  //Located in IO.h
2. _findfirst(const char *, struct _finddata_t *); //Located in IO.h

The code where these two functions are used will follow, but here is what I already know about them. (1 refers to access, and 2 reffers to _findfirst)
1. Checks to see if you have some type of permission to access a file.  In my case, checks for read permision.

//Code
miscvar=access(filename,4); //Checks to see if you can read the file

2. (Not sure about this one). Searches for all files that have "const char *", and stores results at "struct _finddata_t *" ???

//Code
if((miscvar=_findfirst("*.*",&miscvar2))!=-1) //Looks for all files???


If you could please help me emulate or get a good substitution for these files I would greately appriciate it.  Thanks.
Use CreateFile() to replace access().

Use FindFirstFile to replace _findfirst().

Here's an implementation for access() that I wrote.  I didn't test it but it should get you started.

Code:

#define ACCESS_EXIST 0
#define ACCESS_READ  2
#define ACCESS_WRITE 4
#define ACCESS_READWRITE 6

extern int errno = 0;

#define ENOENT 2
#define EACCES 13

int access (LPCTSTR lpFileName, int nMode)
{
HANDLE hFile;

   switch (nMode)
   {
   case ACCESS_EXIST:
       hFile = CreateFile(lpFileName, 0, 0, NULL, OPEN_EXISTING, 0, NULL);
       if (INVALID_HANDLE_VALUE == hFile)
       {
           errno = ENOENT;
           return -1;
       }
       else
       {
           CloseHandle(hFile);
           return 0;
       }

   case ACCESS_READ:
       hFile = CreateFile(lpFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
       if (INVALID_HANDLE_VALUE == hFile)
       {
           if (GetLastError() == ERROR_FILE_NOT_FOUND)
               errno = ENOENT;
           else
               errno = EACCES;
           return -1;
       }
       else
       {
           CloseHandle(hFile);
           return 0;
       }

   case ACCESS_WRITE:
       hFile = CreateFile(lpFileName, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
       if (INVALID_HANDLE_VALUE == hFile)
       {
           if (GetLastError() == ERROR_FILE_NOT_FOUND)
               errno = ENOENT;
           else
               errno = EACCES;
           return -1;
       }
       else
       {
           CloseHandle(hFile);
           return 0;
       }

   case ACCESS_READWRITE:
       hFile = CreateFile(lpFileName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
       if (INVALID_HANDLE_VALUE == hFile)
       {
           if (GetLastError() == ERROR_FILE_NOT_FOUND)
               errno = ENOENT;
           else
               errno = EACCES;
           return -1;
       }
       else
       {
           CloseHandle(hFile);
           return 0;
       }
  }

   return -1; // bad parameter?
}


Heh heh! :) I guess you type faster than me Digby. There, now you have two solutions to a problem using two different mechanisms, from two great coders. ;)

Dan East
The Windows API FindFirstFile, FindNextFile and FindClose will provide the solution to implementing both those functions. FindFirstFile and FindNextFile fill a struct (WIN32_FIND_DATA) with information about the found file (attributes, file times, size, etc). Check WIN32_FIND_DATA::dwFileAttributes for the bits FILE_ATTRIBUTE_READONLY to be set set for the access function.  Ahh, heck, I'll just code _access for you:
[fixed]
int _access(const char *path, int mode) {
 //Have to convert to UNICODE
 TCHAR fname[MAX_PATH];
 MultiByteToWideChar( CP_ACP, 0, path, -1,
   fname, sizeof( fname )/sizeof( TCHAR ) );
 
 WIN32_FIND_DATA ffd;
 //Get info on target file
 HANDLE h=FindFirstFile( fname, &ffd );

 if ( h==INVALID_HANDLE_VALUE )
   return -1;  //Can't find file
 FindClose( h );

 if (ffd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
   return 0; //Always return success if target is directory and exists

 switch ( mode ) {
   case 00: //Check existance
     return 0;
   case 06: //Check Read & Write permission
   case 02: //Check Write permission
     return ffd.dwFileAttributes&FILE_ATTRIBUTE_READONLY?-1:0;
   case 04: //Check Read permission
     return 0; //Assume always have read permission
 }
 //Bad mode value supplied, return failure
 return -1;
}[/fixed]

That assumes a file can always be read, thus it obviously is not taking into account file locking due to access by an application. I doubt the true implementation of _access does that anyways - it probably goes by file permissions only.

Dan East
You fuckin' guys!  GEEKS!

Ok, only because I didn't get here first...

:-D
ok, now use them both and tell us which code is better/faster:)
Alright, thanks for your help.  I'll try both of them out in a minute, and see which one works better.  You guys are great, but don't think that this was the only question you'll have to answer because there will be plenty more ;)
I have decided to just continue this thread with my questions as to not clutter the board with a bunch of similar threads.  Ok, here it goes.

Ok, first the results.  Digby, I used your access routine and Dan, I used some of the logic behind your idea, namely the bit checking.  So both were very helpful.  Moreover, you are both tied at 1 to 1.  Now for round two.  Ok, here is the next function (still dealing with files).  (Note: It is in the same source file, just incase that helps)
Code:

if( chdir(dta.cFileName)==0 ) //Changes the directory to dta.cFileName (was originally dta.name before _FindFirst was changed to FindFirstFile).

I have looked, but cannot find anything that would change a directory in WinCE.
Windows CE does not have a concept of a CWD (Current Working Directory). I would recommend creating a global variable in which you would store your own "CWD". When chdir is called you simply make sure that directory does indeed exist (using FindFirstFile), then copy that string into your global CWD. Now, for the tricky parts.

You would then need to take the CWD into account for every io function you implement that accepts a filename. Check the first character to see if it indicates root "\". If it does, they are specifying an absolute path, so leave it as is. If not, you have to cat it to your CWD value to build the full path. Note that things can get even more complex with those relative paths, for example "..\myfile.dat". You would then have to lop off the deepest directory in the CWD before you concatenate. You would have to check chdir calls for relative paths as well: chdir( ".." );

A final question is what you should initialize CWD to at startup. You could either specify the root \, or probably more useful, the path the running exe is located in. You can find that by extracting the path portion from the value returned by GetModuleFileName.

Dan East
Excellent Dan.  Keep up the good work.  I'm going to sit back and eat Frito pie and watch my new 50" plasma TV.

:) I wouldn't mind one of those at all. Today I'm running around the house, playing with my wireless LAN I just got up and going. :)

Dan East
So, what you're saying is that you have one heck of a t.v., and while I am trying to teach myself how to program better with porting exercises like this, you are watching t.v. and laughing (at some funny movie no doubt).  Well, I have just one thing to say to you, can I come over and join you.  Hay guess what.  On the HPC2000 in this text box, if you hold the backspace key, the cursor will go backwords, but the letters will not  be deleted untill you let go of the key.  Wierd.
Oops, I forgot to ask my question.  This should be a simple one, and I think that I already know the answer.  Instead of emulating the chdir function, since I think that it is only used once, and the Windows Ce doesn't use it anyways, couldn't I just remove it, and make sure that the remainder of the code in that function thinks that I called chdir by giving it a "fake result" of 0(and of courses tie up any loose ends that that creates)?
As long as all your calls to IO functions that take filenames specify absolute paths, then the CWD is never needed, and it should work.

Dan East