Guest User

mOS vfs_path_compose

a guest
Feb 1st, 2015
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.42 KB | None | 0 0
  1. //vfs.h
  2. #define isdot(x) (x == '.')
  3. #define isfndelim(x) ((x == '/') || (x == 0))
  4.  
  5.  
  6. //vfs.c
  7. char* vfs_path_compose(char* path, char* buf, uint16_t size) {
  8.     char* bufstart = buf;
  9.     char* bufend;
  10.     char* csource;
  11.     uint8_t copyStage;
  12.     char oldchar;
  13.    
  14.     if (path[0] == '/') {
  15.         return path;
  16.     }
  17.    
  18.     //Don't copy, get the pointer directly
  19.     csource = vfs_getcwd(NULL, 0);
  20.    
  21.     //cwd may not be a relative path...
  22.     if (csource[0] != '/') {
  23.         return NULL;
  24.     }
  25.    
  26.    
  27.     copyStage = 0;
  28.     bufend = (char*)((uint16_t)buf + size - 1);
  29.    
  30.     //Add a slash to begin with (and check for buffer size >= 1)
  31.     if (buf >= bufend) {
  32.         return NULL;
  33.     }
  34.     *buf++ = '/';
  35.     *buf = 0;
  36.    
  37.     while (1) {
  38.         //Skip over any leading slashes in the source
  39.         while (csource[0] == '/') {
  40.             csource++;
  41.         }
  42.        
  43.         if (isdot(csource[0]) && isfndelim(csource[1])) {
  44.             //That's a change to "dot", do nothing but advancing forward
  45.            
  46.             //Skip over the dot
  47.             csource += 1;
  48.         } else if (isdot(csource[0]) && isdot(csource[1]) && isfndelim(csource[2])) {
  49.             //That's a change to "dot-dot", go one directory upward
  50.            
  51.             //Skip over dot-dot
  52.             csource += 2;
  53.            
  54.             //Remove any slashes at the end
  55.             while (*(buf - 1) == '/') {
  56.                 if ((uint16_t)buf <= (uint16_t)bufstart) {
  57.                     return NULL;
  58.                 }
  59.                 buf--;
  60.                 *buf = 0;
  61.             }
  62.            
  63.             //Remove the last directory
  64.             do {
  65.                 if ((uint16_t)buf <= (uint16_t)bufstart) {
  66.                     return NULL;
  67.                 }
  68.                 buf--;
  69.                 oldchar = *buf;
  70.                 *buf = 0;
  71.             } while (oldchar != '/');
  72.             *buf++ = '/'; //Add the slash at the end
  73.             *buf = 0;
  74.         } else if (csource[0] == 0) {
  75.             //End of source string reached.
  76.             //Switch to next string if there's another string left,
  77.             //finish operation if there's nothing left
  78.             if (copyStage == 0) {
  79.                 csource = path;
  80.                 copyStage = 1;
  81.             } else {
  82.                 break;
  83.             }
  84.         } else {
  85.             //Normal directory, just copy
  86.            
  87.             //Just in case there's no slash at the end of the buffer
  88.             if (*(buf - 1) != '/') {
  89.                 if (buf == bufend) {
  90.                     //Overflow...
  91.                     return NULL;
  92.                 }
  93.                 *buf++ = '/';
  94.                 *buf = 0;
  95.             }
  96.            
  97.             //Copy up to (not including) the next slash or string end
  98.             while (!isfndelim(csource[0])) {
  99.                 if (buf == bufend) {
  100.                     return NULL;
  101.                 }
  102.                 *buf++ = *csource++;
  103.                 *buf = 0;
  104.             }
  105.            
  106.             //Add a slash at the end of the buffer
  107.             if (buf == bufend) {
  108.                 return NULL;
  109.             }
  110.             *buf++ = '/';
  111.             *buf = 0;
  112.         }
  113.        
  114.     }
  115.    
  116.     return bufstart;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment