www.digitalmars.com         C & C++   DMDScript  

D - something evil with array concatenation in recursive functions

reply Ruslanas Abdrachimovas <anubis 03bar.ktu.lt> writes:
Hello,

I have a proble running this code:

=========================================================
void getAllFiles(char[] dir, List lst, char[] name)
{
     WIN32_FIND_DATA currentFileData;

     printf("Search root [%s]\n", (char*)dir);

     char[] searchName = dir[0..dir.length];
     searchName ~= "\\*.*";  // this line works only first time :(((((
     printf("Search name [%s]\n", (char*)searchName);
     HANDLE search = FindFirstFileA((char*)searchName, 
&currentFileData);

     if (search == INVALID_HANDLE_VALUE)
     {
         printf("Invalid handle returning.\n\n");
         FindClose(search);
         return;
     }

     BOOL next = true;
     while (next != false)
     {
         printf("Found file base name [%s]\n", 
(char*)currentFileData.cFileName);

         if (strcmp(currentFileData.cFileName, ".") != 0 &&
             strcmp(currentFileData.cFileName, "..") != 0)
         {
             //if ((currentFileData.dwFileAttributes & 
FILE_ATTRIBUTE_DIRECTORY == FILE_ATTRIBUTE_DIRECTORY) &&
             //    (currentFileData.dwFileAttributes & 
FILE_ATTRIBUTE_ARCHIVE != FILE_ATTRIBUTE_ARCHIVE))
             char[] fullPath = dir ~ '\' ~ currentFileData.cFileName;
             printf("Found file full name [%s]\n", (char*)fullPath);

             printf("\tAttributes [0x%08xh]\n", 
GetFileAttributesA((char*)fullPath));
             printf("\tMasked [0x%08xh]\n", 
(GetFileAttributesA((char*)fullPath) & FILE_ATTRIBUTE_DIRECTORY));
             if ((GetFileAttributesA((char*)fullPath) & 
FILE_ATTRIBUTE_DIRECTORY) != 0)
             {
                 printf("Is directory\n");
                 getAllFiles(fullPath, lst, name);
             }
             else
             {
                 printf("Is file\n");
                 printf("Adding new item to list\n");

                 Item itm = new Item();
                 int len = strlen(currentFileData.cFileName);
                 itm.str = new char[len];
                 itm.str[0..len] = currentFileData.cFileName[0..len];
                 lst.add(itm);
             }
         }

         printf("Search next file\n");
         next = FindNextFileA(search, &currentFileData);
         printf("\n");
     }

     FindClose(search);
}
=========================================================

Maybe some body knows what the problem is?

Ruslanas
Jun 21 2002
parent reply "Andrew Edwards" <crxace13 comcast.net> writes:
not much I can do for you Ruslanas but I did notice several [%s] which
should read [%.*s] in order to process correctly in D if I'm not mistaking!
Can someone back me up on this one?

Andrew

"Ruslanas Abdrachimovas" <anubis 03bar.ktu.lt> wrote in message
news:3D132DD8.6090605 03bar.ktu.lt...
| Hello,
|
| I have a proble running this code:
|
| =========================================================
| void getAllFiles(char[] dir, List lst, char[] name)
| {
|      WIN32_FIND_DATA currentFileData;
|
|      printf("Search root [%s]\n", (char*)dir);
|
|      char[] searchName = dir[0..dir.length];
|      searchName ~= "\\*.*";  // this line works only first time :(((((
|      printf("Search name [%s]\n", (char*)searchName);
|      HANDLE search = FindFirstFileA((char*)searchName,
| &currentFileData);
|
|      if (search == INVALID_HANDLE_VALUE)
|      {
|          printf("Invalid handle returning.\n\n");
|          FindClose(search);
|          return;
|      }
|
|      BOOL next = true;
|      while (next != false)
|      {
|          printf("Found file base name [%s]\n",
| (char*)currentFileData.cFileName);
|
|          if (strcmp(currentFileData.cFileName, ".") != 0 &&
|              strcmp(currentFileData.cFileName, "..") != 0)
|          {
|              //if ((currentFileData.dwFileAttributes &
| FILE_ATTRIBUTE_DIRECTORY == FILE_ATTRIBUTE_DIRECTORY) &&
|              //    (currentFileData.dwFileAttributes &
| FILE_ATTRIBUTE_ARCHIVE != FILE_ATTRIBUTE_ARCHIVE))
|              char[] fullPath = dir ~ '\' ~ currentFileData.cFileName;
|              printf("Found file full name [%s]\n", (char*)fullPath);
|
|              printf("\tAttributes [0x%08xh]\n",
| GetFileAttributesA((char*)fullPath));
|              printf("\tMasked [0x%08xh]\n",
| (GetFileAttributesA((char*)fullPath) & FILE_ATTRIBUTE_DIRECTORY));
|              if ((GetFileAttributesA((char*)fullPath) &
| FILE_ATTRIBUTE_DIRECTORY) != 0)
|              {
|                  printf("Is directory\n");
|                  getAllFiles(fullPath, lst, name);
|              }
|              else
|              {
|                  printf("Is file\n");
|                  printf("Adding new item to list\n");
|
|                  Item itm = new Item();
|                  int len = strlen(currentFileData.cFileName);
|                  itm.str = new char[len];
|                  itm.str[0..len] = currentFileData.cFileName[0..len];
|                  lst.add(itm);
|              }
|          }
|
|          printf("Search next file\n");
|          next = FindNextFileA(search, &currentFileData);
|          printf("\n");
|      }
|
|      FindClose(search);
| }
| =========================================================
|
| Maybe some body knows what the problem is?
|
| Ruslanas
|
Jun 21 2002
parent "Dario" <supdar yahoo.com> writes:
 not much I can do for you Ruslanas but I did notice several [%s] which
 should read [%.*s] in order to process correctly in D if I'm not

 Can someone back me up on this one?

 Andrew

You are not mistaking. D strings are not guaranteed to be zero-terminated C strings: only text between quotes ("blah" and 'blah') have an appended \0.
Jun 22 2002