Quantcast

Jump to content

» «
Photo

Getting all files in

2 replies to this topic
Bad.boy!
  • Bad.boy!

    SA modder

  • Feroci
  • Joined: 20 Jun 2010
  • Netherlands

#1

Posted 25 September 2012 - 08:04 PM

So I'm trying to create a function that searches for all files in a directory, but I want to be able to call it from C# without to much difficulties so I want the return to be a char pointer. Because I couldn't manage to do this using strcat_s and I don't want to use strings, the code is very "unsafe" and it doesn't work (because of the return). Can somebody tell me how to "fix" the code without rewriting the whole thing?

CODE
#include <Windows.h>
#include <strsafe.h>

extern "C"
{
char* GetFilesInFolders(LPCWSTR filedir, char* path)
{
 char* files = (char*)malloc(10000);
 strcpy(files, ""); // Anti crash

 char DefChar = ' ';
 bool LastPoint = false;

 WIN32_FIND_DATA ffd;
 TCHAR szDir[MAX_PATH];
 HANDLE hFind = INVALID_HANDLE_VALUE;
 DWORD dwError = 0;

 StringCchCopy(szDir, MAX_PATH, filedir);
 hFind = FindFirstFile(szDir, &ffd);

 if (INVALID_HANDLE_VALUE == hFind)
  return "";

 do
 {
  DWORD attributes = ffd.dwFileAttributes;
  LPCWSTR nm = ffd.cFileName;
  char name[260];
  WideCharToMultiByte(CP_ACP,0,ffd.cFileName,-1, name,260,&DefChar, NULL);

  for (int i = 0; i <= 260; i++)
  {
   if (name[i] == '.')
    LastPoint = true;
   else if (name[i] == ' ')
    break;
  }

  if (LastPoint == true)
  {
   LastPoint = false;
   continue;
  }

  if (attributes & FILE_ATTRIBUTE_HIDDEN)
  {
   continue;
  }
  else if (attributes & FILE_ATTRIBUTE_DIRECTORY)
  {
   char* add = "\\";
   char* extra = "*";
   size_t length = strlen(path) + strlen(name) + strlen(add);

   char* newpath = (char*)malloc(length + 1);
   strcpy(newpath, path);
   strcat(newpath, name);
   strcat(newpath, add);
   puts(newpath);

   char* newfiledir = (char*)malloc(length + 2);
   strcpy(newfiledir, path);
   strcat(newfiledir, name);
   strcat(newfiledir, add);
   strcat(newfiledir, extra);
   puts(newfiledir);

   size_t origsize = strlen(newfiledir) + 1;
   const size_t newsize = 100;
   size_t convertedChars = 0;
   wchar_t wcstring[newsize];
   mbstowcs_s(&convertedChars, wcstring, origsize, newfiledir, _TRUNCATE);
   LPCWSTR dir = wcstring;

   free(newfiledir);
   char* bestanden = GetFilesInFolders(dir, newpath);
   strcpy(files, bestanden);
   free(newpath);
  }
  else
  {
   size_t length = strlen(path) + strlen(name);
   char* file = (char*)malloc(length + 1);
   strcpy(file, path);
   strcat(file, name);
   puts(file);
   
   strcat(files, file);
   puts(files);
  }
 }
 while (FindNextFile(hFind, &ffd) != 0);

 FindClose(hFind);
 puts(files);
 return files;
}
}

nightwalker83
  • nightwalker83

    Don't mind me

  • Members
  • Joined: 10 Oct 2004
  • None

#2

Posted 26 September 2012 - 06:06 AM

What? Are you saying you want to call a c function from C#? Why not just write a function in C# to do the same thing?

Bad.boy!
  • Bad.boy!

    SA modder

  • Feroci
  • Joined: 20 Jun 2010
  • Netherlands

#3

Posted 26 September 2012 - 06:48 AM Edited by Bad.boy!, 26 September 2012 - 05:00 PM.

Because in C# finding all files is much slower. If I wanted to add a big list of files it would make a big difference.

EDIT:
Fixed the code, I used strcopy when I had to use strcat and the last char in a string is '\0', not ' '. I also forgot to change the bool back if the next char isn't a dot. I also increased the memory for "files". I now need to find a way to solve memory problems with many files.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users