Re: [CODE] Patrick Dughi's DBS in VC6

From: Henrik Stuart (hstuart@inoyfb.com)
Date: 12/20/02


Greetings,

>>   I've handpatched it into my 3.1 codebase, and the only problem I
>>   have with it is boards.c file which has an include of <dirent.h>.
>>   It simply doesn't exist in VC6, and while I know it handles
>>   directory structures, I can't seem to find a replacement for it
>>   within VC6. Is there any at all, or how should I manage without it?
>>   Thanks in advance.

dirent.h/scandir.c belong in the linux world, not in the windows
world, because, as Thomas stated, Windows doesn't use the same
directory structure as linux (and bsd, etc.) does.

Now, there is no need to complete dispair as these can be written to
wrap around Windows functionality - as follows.

scandir_win32.c:
----------------
#include <windows.h>
#include "dirent.h"

int scandir(const char *dirname, struct dirent ***namelist,
    int (*select)(struct dirent *),
    int (*compar)(struct dirent **, struct dirent **)) {
  int len;
  char *findIn, *d;
  WIN32_FIND_DATA find;
  HANDLE h;
  int nDir = 0, NDir = 0;
  struct dirent **dir = 0, *selectDir;
  unsigned long ret;

  len    = strlen(dirname);
  findIn = malloc(len+5);
  strcpy(findIn, dirname);
  for (d = findIn; *d; d++) if (*d=='/') *d='\\';
  if ((len==0)) { strcpy(findIn, ".\\*"); }
  if ((len==1)&& (d[-1]=='.')) { strcpy(findIn, ".\\*"); }
  if ((len>0) && (d[-1]=='\\')) { *d++ = '*'; *d = 0; }
  if ((len>1) && (d[-1]=='.') && (d[-2]=='\\')) { d[-1] = '*'; }

  if ((h=FindFirstFile(findIn, &find))==INVALID_HANDLE_VALUE) {
    ret = GetLastError();
    if (ret != ERROR_NO_MORE_FILES) {
      // TODO: return some error code
    }
    *namelist = dir;
    return nDir;
  }
  do {
    selectDir=(struct dirent*)malloc(sizeof(struct
dirent)+strlen(find.cFileName));
    strcpy(selectDir->d_name, find.cFileName);
    if (!select || (*select)(selectDir)) {
      if (nDir==NDir) {
 struct dirent **tempDir = calloc(sizeof(struct dirent*), NDir+33);
 if (NDir) memcpy(tempDir, dir, sizeof(struct dirent*)*NDir);
 if (dir) free(dir);
 dir = tempDir;
 NDir += 32;
      }
      dir[nDir] = selectDir;
      nDir++;
      dir[nDir] = 0;
    } else {
      free(selectDir);
    }
  } while (FindNextFile(h, &find));
  ret = GetLastError();
  if (ret != ERROR_NO_MORE_FILES) {
    // TODO: return some error code
  }
  FindClose(h);

  free (findIn);

  if (compar) qsort (dir, nDir, sizeof(*dir),
       (int(*)(const void*, const void*))compar);

  *namelist = dir;
  return nDir;
}

int alphasort (struct dirent **a, struct dirent **b) {
  return strcmp ((*a)->d_name, (*b)->d_name);
}
-------------------------- <snip> --------------------------

dirent.h:
---------
#ifndef dirent_h
#define dirent_h

struct dirent {
 char d_name[1];
};

int scandir(const char *dirname, struct dirent ***namelist,
    int (*select)(struct dirent *),
    int (*compar)(struct dirent **, struct dirent **));

int alphasort (struct dirent **a, struct dirent **b);

#endif /* dirent_h */
-------------------------- <snip> --------------------------

Including dirent.h into your INCLUDE environment variable (or the
include path in settings|options) you should be able to use it,
provided you add scandir_win32.c to your project as well.

The files are still a bit rudimentary and they are based strongly on
an initial port done by FLTK - I couldn't find the originals again
when I looked, hence the inclusion of these files here. If anyone
finishes them up to provide the full functionality that dirent.h
normally does, please drop me a line with an url or something
equivalent.

As  far  as  I recall from using the above files (I try to stay as far
away  from  C and platform-specific stuff that I can) then there might
be a few problems with trailing /'s or \\'s in dirs and specifying the
board  files/player files/etc. so you might want to make sure that the
right  path  is  actually  used in case any errors occur, e.g. that no
boards are found.

Otherwise, as Ronald pointed out in another post, then you can merely
use the actual numbers of the boards to load them in, rather than to
blindlessly browse the dir, but if you don't want to work with
rewriting the patch too much then the above files should help.

--
Yours truly,
  Henrik Stuart (http://www.unprompted.com/hstuart/)

--
   +---------------------------------------------------------------+
   | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
   | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
   | Newbie List:  http://groups.yahoo.com/group/circle-newbies/   |
   +---------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 06/25/03 PDT