New pair of eyes please

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

Okay, I have two functions, the first formats the MSDOS file name, i.e. creates it.  The second searches for the said name and if found changes the name.

Okay, still here?

 

the format is 8 bit name, and 3 bit extension in a char array "dos_name".

 

when I input a name that is shorter than 8 characters and find a match, i.e. search returns that the name already exists the formatter pends the "~NUM" to far in for example: 

 

I expect the result to display WTEST~7TXT and so on, notice though that WWM. JENTXT and WWW. J~1TXT are correct.  in that case the file name was a max of 8 characters and 3 extension.

 

And the two functions are as follows:

void msdos_name_format(struct dir_entry * directory, char * name, char * dos_name){

	char * namePtr = name;
	int i = 0;

	memset(dos_name, '\0', 11);

	dos_name[0] = TAG;

	int crop = 0;

	// scan for last three bytes
	for(i = 0; *name != '\0'; name++, crop++);
	for(int k = 0; k < 3; k++, name--);

	// tag end of entry with the last three bytes
	for(int k = 0; k < 3; k++, name++){

		dos_name[8 + k] = *name;

		// MSDOS Name must be in upper case
		if((dos_name[8 + k] >= 'a') && (dos_name[8 + k] <= 'z')){
			dos_name[8 + k] -= 0x20;
		}
	}

	name = namePtr;

	crop -= 3;
	if(crop > 8)
		crop = 8;

	name = namePtr;

	for(i = 1; i < crop; i++, name++){

		dos_name[i] = * name;

		// MSDOS Name must be in upper case
		if((dos_name[i] >= 'a') && (dos_name[i] <= 'z')){
			dos_name[i] -= 0x20;
		}
	}
}
void validate_dos_name(struct ff_file * stream, char * dos_name){

	char extension[3];

	extension[0] = dos_name[8];
	extension[1] = dos_name[9];
	extension[2] = dos_name[10];

	int index = 0, i = 0;
	for(i = 7; dos_name[i] == '\0'; i--);

	// file/folder name indexing
	while(searh_ms_dos_name(stream, dos_name)){

		index++;

		if(index < 10){

			dos_name[i - 1] = '~';
			itoa(index, (char *)&dos_name[i], 10);
		}
		else if(index < 100){

			dos_name[i - 2] = '~';
			itoa(index, (char *)&dos_name[i - 1], 10);
		}
		else if(index < 1000){

			dos_name[i - 3] = '~';
			itoa(index, (char *)&dos_name[i - 2], 10);
		}
		else if(index < 10000){

			dos_name[i - 4] = '~';
			itoa(index, (char *)&dos_name[i - 3], 10);
		}
		dos_name[8]  = extension[0];
		dos_name[9]  = extension[1];
		dos_name[10] = extension[2];
	}
}

 

 

This topic has a solution.
Last Edited: Thu. Oct 24, 2019 - 12:44 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


of interest is the code snippet that should get the index where toinsert the "~NUM".

 

	int index = 0, i = 0;
	for(i = 7; dos_name[i] == '\0'; i--);

 

EDITED:

 

This:

	int index = 0, i = 0;
	for(; ((dos_name[i] != '\0') && (i < 8)); i++);

produces this:

Last Edited: Wed. Oct 23, 2019 - 09:43 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just to clarify, I'm looking to append "~NUM" to the end of the MSDOS file name.

So TEST.TXT becomes TEST~1.TXT

 

or

 

WILLIAM.TXT becomes WILLI~1.TXT

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Solution:

void validate_dos_name(struct ff_file * stream, char * dos_name){


	char extension[3];

	extension[0] = dos_name[8];
	extension[1] = dos_name[9];
	extension[2] = dos_name[10];


	int index = 0, i = 0;

	for(i = 7; dos_name[i] == '\0'; i--);
	if(++i > 5)
		i = 6;

	// file/folder name indexing
	while(searh_ms_dos_name(stream, dos_name)){

		index++;

		if(index < 10){

			dos_name[i] = '~';
			itoa(index, (char *)&dos_name[i + 1], 10);
		}
		else if(index < 100){

			dos_name[i - 1] = '~';
			itoa(index, (char *)&dos_name[i], 10);
		}
		else if(index < 1000){

			dos_name[i - 2] = '~';
			itoa(index, (char *)&dos_name[i - 1], 10);
		}
		else if(index < 10000){

			dos_name[i - 3] = '~';
			itoa(index, (char *)&dos_name[i - 2], 10);
		}
		dos_name[8]  = extension[0];
		dos_name[9]  = extension[1];
		dos_name[10] = extension[2];
	}
}

Thanks 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You are a great self helper!

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
	for(i = 7; dos_name[i] == '\0'; i--);
	if(++i > 5)
		i = 6;

Oof! I can't help thinking that sequences like this deserve some comments.

 

So the backwards for() loop is really just some kind of strlen()? I can't help thinking the actual use of strlen() might make this clearer to the reader.

 

But you actually want "strlen() + 1" (the position of the 0x00) and if it's anything above 5 then it can never be more than 6 ?