forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

stdio - Path normalization

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PS2 Development
View previous topic :: View next topic  
Author Message
evilo



Joined: 22 Apr 2004
Posts: 230

PostPosted: Mon Sep 25, 2006 6:00 am    Post subject: stdio - Path normalization Reply with quote

Hi,

I wanted to add a normalization function in stdio.c

Code:

 /* Normalize a pathname by removing
  . and .. components, duplicated /, etc. */
char* __ps2_normalize_path(char *path_name)
{
   int i, j;
   int first, next;
   static char out[255];
   
   /* First copy the path into our temp buffer */
   strcpy(out, path_name);
        /* Then append "/" to make the rest easier */
   strcat(out,"/");

   /* Convert "//" to "/" */
   for(i=0; out[i+1]; i++) {
      if(out[i]=='/' && out[i+1]=='/') {
         for(j=i+1; out[j]; j++)
            out[j] = out[j+1];
         i--;
      }
   }

   /* Convert "/./" to "/" */
   for(i=0; out[i] && out[i+1] && out[i+2]; i++) {
      if(out[i]=='/' && out[i+1]=='.' && out[i+2]=='/') {
         for(j=i+1; out[j]; j++)
            out[j] = out[j+2];
         i--;
      }
   }

   /* Convert "/path/../" to "/" until we can't anymore.  Also
    * convert leading "/../" to "/" */
   first = next = 0;
   while(1) {
      /* If a "../" follows, remove it and the parent */
      if(out[next+1] && out[next+1]=='.' &&
         out[next+2] && out[next+2]=='.' &&
         out[next+3] && out[next+3]=='/') {
         for(j=0; out[first+j+1]; j++)
            out[first+j+1] = out[next+j+4];
         first = next = 0;
         continue;
      }

      /* Find next slash */
      first = next;
      for(next=first+1; out[next] && out[next] != '/'; next++)
         continue;
      if(!out[next]) break;
   }

   /* Remove trailing "/" */
   for(i=1; out[i]; i++)
      continue;
   if(i >= 1 && out[i-1] == '/')
      out[i-1] = 0;

   return (char*)out;
}



the function is greatly inspired by the one present in the pspsdk, so credits is for the orginal authors.

I wanted to call it from the fopen function :

Code:

 if (i < _NFILE) {
        char * t_fname = __ps2_normalize_path((char *)fname);
   char b_fname[FILENAME_MAX];
        __iob[i].type = __stdio_get_fd_type(fname);


any comments ?
_________________
http://psxdev.info/evilo/
Back to top
View user's profile Send private message Visit poster's website
evilo



Joined: 22 Apr 2004
Posts: 230

PostPosted: Tue Oct 10, 2006 11:36 pm    Post subject: Reply with quote

If no ones has comments on this, then I will commit it in the SVN

thank you.
_________________
http://psxdev.info/evilo/
Back to top
View user's profile Send private message Visit poster's website
Herben



Joined: 25 Jan 2004
Posts: 107

PostPosted: Wed Nov 22, 2006 4:04 pm    Post subject: Reply with quote

keep in mind that the official CDVDMAN cdrom: FS will not accept '/' for a delimiter. CDVDMAN will only accept '\'
Back to top
View user's profile Send private message
evilo



Joined: 22 Apr 2004
Posts: 230

PostPosted: Mon Nov 27, 2006 12:10 am    Post subject: Reply with quote

Yes I know, but since i'm not using it in my current project, and since it doesn't change "CDVD" path, I think I will keep it as it for now, and commit it in the current state.
_________________
http://psxdev.info/evilo/
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PS2 Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group