
{******************************************************************************}
{                                                                              }
{  Este trabajo fue desarrollado en Mexico en Febrero del 2010                 }
{  Puedes usar partes de este codigo siempre y cuando proporciones los         }
{  creditos correspondientes, te agradeceria poner una nota como la siguiente: }
{  "Este software contiene partes escritas por Andres Jasqui"                  }
{                                                                              }
{******************************************************************************}

unit Utils;

interface

Uses Windows, Forms, SysUtils, IniFiles;

type
	TSystemPath = (Desktop, StartMenu,Programs, Startup, Personal, AppData,
		Fonts, SendTo, Recent, Favorites, Cache, Cookies, History, NetHood,
		PrintHood, Templates, LocADat, WindRoot, WindSys, TempPath, RootDir,
		ProgFiles, ComFiles, ConfigPath, DevicePath, MediaPath, WallPaper);
	THash = (MD5, SHA1);

var
	IniFileName : string;

function OpcC(Section,Ident : string) : string;
function OpcD(Section,Ident : string) : double;
function OpcI(Section,Ident : string) : integer;
procedure OpcCw(Section,Ident,value : string);
function RemplazaCad(StrSrc, StrDest, Str : string) : string;
function GetSystemPath(SystemPath: TSystemPath): string;
procedure Delay(msecs : cardinal);

implementation

uses Math, Registry;

function OpcC(Section,Ident : string) : string;
var ini: TIniFile;
begin
	ini := TIniFile.Create(IniFileName);
	Result := ini.ReadString(Section,Ident,'');
	ini.Free;
end;

function OpcD(Section,Ident : string) : double;
var ini: TIniFile;
begin
	ini := TIniFile.Create(IniFileName);
	Result := ini.ReadFloat(Section,Ident,0.0);
	ini.Free;
end;

function OpcI(Section,Ident : string) : integer;
var ini: TIniFile;
begin
	ini := TIniFile.Create(IniFileName);
	Result := ini.ReadInteger(Section,Ident,0);
	ini.Free;
end;

procedure OpcCw(Section,Ident,value : string);
var ini: TIniFile;
begin
	ini := TIniFile.Create(IniFileName);
	ini.WriteString(Section,Ident,value);
	ini.Free;
end;

function RemplazaCad(StrSrc, StrDest, Str : string) : string;
// RemplazaCad('hola','prueba','Esto es una prueba') = 'Esto es una hola'
begin
	while Pos(StrDest,Str) > 0 do
	begin
		Insert(StrSrc,Str,Pos(StrDest,Str));
		Delete(Str,Pos(StrDest,Str),Length(StrDest));
	end;
	RemplazaCad := Str;
end;

function GetSystemPath(SystemPath: TSystemPath): string;
var ph: PChar;
begin
	with TRegistry.Create do
		try
			RootKey := HKEY_CURRENT_USER;
			OpenKey('\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders',True);

			case SystemPath of
				Desktop: Result   := ReadString('Desktop');
				StartMenu: Result := ReadString('Start Menu');
				Programs: Result  := ReadString('Programs');
				Startup: Result   := ReadString('Startup');
				Personal: Result  := ReadString('Personal');
				AppData: Result   := ReadString('AppData');
				Fonts: Result     := ReadString('Fonts');
				SendTo: Result    := ReadString('SendTo');
				Recent: Result    := ReadString('Recent');
				Favorites: Result := ReadString('Favorites');
				Cache: Result     := ReadString('Cache');
				Cookies: Result   := ReadString('Cookies');
				History: Result   := ReadString('History');
				NetHood: Result   := ReadString('NetHood');
				PrintHood: Result := ReadString('PrintHood');
				Templates: Result := ReadString('Templates');
				LocADat: Result   := ReadString('Local AppData');
				WindRoot: begin
					GetMem(ph, 255);
					GetWindowsDirectory(ph, 254);
					Result := Strpas(ph);
					Freemem(ph);
				end;
				WindSys: begin
					GetMem(ph, 255);
					GetSystemDirectory(ph, 254);
					Result := Strpas(ph);
					Freemem(ph);
				end;
				TempPath: begin
					GetMem(ph, 255);
					GetTempPath(254, ph);
					Result := Strpas(ph);
					Freemem(ph);
				end;
				RootDir: begin
					GetMem(ph, 255);
					GetSystemDirectory(ph, 254);
					Result := (Copy(Strpas(ph), 1, 2));
					Freemem(ph);
				end;
			end;

			RootKey := HKEY_LOCAL_MACHINE;
			OpenKey('\SOFTWARE\Microsoft\Windows\CurrentVersion', True);

			case SystemPath of
				ProgFiles: Result := ReadString('ProgramFilesDir');
				ComFiles: Result := ReadString('CommonFilesDir');
				ConfigPath: Result := ReadString('ConfigPath');
				DevicePath: Result := ReadString('DevicePath');
				MediaPath: Result := ReadString('MediaPath');
				WallPaper: Result := ReadString('WallPaperDir');
			end;

		finally
			CloseKey;
			Free;
		end;

	if (Result <> '') and (Result[Length(Result)] <> '\') then
		Result := Result + '\';
end;

procedure Delay(msecs : cardinal);
var FirstTickCount: cardinal;
begin
	FirstTickCount := GetTickCount;
	repeat
		Application.ProcessMessages;
	until ((GetTickCount-FirstTickCount) >= msecs);
end;

end.
