Storing directory filenames in a Text array?
1 day 7 hours ago - 1 day 2 hours ago #242
by ludojoey
Storing directory filenames in a Text array? was created by ludojoey
I’m running into a small programming issue.
The goal is quite simple: I want to read the list of files in a directory and store that list in an array.
In theory, this should be straightforward... But I can’t seem to get it to work properly in LOFI.
Maybe the solution is actually very simple and I’m just missing it! (Sometimes, often, we tend to overcomplicate things for no reason)
The problem is that it is not possible to assign a text variable using the = operator.
The only options seem to be using concat() or directly manipulating memory with poke.
My latest idea was therefore to initialize each element of the array with a string of 12 spaces (the maximum number of characters for a filename), and then use concat() to copy the filename into it.
To do this, I first set the first byte of the string to 0, in order to "trick" concat() into writing the filename at the beginning of the array element.
Here is the code:
text fileList[10];
text t={12};
nat a;
nat nn;
byte numFiles;
bool bb;
fOpDir(".");
while(true) {
bb=fRdDir(&t,&nn);
if (peek(&t)==0)
break;
if (bb==false) {
fileList[numFiles]=" ";
poke(peekW(&fileList+(nat)(2*numFiles)),0);
concat(fileList[numFiles],(text)(&t));
numFiles++;
if (numFiles==10) break;
}
}
print("numfiles=",numFiles,"
");
for(byte i;i<numFiles;i++) {
print(fileList,"
");
}
However, this doesn't work as expected.
I also try to make a simple :
fileList[numFiles]=t;
but as a result fileList[numFiles] is an empty string.
From what I can see, LOFI seems to make several elements of a text array point to the same memory area when they contain exactly the same string.
This is actually a good optimization for saving memory, but it causes a problem in my case.
The following line:
fileList[numFiles]=" ";
seems to make all the elements initialized with the same string point to the same memory area.
As a result, all the elements of the array end up containing the same value.
My idea would be to make the initialization string different at each iteration, so that LOFI uses a different memory area for each element.
But I don't see how to do this, since LOFI doesn't provide functions for dynamically creating or manipulating strings (chr(), etc.).
Does anyone have an idea how to solve this problem?
P.S. I could perhaps initialize each element of the array with a different value when declaring the array, but that seems rather cumbersome, especially since I would like the array to be able to store up to 150 different filenames.
EDIT: I checked the "fselect" from Dav id.
It doesn't use array, but "big" buffer.
It could be a solution. I am going to see that!
The goal is quite simple: I want to read the list of files in a directory and store that list in an array.
In theory, this should be straightforward... But I can’t seem to get it to work properly in LOFI.
Maybe the solution is actually very simple and I’m just missing it! (Sometimes, often, we tend to overcomplicate things for no reason)
The problem is that it is not possible to assign a text variable using the = operator.
The only options seem to be using concat() or directly manipulating memory with poke.
My latest idea was therefore to initialize each element of the array with a string of 12 spaces (the maximum number of characters for a filename), and then use concat() to copy the filename into it.
To do this, I first set the first byte of the string to 0, in order to "trick" concat() into writing the filename at the beginning of the array element.
Here is the code:
text fileList[10];
text t={12};
nat a;
nat nn;
byte numFiles;
bool bb;
fOpDir(".");
while(true) {
bb=fRdDir(&t,&nn);
if (peek(&t)==0)
break;
if (bb==false) {
fileList[numFiles]=" ";
poke(peekW(&fileList+(nat)(2*numFiles)),0);
concat(fileList[numFiles],(text)(&t));
numFiles++;
if (numFiles==10) break;
}
}
print("numfiles=",numFiles,"
");
for(byte i;i<numFiles;i++) {
print(fileList,"
");
}
However, this doesn't work as expected.
I also try to make a simple :
fileList[numFiles]=t;
but as a result fileList[numFiles] is an empty string.
From what I can see, LOFI seems to make several elements of a text array point to the same memory area when they contain exactly the same string.
This is actually a good optimization for saving memory, but it causes a problem in my case.
The following line:
fileList[numFiles]=" ";
seems to make all the elements initialized with the same string point to the same memory area.
As a result, all the elements of the array end up containing the same value.
My idea would be to make the initialization string different at each iteration, so that LOFI uses a different memory area for each element.
But I don't see how to do this, since LOFI doesn't provide functions for dynamically creating or manipulating strings (chr(), etc.).
Does anyone have an idea how to solve this problem?
P.S. I could perhaps initialize each element of the array with a different value when declaring the array, but that seems rather cumbersome, especially since I would like the array to be able to store up to 150 different filenames.
EDIT: I checked the "fselect" from Dav id.
It doesn't use array, but "big" buffer.
It could be a solution. I am going to see that!
Last edit: 1 day 2 hours ago by ludojoey.
Please Log in or Create an account to join the conversation.
1 day 2 hours ago - 1 day 2 hours ago #243
by ludojoey
Replied by ludojoey on topic Storing directory filenames in a Text array?
I’ll answer myself!
The solution I found is to create a custom type containing a single text variable with a length of 12, instead of using an array of text variables directly.
Then I create my array using this custom type.
type tfilelist {
text fileName={12};
};
tfilelist fileList[10];
text t={12};
nat a;
nat nn;
byte numFiles;
bool bb;
fOpDir(".");
while(true) {
bb=fRdDir(&t,&nn);
if (peek(&t)==0)
break;
if (bb==false) {
print("t=",t,"
");
fileList[numFiles].fileName=t;
numFiles++;
if (numFiles==10) break;
}
}
print("numfiles=",numFiles,"
");
for(byte i;i<numFiles;i++) {
print("i=",i," ",fileList.fileName,"
");
}
Problem solved!
The solution I found is to create a custom type containing a single text variable with a length of 12, instead of using an array of text variables directly.
Then I create my array using this custom type.
type tfilelist {
text fileName={12};
};
tfilelist fileList[10];
text t={12};
nat a;
nat nn;
byte numFiles;
bool bb;
fOpDir(".");
while(true) {
bb=fRdDir(&t,&nn);
if (peek(&t)==0)
break;
if (bb==false) {
print("t=",t,"
");
fileList[numFiles].fileName=t;
numFiles++;
if (numFiles==10) break;
}
}
print("numfiles=",numFiles,"
");
for(byte i;i<numFiles;i++) {
print("i=",i," ",fileList.fileName,"
");
}
Problem solved!
Last edit: 1 day 2 hours ago by ludojoey.
Please Log in or Create an account to join the conversation.
Time to create page: 0.156 seconds