Advertisement
RaFaeLs

## SA:MP RL_FILES v1.2.7 ##

Feb 3rd, 2013
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 4.42 KB | None | 0 0
  1. /*
  2.  *                              rl_files 1.3.1
  3.  *        All right reserved! © By: RaFaeL
  4.  *
  5.  * Owner                : RaFaeL
  6.  * Time                 : 28.01.2013
  7.  *
  8.  * Thanks to:
  9.  *                      RaFaeL                           - Scripting, PAWN
  10.  *                      Kyosaur                          - How to build plugin
  11.  *                      Gamer_Z                          - Help in passing arrays by referance
  12.  *                      SA:MP Team past, present, future - SA:MP.
  13.  *
  14.  *
  15.  * Functions:
  16.  *          fcheck(dir[], idx) - Check if file exist by idx
  17.  *          fnum(dir[]) - Return how much files in dir
  18.  *          fname(dir[], idx, souce[], len = sizeof(souce)) - Return the file name by idx
  19.  *          fprint(dir[]) - Print all files in dir
  20.  *          fend(file[]) - Delete the file ext like .txt
  21.  *          farray(dir[], arr[][], &idx = 0, &returned = 0, step = sizeof(arr)) - create files array
  22.  *
  23.  *          Dir:dopen(const dir[]) - Open directory
  24.  *          dreopen(Dir:dir) - Reopen the directory (start file counting from 0)
  25.  *          dread(Dir:dir, file[], &type) - Get files and sub directories from directory
  26.  *          dclose(Dir:dir) - Close directory
  27.  *          dcreate(const dir[]) - Create new directory
  28.  *          dremove(const dir[], bool:empty = true) - Delete exist directory
  29.  *          drename(const oldname[], const newname[]) - Rename directory to new name
  30.  *          dexist(const dir[]) - Check if directory exist
  31.  *
  32.  * CallBacks:
  33.  *          None.
  34.  *
  35.  * Update:
  36.  *      28.01.2013:
  37.  *          Other:              - First Release
  38.  *                              - Bug fixed
  39.  *      29.01.2013:
  40.  *          Functions:          - fend(file[], len = sizeof(file))
  41.  *                              -
  42.  *
  43.  *          Other:
  44.  *                              - fnum(dir[]) changed to PAWN scripted function
  45.  *                              - No meter if "." or "" directory
  46.  *      31.01.2013:
  47.  *          Other:              - Fixed bug while reading directorys
  48.  *                              - Default dir[] = "."
  49.  *      01.02.2013:
  50.  *          Functions:          - farray(dir[], arr[][], &idx = 0, &returned = 0, step = sizeof(arr))
  51.  *                              -
  52.  *      02.02.2013:
  53.  *          Functions:          - Dir:dopen(const dir[])
  54.  *                              - dreopen(Dir:dir)
  55.  *                              - dread(Dir:dir, file[], &type)
  56.  *                              - dclose(Dir:dir)
  57.  *                              - dcreate(const dir[])
  58.  *                              - dremove(const dir[], bool:empty = true)
  59.  *                              - drename(const oldname[], const newname[])
  60.  *                              - dexist(const dir[])
  61.  *
  62.  *          Other:              -
  63.  *                              -
  64.  *
  65.  */
  66.  
  67. #if defined rl_files
  68.     #error files is already defined!
  69. #endif
  70. #define rl_files
  71.  
  72.  
  73. #define MAX_FILE_NAME         64
  74. #define NULL_DIR              "."
  75. #define PRINT_FORMAT          "%s/%s",dir,file
  76.  
  77. #define TYPE_FILE     0x1
  78. #define TYPE_DIR      0x10
  79. #define TYPE_UNKNOWN  0x100
  80.  
  81. //
  82.  
  83. native fname(const dir[], idx, souce[], len = sizeof(souce));
  84.  
  85. native Dir:dopen(const dir[]);
  86. native dreopen(Dir:dir);
  87. native dread(Dir:dir, file[], &type);
  88. native dclose(Dir:dir);
  89.  
  90. native dcreate(const dir[]);
  91. native dremove(const dir[], bool:empty = true);
  92. native drename(const oldname[], const newname[]);
  93. native dexist(const dir[]);
  94.  
  95. //
  96.  
  97. stock fprint(dir[] = NULL_DIR) {
  98.     new
  99.         idx,
  100.         file[MAX_FILE_NAME];
  101.     while(fname(dir, idx, file)) {
  102.         printf(PRINT_FORMAT);
  103.         idx++;
  104.     }
  105.     return 1;
  106. }
  107.  
  108. stock fnum(dir[] = NULL_DIR) {
  109.     new
  110.         idx,
  111.         file[MAX_FILE_NAME];
  112.     while(fname(dir, idx, file)) {
  113.         idx++;
  114.     }
  115.     return idx;
  116. }
  117.  
  118. stock fcheck(dir[], idx) {
  119.     new
  120.         file[MAX_FILE_NAME];
  121.     if(!fname(dir, idx, file))
  122.         return 0;
  123.     return 1;
  124. }
  125.  
  126. stock fend(file[]) {
  127.     new
  128.         ret[MAX_FILE_NAME],
  129.         pos = strfind(file, ".", true);
  130.     strcat((ret[0] = EOS, ret), file);
  131.     if(pos != -1)
  132.         ret[pos] = '\0';
  133.        
  134.     return ret;
  135. }
  136.  
  137. stock farray(dir[], array[][], &idx = 0, &returned = 0, step = sizeof(array), size = sizeof(array[])) {
  138.     new
  139.         idxMax = idx + step,
  140.         posWrite,
  141.        
  142.         file[MAX_FILE_NAME];
  143.        
  144.     returned = 0;
  145.        
  146.     while(fname(dir, idx, file) && idx < idxMax) if(posWrite < idxMax) {
  147.         strcat((array[posWrite][0] = EOS, array[posWrite]), file, size);
  148.         idx++;
  149.         posWrite++;
  150.     }
  151.     returned = posWrite;
  152.    
  153.     return (returned)? (fnum(dir) - returned):(0);
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement