pastebin

Paste Search Dynamic
Recent pastes
SDFCompleteDirectoryChecked
  1. #include <iostream>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. #define SDF_SYSTEM_PATH_LINK_CHARACTER "/"
  6. #define SDF_SYSTEM_PATH_LINK_CHAR '/'
  7.  
  8. using namespace std;
  9.  
  10. bool SDFCompleteDirectoryChecked(char * const vpc_ResultPath, const uint ui_Size, const char * const vpc_Path, const char * const vpc_Directory)
  11. {
  12.     char  *pc_Buffer = null;
  13.     uint   ui_BufferLen = 0;
  14.     char  *pc_Directory = null;
  15.     uint   ui_DirectoryLen = 0;
  16.     int   ui_ResultPathLen = 0;
  17.  
  18.  
  19.     ui_BufferLen = (uint)strlen(vpc_Path)
  20.                  + (uint)strlen(SDF_SYSTEM_PATH_LINK_CHARACTER)
  21.                  + (uint)strlen(vpc_Directory)
  22.                  + (uint)strlen(SDF_SYSTEM_PATH_LINK_CHARACTER) + 1;
  23.  
  24.     ui_DirectoryLen = (uint)strlen(SDF_SYSTEM_PATH_LINK_CHARACTER)
  25.                     + (uint)strlen(vpc_Directory) + 1;
  26.  
  27.     /* allocate memory for vpc_Pathvpc_FileName */
  28.     pc_Buffer = (char*)malloc(sizeof(char) * ui_BufferLen);
  29.  
  30.     strncpy(pc_Buffer, vpc_Path, ui_BufferLen);
  31.     pc_Buffer[ui_BufferLen-1] = '';
  32.  
  33.     /* remove '/' from the end of path, if exists */
  34.     if (pc_Buffer[strlen(pc_Buffer)-1] == SDF_SYSTEM_PATH_LINK_CHAR)
  35.         pc_Buffer[strlen(pc_Buffer)-1] = '';
  36.        
  37.     /* prepare string with "/vpc_Directory" */
  38.     pc_Directory = (char*)malloc(sizeof(char) * ui_DirectoryLen);
  39.     snprintf(pc_Directory, ui_DirectoryLen, "%s%s", SDF_SYSTEM_PATH_LINK_CHARACTER, vpc_Directory);
  40.  
  41.     /* add "/vpc_Directory" at the end of the path if it doesn't end with it */
  42.     strncat(pc_Buffer, pc_Directory, ui_BufferLen - strlen(pc_Buffer));
  43.  
  44.     strncat(pc_Buffer, SDF_SYSTEM_PATH_LINK_CHARACTER, ui_BufferLen - strlen(pc_Buffer));
  45.  
  46.     /* copy buffer to ResultPath */
  47.     ui_ResultPathLen = snprintf(vpc_ResultPath, ui_Size, "%s", pc_Buffer);
  48.  
  49.     /* check if result fit in user given char array */
  50.     if(ui_ResultPathLen < 0 || (uint)ui_ResultPathLen >= ui_Size)
  51.         return false;
  52.  
  53.  
  54.     free(pc_Buffer);
  55.     free(pc_Directory);
  56.     return true;
  57. }
  58.  
  59.  
  60. int main() {
  61.         // your code goes here
  62.         char Result[4001];
  63.         char Path[] = "/home/dps/exchange_bpc/bpc_temp";
  64.         char Directory[] = "search";
  65.        
  66.         SDFCompleteDirectoryChecked(Result, 4001, Path, Directory);
  67.         std::cout << Result;
  68.         return 0;
  69. }
Parsed in 0.031 seconds