Commit 765e231a authored by Richard Levitte's avatar Richard Levitte
Browse files

From LPlib:

Some code beautification.

Change the macro CP_THREAD_ACP to CP_ACP, because the latter is more
widely defined.

Add a conditional macro definition in case FindFirstFile and
FindNextFile aren't properly defined (might happen on WinCE).

Suggested by Andy Polyakov <appro@fy.chalmers.se>
parent 89c53672
Loading
Loading
Loading
Loading
+19 −11
Original line number Diff line number Diff line
/* $LP: LPlib/source/LPdir_win.c,v 1.4 2004/07/20 21:15:55 _cvs_levitte Exp $ */
/* $LP: LPlib/source/LPdir_win.c,v 1.5 2004/07/20 22:39:13 _cvs_levitte Exp $ */
/*
 * Copyright (c) 2004, Richard Levitte <richard@levitte.org>
 * All rights reserved.
@@ -30,6 +30,12 @@
#include "LPdir.h"
#endif

/* It seems like WinCE doesn't always have the "normal" mapping
   macros.  We're opting for the UNICODE ones. */
#if defined(LP_SYS_WINCE) && !defined(FindFirstFile)
# define FindFirstFile FindFirstFileW
#endif

struct LP_dir_context_st
{
  WIN32_FIND_DATA ctx;
@@ -66,13 +72,14 @@ const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory)
	  wdir = (TCHAR *)malloc((len + 1) * sizeof(TCHAR));
	  if (wdir == NULL)
	    {
	      errno = ENOMEM;
	      free(*ctx);
	      *ctx = NULL;
	      errno = ENOMEM;
	      return 0;
	    }

#ifdef LP_MULTIBYTE_AVAILABLE
	  if (!MultiByteToWideChar (CP_THREAD_ACP,0,directory,len,wdir,len+1))
	  if (!MultiByteToWideChar(CP_ACP, 0, directory, len, wdir, len + 1))
#endif
	    for (index = 0; index < strlen(directory) + 1; index++)
	      wdir[index] = (TCHAR)directory[index];
@@ -103,17 +110,17 @@ const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory)
  if (sizeof(TCHAR) != sizeof(char))
    {
      TCHAR *wdir = (*ctx)->ctx.cFileName;
      size_t i,len;
      size_t index, len;

      for (len = 0; wdir[len] && len < (sizeof((*ctx)->entry_name) - 1);)
	len++;

#ifdef LP_MULTIBYTE_AVAILABLE
      if (!WideCharToMultiByte (CP_THREAD_ACP,0,wdir,len,
				(*ctx)->entry_name,
      if (!WideCharToMultiByte(CP_ACP, 0, wdir, len, (*ctx)->entry_name,
			       sizeof((*ctx)->entry_name) - 1, NULL, 0))
#endif
	for (i=0;i<len;i++) (*ctx)->entry_name[i] = (char)wdir[i];
	for (index = 0; index < len; index++)
	  (*ctx)->entry_name[index] = (char)wdir[index];
    }
  else
    strncpy((*ctx)->entry_name, (*ctx)->ctx.cFileName,
@@ -130,6 +137,7 @@ int LP_find_file_end(LP_DIR_CTX **ctx)
    {
      FindClose((*ctx)->handle);
      free(*ctx);
      *ctx = NULL;
      return 1;
    }
  errno = EINVAL;