首页 > 代码库 > 读取INI文件插件

读取INI文件插件

GetPrivateProfile.inc

 1 /** 2  * AMXX INI 读取模块 3  * 兵联互创|展鸿 编写 4  * 时间 2015-1-12 5  */ 6  7 enum SUIC_DATA 8 { 9     SUIC_STRING = 1,10     SUIC_INT,11     SUIC_FLOAT,12     SUIC_ARRAY_FLOAT,13     SUIC_ARRAY_INT14 }15 16 /**17  * @说明: 读INI文件18  * @参数: lpApplicationName        --配置节名;19  * @参数: lpKeyName                --配置项名;20  * @参数: lpDefault                --默认值 (读取失败时使用此字符串);21  * @参数: lpFileName            --文件名;22  * @参数: iDataType                --数据类型 (参考SUIC_*的定义);23  * @参数: anyReturnData            --接收数据的变量 (当数据类型为字符串时, 必须追加容器大小参数; 当数据类型为数组时, 数组大小必须为32.);24  * @返回值: 无;25  */26 native GetPrivateProfile(const lpApplicationName[], const lpKeyName[], const lpDefault[], const lpFileName[], SUIC_DATA:iDataType, any:...);

GetPrivateProfile.sma

  1 #include <amxmodx>  2   3 #include "./GetPrivateProfile.inc"  4   5 public plugin_natives()  6 {  7     register_native("GetPrivateProfile", "Native_GetPrivateProfile");  8 }  9  10 public plugin_init() 11 { 12     register_plugin("GetPrivateProfile", "1.1", "SUIC"); 13 } 14  15 public Native_GetPrivateProfile(iPlugin, iParams) 16 { 17     new lpApplicationName[64], lpKeyName[64], lpDefault[128], lpFileName[128]; 18  19     get_string(1, lpApplicationName, charsmax(lpApplicationName)); 20     get_string(2, lpKeyName, charsmax(lpKeyName)); 21     get_string(3, lpDefault, charsmax(lpDefault)); 22     get_string(4, lpFileName, charsmax(lpFileName)); 23  24     new szBuffer[1024], szLeft[1024], szRight[1024], Float:flData[32], iData[32]; 25  26     if(strlen(lpApplicationName) && strlen(lpKeyName) && file_exists(lpFileName)) 27     { 28         new hFile = fopen(lpFileName, "rb"); 29  30         if(hFile) 31         { 32             while(!feof(hFile)) 33             { 34                 fgets(hFile, szBuffer, charsmax(szBuffer)); 35                 trim(szBuffer); 36  37                 if(szBuffer[0] != [) 38                     continue; 39  40                 replace(szBuffer, charsmax(szBuffer), "[", ""); 41                 replace(szBuffer, charsmax(szBuffer), "]", ""); 42  43                 if(!equali(szBuffer, lpApplicationName)) 44                     continue; 45  46                 while(!feof(hFile)) 47                 { 48                     fgets(hFile, szBuffer, charsmax(szBuffer)); 49                     trim(szBuffer); 50  51                     if(!szBuffer[0] || szBuffer[0] == ; || szBuffer[0] == /) 52                         continue; 53  54                     if(szBuffer[0] == [) 55                         break; 56  57                     strtok(szBuffer, szLeft, charsmax(szLeft), szRight, charsmax(szRight), =); 58                     trim(szLeft); 59                     trim(szRight); 60  61                     if(!equali(szLeft, lpKeyName)) 62                         continue; 63  64                     fclose(hFile); 65  66                     switch(get_param(5)) 67                     { 68                         case SUIC_STRING: set_string(6, szRight, get_param(7)); 69                         case SUIC_INT: set_param_byref(6, str_to_num(szRight)); 70                         case SUIC_FLOAT: set_float_byref(6, str_to_float(szRight)); 71                         case SUIC_ARRAY_FLOAT: 72                         { 73                             BreakupStringArray(szRight, flData, sizeof flData, 2); 74                             set_array_f(6, flData, 32); 75                         } 76                         case SUIC_ARRAY_INT: 77                         { 78                             BreakupStringArray(szRight, iData, sizeof iData, 1); 79                             set_array(6, iData, 32); 80                         } 81                     } 82  83                     return true; 84                 } 85                 break; 86             } 87             fclose(hFile); 88         } 89     } 90  91     switch(get_param(5)) 92     { 93         case SUIC_STRING: set_string(6, lpDefault, get_param(7)); 94         case SUIC_INT: set_param_byref(6, str_to_num(lpDefault)); 95         case SUIC_FLOAT: set_float_byref(6, str_to_float(lpDefault)); 96         case SUIC_ARRAY_FLOAT: 97         { 98             BreakupStringArray(lpDefault, flData, sizeof flData, 2); 99             set_array_f(6, flData, 32);100         }101         case SUIC_ARRAY_INT:102         {103             BreakupStringArray(lpDefault, iData, sizeof iData, 1);104             set_array(6, iData, 32);105         }106     }107 108     return false;109 }110 111 stock BreakupStringArray(const szData[], any:flData[], iSize, iDataType)112 {113     new szBuffer[64], szValue[64], iNum;114     copy(szBuffer, charsmax(szBuffer), szData);115     trim(szBuffer);116 117     while(szBuffer[0] && strtok(szBuffer, szValue, charsmax(szValue), szBuffer, charsmax(szBuffer), ,))118     {119         if(iNum >= iSize)120             break;121 122         trim(szValue);123 124         switch(iDataType)125         {126             case 1: flData[iNum++] = str_to_num(szValue);127             case 2: flData[iNum++] = str_to_float(szValue);128         }129     }130 }

 TEST.sma

 1 #include <amxmodx> 2  3 #include "./GetPrivateProfile.inc" 4  5 public plugin_init() 6 { 7     new str[20]; 8     GetPrivateProfile("TEST1", "TEST2", "Error", "TEST.ini", SUIC_STRING, str, charsmax(str)); 9     10     new ii;11     GetPrivateProfile("TEST1", "TEST3", "-1", "TEST.ini", SUIC_INT, ii);12     13     new Float:ff;14     GetPrivateProfile("TEST1", "TEST4", "-1.5", "TEST.ini", SUIC_FLOAT, ff);15     16     new arri[32];17     GetPrivateProfile("TEST1", "TEST5", "-1, -2, -3", "TEST.ini", SUIC_ARRAY_INT, arri);18     19     new Float:arrf[32];20     GetPrivateProfile("TEST1", "TEST6", "-1.5, -2.5, -3.5", "TEST.ini", SUIC_ARRAY_FLOAT, arrf);21     22     server_print("start call");23     server_print("string [%s]", str);24     server_print("int [ %d ]", ii);25     server_print("float [ %f ]", ff);26     server_print("array int [ %d %d %d ]", arri[0], arri[1], arri[2]);27     server_print("array float [ %f %f %f ]", arrf[0], arrf[1], arrf[2]);28     server_print("end");29 }

TEST.ini 文件路径: "cstrike/TEST.ini"

[TEST1]
TEST2=Hello World !!!
TEST3=233
TEST4=233.3
TEST5=4, 5, 6
TEST6=4.2, 5.2, 6.2

 

读取INI文件插件