hedgewars/uUtils.pas
branchqmlfrontend
changeset 11828 a69124eb7ce7
parent 11633 215b837c16f9
parent 11826 7654e2357934
child 12855 1b2b84315d27
equal deleted inserted replaced
11827:8c71c5a1172f 11828:a69124eb7ce7
    21 unit uUtils;
    21 unit uUtils;
    22 
    22 
    23 interface
    23 interface
    24 uses uTypes, uFloat;
    24 uses uTypes, uFloat;
    25 
    25 
       
    26 // returns s with whitespaces (chars <= #32) removed form both ends
       
    27 function Trim(s: shortstring) : shortstring;
       
    28 
    26 procedure SplitBySpace(var a, b: shortstring);
    29 procedure SplitBySpace(var a, b: shortstring);
    27 procedure SplitByChar(var a, b: shortstring; c: char);
    30 procedure SplitByChar(var a, b: shortstring; c: char);
    28 procedure SplitByCharA(var a, b: ansistring; c: char);
    31 procedure SplitByCharA(var a, b: ansistring; c: char);
       
    32 
       
    33 function ExtractFileDir(s: shortstring) : shortstring;
       
    34 function ExtractFileName(s: shortstring) : shortstring;
    29 
    35 
    30 function  EnumToStr(const en : TGearType) : shortstring; overload;
    36 function  EnumToStr(const en : TGearType) : shortstring; overload;
    31 function  EnumToStr(const en : TVisualGearType) : shortstring; overload;
    37 function  EnumToStr(const en : TVisualGearType) : shortstring; overload;
    32 function  EnumToStr(const en : TSound) : shortstring; overload;
    38 function  EnumToStr(const en : TSound) : shortstring; overload;
    33 function  EnumToStr(const en : TAmmoType) : shortstring; overload;
    39 function  EnumToStr(const en : TAmmoType) : shortstring; overload;
    68 function  CheckNoTeamOrHH: boolean; inline;
    74 function  CheckNoTeamOrHH: boolean; inline;
    69 
    75 
    70 function  GetLaunchX(at: TAmmoType; dir: LongInt; angle: LongInt): LongInt;
    76 function  GetLaunchX(at: TAmmoType; dir: LongInt; angle: LongInt): LongInt;
    71 function  GetLaunchY(at: TAmmoType; angle: LongInt): LongInt;
    77 function  GetLaunchY(at: TAmmoType; angle: LongInt): LongInt;
    72 
    78 
       
    79 function read1stLn(filePath: shortstring): shortstring;
       
    80 function readValueFromINI(key, filePath: shortstring): shortstring;
       
    81 
    73 {$IFNDEF PAS2C}
    82 {$IFNDEF PAS2C}
    74 procedure Write(var f: textfile; s: shortstring);
    83 procedure Write(var f: textfile; s: shortstring);
    75 procedure WriteLn(var f: textfile; s: shortstring);
    84 procedure WriteLn(var f: textfile; s: shortstring);
    76 function StrLength(s: PChar): Longword;
    85 function StrLength(s: PChar): Longword;
    77 procedure SetLengthA(var s: ansistring; len: Longword);
    86 procedure SetLengthA(var s: ansistring; len: Longword);
    93 procedure initModule(isNotPreview: boolean);
   102 procedure initModule(isNotPreview: boolean);
    94 procedure freeModule;
   103 procedure freeModule;
    95 
   104 
    96 
   105 
    97 implementation
   106 implementation
    98 uses {$IFNDEF PAS2C}typinfo, {$ENDIF}Math, uConsts, uVariables, SysUtils, uPhysFSLayer;
   107 uses {$IFNDEF PAS2C}typinfo, {$ENDIF}Math, uConsts, uVariables, uPhysFSLayer, uDebug;
    99 
   108 
   100 {$IFDEF DEBUGFILE}
   109 {$IFDEF DEBUGFILE}
   101 var logFile: PFSFile;
   110 var logFile: PFSFile;
   102 {$IFDEF USE_VIDEO_RECORDING}
   111 {$IFDEF USE_VIDEO_RECORDING}
   103     logMutex: TRTLCriticalSection; // mutex for debug file
   112     logMutex: TRTLCriticalSection; // mutex for debug file
   104 {$ENDIF}
   113 {$ENDIF}
   105 {$ENDIF}
   114 {$ENDIF}
   106 var CharArray: array[0..255] of Char;
   115 var CharArray: array[0..255] of Char;
       
   116 
       
   117 // All leading/tailing characters with ordinal values less than or equal to 32 (a space) are stripped.
       
   118 function Trim(s: shortstring) : shortstring;
       
   119 var len, left, right: integer;
       
   120 begin
       
   121 
       
   122 len:= Length(s);
       
   123 
       
   124 if len = 0 then
       
   125     exit(s);
       
   126 
       
   127 // find first non-whitespace
       
   128 left:= 1;
       
   129 while left <= len do
       
   130     begin
       
   131     if s[left] > #32 then
       
   132         break;
       
   133     inc(left);
       
   134     end;
       
   135 
       
   136 // find last non-whitespace
       
   137 right:= len;
       
   138 while right >= 1 do
       
   139     begin
       
   140     if s[right] > #32 then
       
   141         break;
       
   142     dec(right);
       
   143     end;
       
   144 
       
   145 // string is whitespace only
       
   146 if left > right then
       
   147     exit('');
       
   148 
       
   149 // get string without surrounding whitespace
       
   150 len:= right - left + 1;
       
   151 
       
   152 Trim:= copy(s, left, len);
       
   153 
       
   154 end;
       
   155 
       
   156 function GetLastSlashPos(var s: shortString) : integer;
       
   157 var lslash: integer;
       
   158     c: char;
       
   159 begin
       
   160 
       
   161 // find last slash
       
   162 lslash:= Length(s);
       
   163 while lslash >= 1 do
       
   164     begin
       
   165     c:= s[lslash];
       
   166     if (c = #47) or (c = #92) then
       
   167         break;
       
   168     dec(lslash); end;
       
   169 
       
   170 GetLastSlashPos:= lslash;
       
   171 end;
       
   172 
       
   173 function ExtractFileDir(s: shortstring) : shortstring;
       
   174 var lslash: byte;
       
   175 begin
       
   176 
       
   177 if Length(s) = 0 then
       
   178     exit(s);
       
   179 
       
   180 lslash:= GetLastSlashPos(s);
       
   181 
       
   182 if lslash <= 1 then
       
   183     exit('');
       
   184 
       
   185 s[0]:= char(lslash - 1);
       
   186 
       
   187 ExtractFileDir:= s;
       
   188 end;
       
   189 
       
   190 function ExtractFileName(s: shortstring) : shortstring;
       
   191 var lslash, len: byte;
       
   192 begin
       
   193 
       
   194 len:= Length(s);
       
   195 
       
   196 if len = 0 then
       
   197     exit(s);
       
   198 
       
   199 lslash:= GetLastSlashPos(s);
       
   200 
       
   201 if lslash < 1 then
       
   202     exit(s);
       
   203 
       
   204 if lslash = len then
       
   205     exit('');
       
   206 
       
   207 len:= len - lslash;
       
   208 ExtractFilename:= copy(s, lslash + 1, len);
       
   209 end;
   107 
   210 
   108 procedure SplitBySpace(var a,b: shortstring);
   211 procedure SplitBySpace(var a,b: shortstring);
   109 begin
   212 begin
   110 SplitByChar(a,b,' ');
   213 SplitByChar(a,b,' ');
   111 end;
   214 end;
   350 {$IFDEF DEBUGFILE}
   453 {$IFDEF DEBUGFILE}
   351 
   454 
   352 {$IFDEF USE_VIDEO_RECORDING}
   455 {$IFDEF USE_VIDEO_RECORDING}
   353 EnterCriticalSection(logMutex);
   456 EnterCriticalSection(logMutex);
   354 {$ENDIF}
   457 {$ENDIF}
   355 pfsWriteLn(logFile, inttostr(GameTicks)  + ': ' + s);
   458 if logFile <> nil then
       
   459     pfsWriteLn(logFile, inttostr(GameTicks)  + ': ' + s)
       
   460 else
       
   461     WriteLn(stdout, inttostr(GameTicks)  + ': ' + s);
   356 
   462 
   357 {$IFDEF USE_VIDEO_RECORDING}
   463 {$IFDEF USE_VIDEO_RECORDING}
   358 LeaveCriticalSection(logMutex);
   464 LeaveCriticalSection(logMutex);
   359 {$ENDIF}
   465 {$ENDIF}
   360 
   466 
   506         end;
   612         end;
   507 
   613 
   508     sanitizeCharForLog:= r
   614     sanitizeCharForLog:= r
   509 end;
   615 end;
   510 
   616 
       
   617 function read1stLn(filePath: shortstring): shortstring;
       
   618 var f: pfsFile;
       
   619 begin
       
   620     read1stLn:= '';
       
   621     if pfsExists(filePath) then
       
   622         begin
       
   623         f:= pfsOpenRead(filePath);
       
   624         if (not pfsEOF(f)) and allOK then
       
   625             pfsReadLn(f, read1stLn);
       
   626         pfsClose(f);
       
   627         f:= nil;
       
   628         end;
       
   629 end;
       
   630 
       
   631 function readValueFromINI(key, filePath: shortstring): shortstring;
       
   632 var f: pfsFile;
       
   633 	s: shortstring;
       
   634 	i: LongInt;
       
   635 begin
       
   636     s:= '';
       
   637 	readValueFromINI:= '';
       
   638 
       
   639     if pfsExists(filePath) then
       
   640         begin
       
   641         f:= pfsOpenRead(filePath);
       
   642 
       
   643         while (not pfsEOF(f)) and allOK do
       
   644 			begin pfsReadLn(f, s);
       
   645 			if Length(s) = 0 then
       
   646 				continue;
       
   647 			if s[1] = ';' then
       
   648 				continue;
       
   649 
       
   650 			i:= Pos('=', s);
       
   651 			if Trim(Copy(s, 1, Pred(i))) = key then
       
   652 				begin
       
   653 				Delete(s, 1, i);
       
   654 				readValueFromINI:= s;
       
   655 				end;
       
   656 			end;
       
   657         pfsClose(f);
       
   658         f:= nil;
       
   659         end;
       
   660 end;
       
   661 
   511 procedure initModule(isNotPreview: boolean);
   662 procedure initModule(isNotPreview: boolean);
   512 {$IFDEF DEBUGFILE}
   663 {$IFDEF DEBUGFILE}
   513 var logfileBase: shortstring;
   664 var logfileBase: shortstring;
   514     i: LongInt;
   665     i: LongInt;
   515 {$ENDIF}
   666 {$ENDIF}
   533         logfileBase:= 'preview';
   684         logfileBase:= 'preview';
   534         {$ENDIF}
   685         {$ENDIF}
   535 {$IFDEF USE_VIDEO_RECORDING}
   686 {$IFDEF USE_VIDEO_RECORDING}
   536     InitCriticalSection(logMutex);
   687     InitCriticalSection(logMutex);
   537 {$ENDIF}
   688 {$ENDIF}
       
   689     if not pfsExists('/Logs') then
       
   690         pfsMakeDir('/Logs');
   538     // if log is locked, write to the next one
   691     // if log is locked, write to the next one
   539     i:= 0;
   692     i:= 0;
   540     while(i < 7) do
   693     while(i < 7) do
   541     begin
   694     begin
   542         logFile:= pfsOpenWrite('/Logs/' + logfileBase + inttostr(i) + '.log');
   695         logFile:= pfsOpenWrite('/Logs/' + logfileBase + inttostr(i) + '.log');
   543         if logFile <> nil then
   696         if logFile <> nil then
   544             break;
   697             break;
   545         inc(i)
   698         inc(i)
   546     end;
   699     end;
       
   700 
       
   701     if logFile = nil then
       
   702         WriteLn(stdout, '[WARNING] Could not open log file for writing. Log will be written to stdout!');
   547 {$ENDIF}
   703 {$ENDIF}
   548 
   704 
   549     //mobile stuff
   705     //mobile stuff
   550 {$IFDEF IPHONEOS}
   706 {$IFDEF IPHONEOS}
   551     mobileRecord.PerformRumble:= @AudioServicesPlaySystemSound;
   707     mobileRecord.PerformRumble:= @AudioServicesPlaySystemSound;