Advertisement
BigETI

file_ex.inc

Jun 11th, 2013
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 25.03 KB | None | 0 0
  1. /*
  2.     FileEx (Extended file functions controlling and debugging script) made by BigETI © 2013
  3.    
  4.     This include allows you to control and debug your scripts which uses file functions from file.inc
  5.     Also by defining FILE_EX_ENABLE_REMOTE you allow your main script to send error messages to other scripts.
  6.  
  7.  
  8.    
  9.     - Links:
  10.     ==================================================
  11.         - file_ex.inc:              http://pastebin.com/w3S4YYv2
  12.         - file_ex_remote_def.inc:   http://pastebin.com/KNjpZkmP
  13.         - file_ex_remote.inc        http://pastebin.com/Vsf3Wd8j
  14.     ==================================================
  15.  
  16.  
  17.  
  18.     - Documentation:
  19.  
  20.     ==================================================
  21.  
  22.         - Definitions ( Those definitions below, if needed have to be defined on top of your script, above #include <file_ex> )
  23.  
  24.             -------------------------
  25.             - FILE_EX_NO_TRACKING
  26.  
  27.                 - Description:
  28.                     - Disables all tracking features of this include.
  29.                     - It makes result feedback and debugging useless.
  30.  
  31.                 - Usage:
  32.                     - #define FILE_EX_NO_TRACKING
  33.  
  34.             -------------------------
  35.             - FILE_EX_DEBUG_LEVEL
  36.  
  37.                 - Description:
  38.                     - Defines the current debug level your script should debug the file functions.
  39.                     - Useless, if defined FILE_EX_NO_TRACKING.
  40.  
  41.                 - Levels:
  42.                     - Level 0 (Default):
  43.                         - Nothing will be debugged!
  44.  
  45.                     - Level 1:
  46.                         - Functions like fopen, ftemp, fclose, fremove will be debugged.
  47.  
  48.                     - Level 2:
  49.                         - All other file functions will be debugged, also the ones from level 1.
  50.  
  51.                     - Level 3 and above:
  52.                         - Prints fwrite, fread, fputchar, fgetchar, fblockwrite, fblockread, fseek, fexist, and flenght inputs and outputs.
  53.                         - Also does the same as level 2 and level 1.
  54.  
  55.                 - Usage:
  56.                     - Example: #define FILE_EX_DEBUG_LEVEL  1
  57.  
  58.             -------------------------
  59.             - FILE_EX_ENABLE_REMOTE
  60.  
  61.                 - Description:
  62.                     - Error reports will be send to other scripts.
  63.                     - If needed use atleast a script with file_ex_remote.inc included
  64.                     - Define FILE_EX_SCRIPT_ID with an unique ID to identify your script from your other scripts!
  65.  
  66.                 - Usage:
  67.                     - #define FILE_EX_ENABLE_REMOTE
  68.  
  69.             -------------------------
  70.             - FILE_EX_SCRIPT_ID
  71.  
  72.                 - Description:
  73.                     - Gives your script an unique ID to be indentified from other scripts
  74.  
  75.                 - Usage:
  76.                     - Example:
  77.                         - #define FILE_EX_SCRIPT_ID 5
  78.  
  79.             -------------------------
  80.             - FILE_EX_FGETCHAR_FIX
  81.  
  82.                 - Description:
  83.                     - Fixes the native fgetchar's arguments.
  84.                     - Removes the second useless argument.
  85.                     - fgetchar(File:handle, bool:utf8 = true) instead of fgetchar(File:handle, value, bool:utf8 = true)
  86.                     - Do not use this, if you get compatiblitiy issues with your script.
  87.  
  88.                 - Usage
  89.                     - #define FILE_EX_FGETCHAR_FIX
  90.  
  91.             -------------------------
  92.  
  93.  
  94.         - Macros:
  95.        
  96.             -------------------------
  97.             - F_EX::file_read(const file_name[])<variable_name>
  98.            
  99.                 - Description:
  100.                     - Handles a file stream safely inside brackets.
  101.                     - Read only mode!
  102.                     - Much easier to open and close files.
  103.                     - It does not require fclose inside, hence DO NOT USE THIS IN THIS CASE!
  104.                     - Supports all file stream modes from file.inc
  105.                     - "variable_name" defines the variable, which will hold the file stream handle.
  106.                     - A variable in "variable_name" will be declared inside this macro, so do not declare a variable for this!
  107.  
  108.                 - Usage:
  109.                     - Example:
  110.                         F_EX::file_read("example.txt")<example_txt_handle>
  111.                         {
  112.                             new buffer[128];
  113.                             fread(example_txt_handle, buffer);
  114.                             print(buffer);
  115.                         }
  116.  
  117.  
  118.             -------------------------
  119.             - F_EX::file_write(const file_name[])<variable_name>
  120.  
  121.                 - Description:
  122.                     - Handles a file stream safely inside brackets.
  123.                     - Write only mode!
  124.                     - Much easier to open and close files.
  125.                     - It does not require fclose inside, hence DO NOT USE THIS IN THIS CASE!
  126.                     - Supports all file stream modes from file.inc
  127.                     - "variable_name" defines the variable, which will hold the file stream handle.
  128.                     - A variable in "variable_name" will be declared inside this macro, so do not declare a variable for this!
  129.  
  130.                 - Usage:
  131.                     - Example:
  132.                         F_EX::file_write("example.txt")<example_txt_handle>
  133.                         {
  134.                             fwrite(example_txt_handle, "Hello world!\r\n");
  135.                         }
  136.            
  137.             -------------------------
  138.             - F_EX::file_readwrite(const file_name[])<variable_name>
  139.  
  140.                 - Description:
  141.                     - Handles a file stream safely inside brackets.
  142.                     - Read and write mode.
  143.                     - Much easier to open and close files.
  144.                     - It does not require fclose inside, hence DO NOT USE THIS IN THIS CASE!
  145.                     - Supports all file stream modes from file.inc
  146.                     - "variable_name" defines the variable, which will hold the file stream handle.
  147.                     - A variable in "variable_name" will be declared inside this macro, so do not declare a variable for this!
  148.  
  149.                 - Usage:
  150.                     - Example:
  151.                         F_EX::file_readwrite("example.txt")<example_txt_handle>
  152.                         {
  153.                             fwrite(example_txt_handle, "Hello world!\r\n");
  154.                             new buffer[128];
  155.                             fread(example_txt_handle, buffer);
  156.                             print(buffer);
  157.                         }
  158.            
  159.             -------------------------
  160.             - F_EX::file_append(const file_name[])<variable_name>
  161.  
  162.                 - Description:
  163.                     - Handles a file stream safely inside brackets.
  164.                     - Append only mode!
  165.                     - Much easier to open and close files.
  166.                     - It does not require fclose inside, hence DO NOT USE THIS IN THIS CASE!
  167.                     - Supports all file stream modes from file.inc
  168.                     - "variable_name" defines the variable, which will hold the file stream handle.
  169.                     - A variable in "variable_name" will be declared inside this macro, so do not declare a variable for this!
  170.  
  171.                 - Usage:
  172.                     - Example:
  173.                         F_EX::file_append("example.txt")<example_txt_handle>
  174.                         {
  175.                             fwrite(example_txt_handle, "Hello world!\r\n");
  176.                         }
  177.            
  178.             -------------------------
  179.             - F_EX::temp_file<variable_name>
  180.  
  181.                 - Description:
  182.                     - Handles a temporary file stream safely inside brackets.
  183.                     - Much easier to open and close temporary files.
  184.                     - It does not require fclose inside, hence DO NOT USE THIS IN THIS CASE!
  185.                     - Supports all file stream modes from file.inc
  186.                     - "variable_name" defines the variable, which will hold the file stream handle.
  187.                     - A variable in "variable_name" will be declared inside this macro, so do not declare a variable for this!
  188.  
  189.                 - Usage:
  190.                     - Example:
  191.                         F_EX::temp_file<temp_file_handle>
  192.                         {
  193.                             new buffer = EOF;
  194.                             F:EX::open_read("example.txt", example_txt_handle) while((buffer = fgetchar(example_txt_handle, 0, false)) != EOF) fputchar(temp_file_handle, false);
  195.                             fseek(temp_file_handle);
  196.                             F:EX::open_write("example2.txt", example2_txt_handle)
  197.                             {
  198.                                 buffer = EOF;
  199.                                 while((buffer = fgetchar(temp_file_handle, 0, false)) != EOF) fputchar(example2_txt_handle, false);
  200.                             }
  201.                         }
  202.  
  203.             -------------------------
  204.  
  205.  
  206.         - Enums:
  207.  
  208.             -------------------------
  209.             - F_EX_RES::ENUM
  210.  
  211.                 - Description:
  212.                     - Used to identify specified results as numbers.
  213.  
  214.                 - Items:
  215.                     - F_EX_RES::OK
  216.                         - Result is okay.
  217.  
  218.                     - F_EX_RES::INV_FILE_ACCESS
  219.                         - Result shows an invalid file access.
  220.  
  221.                     - F_EX_RES::INV_TEMP_FILE_ACCESS
  222.                         - Result shows an invalid temporary file access.
  223.  
  224.                     - F_EX_RES::INV_FILE_S_PTR
  225.                         - Result shows an invalid file pointer access.
  226.  
  227.                     - F_EX_RES::EOF
  228.                         - Result shows that the end of a file has been reached.
  229.  
  230.             -------------------------
  231.  
  232.  
  233.         - Stocks:
  234.  
  235.             -------------------------
  236.             - stock F_EX_RES::ENUM: F_EX::get_result(bool:keep_result = true)
  237.  
  238.                 - Description:
  239.                     - Gets the last result ID.
  240.                     - If bool:keep_result is true it will not reset the last result.
  241.                     - Useless, if defined FILE_EX_NO_TRACKING.
  242.  
  243.                 - Usage:
  244.                     - Example:
  245.                         F_EX::fopen_read("example.txt", my_file)
  246.                         {
  247.                             print("Current result: %d", _:F_EX::get_result();
  248.                         }
  249.                        
  250.             -------------------------
  251.             - stock F_EX::print_result(bool:ignore_ok = true, bool:keep_result = true)
  252.  
  253.                 - Description:
  254.                     - Prints the last result properly into the console.
  255.                     - Does not print in OK results, if bool:ignore_ok is set to true.
  256.                     - If bool:keep_result is true it will not reset the last result.
  257.                     - Useless, if defined FILE_EX_NO_TRACKING.
  258.  
  259.                 - Usage:
  260.                     - Example:
  261.                         F_EX::fopen_read("example.txt", my_file)
  262.                         {
  263.                             F_EX::print_result(false);
  264.                         }
  265.  
  266.             -------------------------
  267.  
  268.  
  269.         - Hooked natives (without defining FILE_EX_NO_TRACKING):
  270.  
  271.             - native File:fopen(const name[], filemode: mode = io_readwrite);
  272.             - native bool:fclose(File: handle);
  273.             - native File:ftemp();
  274.             - native bool:fremove(const name[]);
  275.             - native fwrite(File: handle, const string[]);
  276.             - native fread(File: handle, string[], size = sizeof string, bool: pack = false);
  277.             - native bool:fputchar(File: handle, value, bool: utf8 = true);
  278.             - native fgetchar(File: handle, value, bool: utf8 = true);
  279.             - native fblockwrite(File: handle, const buffer[], size = sizeof buffer);
  280.             - native fblockread(File: handle, buffer[], size = sizeof buffer);
  281.             - native fseek(File: handle, position = 0, seek_whence: whence = seek_start);
  282.             - native flength(File: handle);
  283.             - native fexist(const pattern[]);
  284.  
  285.     ==================================================
  286. */
  287.  
  288. #if defined _FILE_EX_INCLUDED_
  289.     #endinput
  290. #endif
  291. #define _FILE_EX_INCLUDED_
  292. #if defined FILE_EX_NO_TRACKING
  293.     #include <file>
  294.     #define f_ex_inc_file_read%2(%0)%3<%1>      for(new File:%1=fopen(%0,io_read),bool:_%1__=true;_%1__;_%1__=(((%1) == File:0)?false:(fclose(%1)?false:false)))
  295.     #define f_ex_inc_file_write%2(%0)%3<%1>     for(new File:%1=fopen(%0,io_write),bool:_%1__=true;_%1__;_%1__=(((%1) == File:0)?false:(fclose(%1)?false:false)))
  296.     #define f_ex_inc_file_readwrite%2(%0)%3<%1> for(new File:%1=fopen(%0,io_readwrite),bool:_%1__=true;_%1__;_%1__=(((%1) == File:0)?false:(fclose(%1)?false:false)))
  297.     #define f_ex_inc_file_append%2(%0)%3<%1>    for(new File:%1=fopen(%0,io_append),bool:_%1__=true;_%1__;_%1__=(((%1) == File:0)?false:(fclose(%1)?false:false)))
  298.     #define f_ex_inc_temp_file%1<%0>            for(new File:%0=ftemp(),bool:_%0__=true;_%0__;_%0__=(((%0) == File:0)?false:(fclose(%0)?false:false)))
  299. #else
  300.     #if defined FILE_EX_ENABLE_REMOTE
  301.         #include <a_samp>
  302.         #include <file_ex_remote_def>
  303.     #else
  304.         #include <file>
  305.     #endif
  306.     #if !defined FILE_EX_DEBUG_LEVEL
  307.         #define FILE_EX_DEBUG_LEVEL 0
  308.     #endif
  309. enum f_ex_res_ENUM
  310. {
  311.     f_ex_res_OK,
  312.     f_ex_res_INV_FILE_ACCESS,
  313.     f_ex_res_INV_TEMP_FILE_ACCESS,
  314.     f_ex_res_INV_FILE_S_PTR,
  315.     f_ex_res_EOF
  316. }
  317. new f_ex_res_ENUM:g_f_ex_res = f_ex_res_OK, g_f_ex_res_descr[128] = "";
  318.     #define f_ex_inc_file_read%2(%0)%3<%1>      for(new File:%1=fopen(%0,io_read),bool:_%1__=true;_%1__;_%1__=(fclose(%1)?false:false))
  319.     #define f_ex_inc_file_write%2(%0)%3<%1>     for(new File:%1=fopen(%0,io_write),bool:_%1__=true;_%1__;_%1__=(fclose(%1)?false:false))
  320.     #define f_ex_inc_file_readwrite%2(%0)%3<%1> for(new File:%1=fopen(%0,io_readwrite),bool:_%1__=true;_%1__;_%1__=(fclose(%1)?false:false))
  321.     #define f_ex_inc_file_append%2(%0)%3<%1>    for(new File:%1=fopen(%0,io_append),bool:_%1__=true;_%1__;_%1__=(fclose(%1)?false:false))
  322.     #define f_ex_inc_temp_file%1<%0>            for(new File:%0=ftemp(),bool:_%0__=true;_%0__;_%0__=(fclose(%0)?false:false))
  323. stock File:f_ex_hook_fopen(const name[], filemode:mode = io_readwrite)
  324. {
  325.     new File:f_ex_debug_var = fopen(name, mode);
  326.     if(f_ex_debug_var)
  327.     {
  328.         g_f_ex_res = f_ex_res_OK;
  329.     #if FILE_EX_DEBUG_LEVEL > 2
  330.         printf(" [DEBUG] <file_ex.inc> fopen() -> Successfully created file stream ( File:%d )", _:f_ex_debug_var);
  331.     #endif
  332.         return f_ex_debug_var;
  333.     }
  334.     format(g_f_ex_res_descr, sizeof(g_f_ex_res_descr), name);
  335.     g_f_ex_res = f_ex_res_INV_FILE_ACCESS;
  336.     #if FILE_EX_DEBUG_LEVEL > 0
  337.     if(mode == io_read) printf("[DEBUG] <file_ex.inc> fopen() -> Failed to open file \"%s\"", name);
  338.     else printf(" [DEBUG] <file_ex.inc> fopen() -> Failed to open or create file \"%s\"", name);
  339.     #endif
  340.     #if defined FILE_EX_ENABLE_REMOTE
  341.     CallRemoteFunction(FILE_EX_RES_CB_NAME_STR, "ddsds", FILE_EX_SCRIPT_ID, _:f_ex_res_INV_FILE_ACCESS, "fopen", 0, name);
  342.     #endif
  343.     return File:0;
  344. }
  345.     #if defined _ALS_fopen
  346.         #undef fopen
  347.     #else
  348.         #define _ALS_fopen
  349.     #endif
  350.     #define fopen   f_ex_hook_fopen
  351. stock bool:f_ex_hook_fclose(File:handle)
  352. {
  353.     if(handle == File:0)
  354.     {
  355.         g_f_ex_res_descr[0] = 0;
  356.         g_f_ex_res = f_ex_res_INV_FILE_S_PTR;
  357.     #if FILE_EX_DEBUG_LEVEL > 0
  358.         print(" [DEBUG] <file_ex.inc> fclose() -> Attempted to destroy a file stream with a NULL pointer ( File:0 )");
  359.     #endif
  360.     #if defined FILE_EX_ENABLE_REMOTE
  361.         CallRemoteFunction(FILE_EX_RES_CB_NAME_STR, "ddsds", FILE_EX_SCRIPT_ID, _:f_ex_res_INV_FILE_S_PTR, "fclose", 0, " ");
  362.     #endif
  363.         return false;
  364.     }
  365.     if(fclose(handle))
  366.     {
  367.         g_f_ex_res = f_ex_res_OK;
  368.         #if FILE_EX_DEBUG_LEVEL > 2
  369.         printf(" [DEBUG] <file_ex.inc> fclose() -> Successfully destroyed file stream ( File:%d )", _:handle);
  370.         #endif
  371.         return true;
  372.     }
  373.     g_f_ex_res_descr[0] = _:handle;
  374.     g_f_ex_res = f_ex_res_INV_FILE_S_PTR;
  375.     #if FILE_EX_DEBUG_LEVEL > 0
  376.     printf(" [DEBUG] <file_ex.inc> fclose() -> Attempted to destroy an invalid file stream ( File:%d )", _:handle);
  377.     #endif
  378.     #if defined FILE_EX_ENABLE_REMOTE
  379.     CallRemoteFunction(FILE_EX_RES_CB_NAME_STR, "ddsds", FILE_EX_SCRIPT_ID, _:f_ex_res_INV_FILE_S_PTR, "fclose", _:handle, " ");
  380.     #endif
  381.     return false;
  382. }
  383.     #if defined _ALS_fclose
  384.         #undef fclose
  385.     #else
  386.         #define _ALS_fclose
  387.     #endif
  388.     #define fclose  f_ex_hook_fclose
  389. stock File:f_ex_hook_ftemp()
  390. {
  391.     new File:f_ex_debug_var = ftemp();
  392.     if(f_ex_debug_var)
  393.     {
  394.         g_f_ex_res = f_ex_res_OK;
  395.     #if FILE_EX_DEBUG_LEVEL > 2
  396.         printf(" [DEBUG] <file_ex.inc> ftemp() -> Successfully created temporary file stream ( File:%d )", _:f_ex_debug_var);
  397.     #endif
  398.         return f_ex_debug_var;
  399.     }
  400.     g_f_ex_res_descr[0] = '\0';
  401.     g_f_ex_res = f_ex_res_INV_TEMP_FILE_ACCESS;
  402.     #if FILE_EX_DEBUG_LEVEL > 0
  403.     print(" [DEBUG] <file_ex.inc> ftemp() -> Failed to create a temporary file stream");
  404.     #endif
  405.     #if defined FILE_EX_ENABLE_REMOTE
  406.     CallRemoteFunction(FILE_EX_RES_CB_NAME_STR, "ddsds", FILE_EX_SCRIPT_ID, _:f_ex_res_INV_TEMP_FILE_ACCESS, "ftemp", 0, "Temporary file");
  407.     #endif
  408.     return File:0;
  409. }
  410.     #if defined _ALS_ftemp
  411.         #undef ftemp
  412.     #else
  413.         #define _ALS_ftemp
  414.     #endif
  415.     #define ftemp   f_ex_hook_ftemp
  416. stock bool:f_ex_hook_fremove(const name[])
  417. {
  418.     if(fremove(name))
  419.     {
  420.         g_f_ex_res = f_ex_res_OK;
  421.     #if FILE_EX_DEBUG_LEVEL > 2
  422.         printf(" [DEBUG] <file_ex.inc> fremove() -> Successfully removed file \"%s\"", name);
  423.     #endif
  424.         return true;
  425.     }
  426.     format(g_f_ex_res_descr, sizeof(g_f_ex_res_descr), name);
  427.     g_f_ex_res = f_ex_res_INV_FILE_ACCESS;
  428.     #if FILE_EX_DEBUG_LEVEL > 0
  429.     printf(" [DEBUG] <file_ex.inc> fremove() -> Failed to remove file \"%s\"", name);
  430.     #endif
  431.     #if defined FILE_EX_ENABLE_REMOTE
  432.     CallRemoteFunction(FILE_EX_RES_CB_NAME_STR, "ddsds", FILE_EX_SCRIPT_ID, _:f_ex_res_INV_FILE_ACCESS, "fremove", 0, name);
  433.     #endif
  434.     return false;
  435. }
  436.     #if defined _ALS_fremove
  437.         #undef fremove
  438.     #else
  439.         #define _ALS_fremove
  440.     #endif
  441.     #define fremove f_ex_hook_fremove
  442. stock f_ex_hook_fwrite(File:handle, const string[])
  443. {
  444.     if(handle)
  445.     {
  446.     #if FILE_EX_DEBUG_LEVEL > 2
  447.         printf(" [DEBUG] <file_ex.inc> fwrite() -> ( File:%d ) %s", _:handle, string);
  448.     #endif
  449.         return fwrite(handle, string);
  450.     }
  451.     g_f_ex_res_descr[0] = 0;
  452.     g_f_ex_res = f_ex_res_INV_FILE_S_PTR;
  453.     #if FILE_EX_DEBUG_LEVEL > 1
  454.     print(" [DEBUG] <file_ex.inc> fwrite() -> Attempted to write into file stream ( File:0 )");
  455.     #endif
  456.     #if defined FILE_EX_ENABLE_REMOTE
  457.     CallRemoteFunction(FILE_EX_RES_CB_NAME_STR, "ddsds", FILE_EX_SCRIPT_ID, _:f_ex_res_INV_FILE_S_PTR, "fwrite", 0, string);
  458.     #endif
  459.     return 0;
  460. }
  461.     #if defined _ALS_fwrite
  462.         #undef fwrite
  463.     #else
  464.         #define _ALS_fwrite
  465.     #endif
  466.     #define fwrite  f_ex_hook_fwrite
  467. stock f_ex_hook_fread(File:handle, string[], size = sizeof string, bool:pack = false)
  468. {
  469.     if(handle == File:0)
  470.     {
  471.         g_f_ex_res_descr[0] = 0;
  472.         g_f_ex_res = f_ex_res_INV_FILE_S_PTR;
  473.     #if FILE_EX_DEBUG_LEVEL > 1
  474.         print(" [DEBUG] <file_ex.inc> fread() -> Attempted to read from file stream ( File:0 )");
  475.     #endif
  476.     #if defined FILE_EX_ENABLE_REMOTE
  477.         CallRemoteFunction(FILE_EX_RES_CB_NAME_STR, "ddsds", FILE_EX_SCRIPT_ID, _:f_ex_res_INV_FILE_S_PTR, "fread", 0, " ");
  478.     #endif
  479.         return 0;
  480.     }
  481.     new f_ex_num_read = fread(handle, string, size, pack);
  482.     #if FILE_EX_DEBUG_LEVEL > 2
  483.         printf(" [DEBUG] <file_ex.inc> fread() -> ( File:%d | Pack = %d) %s", _:handle, _:pack, string);
  484.     #endif
  485.     if(f_ex_num_read == 0)
  486.     {
  487.         g_f_ex_res_descr[0] = _:handle;
  488.         g_f_ex_res = f_ex_res_EOF;
  489.     #if FILE_EX_DEBUG_LEVEL > 1
  490.         printf(" [DEBUG] <file_ex.inc> fread() -> Reached the end of file ( File:%d )", _:handle);
  491.     #endif
  492.     #if defined FILE_EX_ENABLE_REMOTE
  493.         CallRemoteFunction(FILE_EX_RES_CB_NAME_STR, "ddsds", FILE_EX_SCRIPT_ID, _:f_ex_res_EOF, "fread", _:handle, " ");
  494.     #endif
  495.         return 0;
  496.     }
  497.     return f_ex_num_read;
  498. }
  499.     #if defined _ALS_fread
  500.         #undef fread
  501.     #else
  502.         #define _ALS_fread
  503.     #endif
  504.     #define fread   f_ex_hook_fread
  505. stock bool:f_ex_hook_fputchar(File:handle, value, bool:utf8 = true)
  506. {
  507.     if(handle)
  508.     {
  509.     #if FILE_EX_DEBUG_LEVEL > 2
  510.         printf(" [DEBUG] <file_ex.inc> fputchar() -> ( File:%d | UTF8 = %d )  %c", _:handle, _:utf8, value);
  511.     #endif
  512.         return fputchar(handle, value, utf8);
  513.     }
  514.     g_f_ex_res_descr[0] = 0;
  515.     g_f_ex_res = f_ex_res_INV_FILE_S_PTR;
  516.     #if FILE_EX_DEBUG_LEVEL > 1
  517.     print(" [DEBUG] <file_ex.inc> fputchar() -> Attempted to write into file stream ( File:0 )");
  518.     #endif
  519.     #if defined FILE_EX_ENABLE_REMOTE
  520.     new f_ex_str[2] = {'\0', '\0'};
  521.     f_ex_str[0] = value;
  522.     CallRemoteFunction(FILE_EX_RES_CB_NAME_STR, "ddsds", FILE_EX_SCRIPT_ID, _:f_ex_res_INV_FILE_S_PTR, "fputchar", 0, f_ex_str);
  523.     #endif
  524.     return false;
  525. }
  526.     #if defined _ALS_fputchar
  527.         #undef fputchar
  528.     #else
  529.         #define _ALS_fputchar
  530.     #endif
  531.     #define fputchar    f_ex_hook_fputchar
  532.  
  533.     #if defined FILE_EX_FGETCHAR_FIX
  534. stock f_ex_hook_fgetchar(File:handle, bool:utf8 = true)
  535.     #else
  536. stock f_ex_hook_fgetchar(File:handle, value, bool:utf8 = true)
  537.     #endif
  538. {
  539.     if(handle == File:0)
  540.     {
  541.         g_f_ex_res_descr[0] = 0;
  542.         g_f_ex_res = f_ex_res_INV_FILE_S_PTR;
  543.     #if FILE_EX_DEBUG_LEVEL > 1
  544.         print(" [DEBUG] <file_ex.inc> fgetchar() -> Attempted to read from file stream ( File:0 )");
  545.     #endif
  546.     #if defined FILE_EX_ENABLE_REMOTE
  547.         CallRemoteFunction(FILE_EX_RES_CB_NAME_STR, "ddsds", FILE_EX_SCRIPT_ID, _:f_ex_res_INV_FILE_S_PTR, "fgetchar", 0, " ");
  548.     #endif
  549.         return 0;
  550.     }
  551.     #if FILE_EX_DEBUG_LEVEL > 2
  552.     printf(" [DEBUG] <file_ex.inc> fgetchar() -> ( File:%d | UTF8 = %d) %c", _:handle, _:utf8, value);
  553.     #endif
  554.     #if defined FILE_EX_FGETCHAR_FIX
  555.     new f_ex_getchar = fgetchar(handle, 0, utf8);
  556.     #else
  557.     new f_ex_getchar = fgetchar(handle, value, utf8);
  558.     #endif
  559.     if(f_ex_getchar == EOF)
  560.     {
  561.         g_f_ex_res_descr[0] = _:handle;
  562.         g_f_ex_res = f_ex_res_EOF;
  563.     #if FILE_EX_DEBUG_LEVEL > 1
  564.         printf(" [DEBUG] <file_ex.inc> fgetchar() -> Reached the end of file ( File:%d )", _:handle);
  565.     #endif
  566.     #if defined FILE_EX_ENABLE_REMOTE
  567.         CallRemoteFunction(FILE_EX_RES_CB_NAME_STR, "ddsds", FILE_EX_SCRIPT_ID, _:f_ex_res_EOF, "fgetchar", _:handle, " ");
  568.     #endif
  569.         return EOF;
  570.     }
  571.     return f_ex_getchar;
  572. }
  573.     #if defined _ALS_fgetchar
  574.         #undef fgetchar
  575.     #else
  576.         #define _ALS_fgetchar
  577.     #endif
  578.     #define fgetchar    f_ex_hook_fgetchar
  579. stock f_ex_hook_fblockwrite(File:handle, const buffer[], size = sizeof buffer)
  580. {
  581.     if(handle)
  582.     {
  583.     #if FILE_EX_DEBUG_LEVEL > 2
  584.         printf(" [DEBUG] <file_ex.inc> fblockwrite() -> ( File:%d | size = %d)", _:handle, size);
  585.         for(new f_ex_it_i = 0; f_ex_it_i > size; f_ex_it_i++) {printf(" [DEBUG] <file_ex.inc> fblockwrite() -> ( File:%d | Pos = %d | Value = %x | %d )", _:handle, f_ex_it_i, buffer[f_ex_it_i], buffer[f_ex_it_i]);}
  586.     #endif
  587.         return fblockwrite(handle, buffer, size);
  588.     }
  589.     g_f_ex_res_descr[0] = 0;
  590.     g_f_ex_res = f_ex_res_INV_FILE_S_PTR;
  591.     #if FILE_EX_DEBUG_LEVEL > 1
  592.     print(" [DEBUG] <file_ex.inc> fblockwrite() -> Attempted to write to file stream ( File:0 )");
  593.     #endif
  594.     #if defined FILE_EX_ENABLE_REMOTE
  595.     CallRemoteFunction(FILE_EX_RES_CB_NAME_STR, "ddsds", FILE_EX_SCRIPT_ID, _:f_ex_res_INV_FILE_S_PTR, "fblockwrite", 0, " ");
  596.     #endif
  597.     return 0;
  598. }
  599.     #if defined _ALS_fblockwrite
  600.         #undef fblockwrite
  601.     #else
  602.         #define _ALS_fblockwrite
  603.     #endif
  604.     #define fblockwrite f_ex_hook_fblockwrite
  605. stock f_ex_hook_fblockread(File:handle, buffer[], size = sizeof buffer)
  606. {
  607.     if(handle == File:0)
  608.     {
  609.         g_f_ex_res_descr[0] = 0;
  610.         g_f_ex_res = f_ex_res_INV_FILE_S_PTR;
  611.     #if FILE_EX_DEBUG_LEVEL > 1
  612.         print(" [DEBUG] <file_ex.inc> fblockread() -> Attempted to read from file stream ( File:0 )");
  613.     #endif
  614.     #if defined FILE_EX_ENABLE_REMOTE
  615.         CallRemoteFunction(FILE_EX_RES_CB_NAME_STR, "ddsds", FILE_EX_SCRIPT_ID, _:f_ex_res_INV_FILE_S_PTR, "fblockread", 0, " ");
  616.     #endif
  617.         return 0;
  618.     }
  619.     new f_ex_num_cells = fblockread(handle, buffer, size);
  620.     #if FILE_EX_DEBUG_LEVEL > 2
  621.     printf(" [DEBUG] <file_ex.inc> fblockread() -> ( File:%d | size = %d)", _:handle, size);
  622.     for(new f_ex_it_i = 0; f_ex_it_i > size; f_ex_it_i++) {printf(" [DEBUG] <file_ex.inc> fblockread() -> ( File:%d | Pos = %d | Value = %x | %d )", _:handle, f_ex_it_i, buffer[f_ex_it_i], buffer[f_ex_it_i]);}
  623.     #endif
  624.     if(f_ex_num_cells == size)
  625.     {
  626.         g_f_ex_res = f_ex_res_OK;
  627.         return f_ex_num_cells;
  628.     }
  629.     #if FILE_EX_DEBUG_LEVEL > 1
  630.         printf(" [DEBUG] <file_ex.inc> fblockread() -> Reached the end of file ( File:%d )", _:handle);
  631.     #endif
  632.     #if defined FILE_EX_ENABLE_REMOTE
  633.         CallRemoteFunction(FILE_EX_RES_CB_NAME_STR, "ddsds", FILE_EX_SCRIPT_ID, _:f_ex_res_EOF, "fblockread", _:handle, " ");
  634.     #endif
  635.     return f_ex_num_cells;
  636. }
  637.     #if defined _ALS_fblockread
  638.         #undef fblockread
  639.     #else
  640.         #define _ALS_fblockread
  641.     #endif
  642.     #define fblockread  f_ex_hook_fblockread
  643. stock f_ex_hook_fseek(File:handle, position = 0, seek_whence:whence = seek_start)
  644. {
  645.     if(handle == File:0)
  646.     {
  647.         g_f_ex_res_descr[0] = 0;
  648.         g_f_ex_res = f_ex_res_INV_FILE_S_PTR;
  649.     #if FILE_EX_DEBUG_LEVEL > 1
  650.         print(" [DEBUG] <file_ex.inc> fseek() -> Attempted to read from file stream ( File:0 )");
  651.     #endif
  652.     #if defined FILE_EX_ENABLE_REMOTE
  653.         CallRemoteFunction(FILE_EX_RES_CB_NAME_STR, "ddsds", FILE_EX_SCRIPT_ID, _:f_ex_res_INV_FILE_S_PTR, "fseek", 0, " ");
  654.     #endif
  655.         return 0;
  656.     }
  657.     #if FILE_EX_DEBUG_LEVEL > 2
  658.     new f_ex_fseek_ret = fseek(handle, position, whence);
  659.     printf(" [DEBUG] <file_ex.inc> fseek() -> ( File:%d | Move to pos = %d | seek_whence:%d | File pos = %d)", _:handle, position, _:whence, f_ex_fseek_ret);
  660.     return f_ex_fseek_ret;
  661.     #else
  662.     return fseek(handle, position, whence);
  663.     #endif
  664. }
  665.     #if defined _ALS_fseek
  666.         #undef fseek
  667.     #else
  668.         #define _ALS_fseek
  669.     #endif
  670.     #define fseek   f_ex_hook_fseek
  671. stock f_ex_hook_flength(File:handle)
  672. {
  673.     if(handle == File:0)
  674.     {
  675.         g_f_ex_res_descr[0] = 0;
  676.         g_f_ex_res = f_ex_res_INV_FILE_S_PTR;
  677.     #if FILE_EX_DEBUG_LEVEL > 1
  678.         print(" [DEBUG] <file_ex.inc> flength() -> Attempted to read from file stream ( File:0 )");
  679.     #endif
  680.     #if defined FILE_EX_ENABLE_REMOTE
  681.         CallRemoteFunction(FILE_EX_RES_CB_NAME_STR, "ddsds", FILE_EX_SCRIPT_ID, _:f_ex_res_INV_FILE_S_PTR, "flength", 0, " ");
  682.     #endif
  683.         return 0;
  684.     }
  685.     #if FILE_EX_DEBUG_LEVEL > 2
  686.     new f_ex_flength_ret = flength(handle);
  687.     printf(" [DEBUG] <file_ex.inc> flength() -> ( File:%d | Size = %d)", _:handle, f_ex_flength_ret);
  688.     return f_ex_flength_ret;
  689.     #else
  690.     return flength(handle);
  691.     #endif
  692. }
  693.     #if defined _ALS_flength
  694.         #undef flength
  695.     #else
  696.         #define _ALS_flength
  697.     #endif
  698.     #define flength f_ex_hook_flength
  699. stock f_ex_hook_fexist(const pattern[])
  700. {
  701.     if(fexist(pattern))
  702.     {
  703.         g_f_ex_res = f_ex_res_OK;
  704.     #if FILE_EX_DEBUG_LEVEL > 2
  705.         printf(" [DEBUG] <file_ex.inc> fexist() -> File \"%s\" exist", pattern);
  706.     #endif
  707.         return true;
  708.     }
  709.     format(g_f_ex_res_descr, sizeof(g_f_ex_res_descr), pattern);
  710.     g_f_ex_res = f_ex_res_INV_FILE_ACCESS;
  711.     #if FILE_EX_DEBUG_LEVEL > 1
  712.     printf(" [DEBUG] <file_ex.inc> fexist() -> File \"%s\" does not exist", pattern);
  713.     #endif
  714.     #if defined FILE_EX_ENABLE_REMOTE
  715.     CallRemoteFunction(FILE_EX_RES_CB_NAME_STR, "ddsds", FILE_EX_SCRIPT_ID, _:f_ex_res_INV_FILE_ACCESS, "fexist", 0, pattern);
  716.     #endif
  717.     return false;
  718. }
  719.     #if defined _ALS_fexist
  720.         #undef fexist
  721.     #else
  722.         #define _ALS_fexist
  723.     #endif
  724.     #define fexist  f_ex_hook_fexist
  725. stock f_ex_res_ENUM:f_ex_inc_get_result(bool:keep_result = true)
  726. {
  727.     if(keep_result) return g_f_ex_res;
  728.     new f_ex_res_ENUM:ret_g_f_ex_res = g_f_ex_res;
  729.     g_f_ex_res = f_ex_res_OK;
  730.     g_f_ex_res_descr[0] = '\0';
  731.     return ret_g_f_ex_res;
  732. }
  733. stock f_ex_inc_print_result(bool:ignore_ok = true, bool:keep_result = true)
  734. {
  735.     new f_ex_res_ENUM:ret_g_f_ex_res;
  736.     switch(ret_g_f_ex_res = f_ex_inc_get_result(keep_result))
  737.     {
  738.         case f_ex_res_OK:
  739.         {
  740.             if(ignore_ok) return ret_g_f_ex_res;
  741.             print("[FILE EX] No error!");
  742.         }
  743.         case f_ex_res_INV_FILE_ACCESS: printf(" [FILE EX] Invalid file access at \"%s\"", g_f_ex_res_descr);
  744.         case f_ex_res_INV_TEMP_FILE_ACCESS: print(" [FILE EX] Invalid temporary file access");
  745.         case f_ex_res_INV_FILE_S_PTR: printf(" [FILE EX] Invalid file stream pointer ( File:%d )", g_f_ex_res_descr[0]);
  746.         case f_ex_res_EOF: printf(" [FILE EX] End of file ( File:%d ) \"%s\"", g_f_ex_res_descr[0], g_f_ex_res_descr[1]);
  747.     }
  748.     return ret_g_f_ex_res;
  749. }
  750.     #define F_EX_RES::  f_ex_res_
  751. #endif
  752. #define F_EX::      f_ex_inc_
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement