TMapTextfile v.99/1
Class to handle text files as memory mapped files.
Including efficient methodes for access like sequentiell read
or random access read to text, high performance search routines
and many more.
- {
- ==============================================================================
- MapTextfiles - Release 99/1 14.03.1999
- for Delphi 4, should also run with Delphi 3
- ------------------------------------------------------------------------------
- MapTextfiles is FREEWARE. Freeware means, that you can use this software
- without paying anything for it, but it is not public domain! This software
- is protected through the law of the Federal Republic of Germany, the European
- Union and other countries.
- (C)1999 by Peter Hellinger Software, All rights are reserved.
- Peter Hellinger Software, Zerzabelshofstrasse 41, D-90480 N黵nberg
- email: mail@helli.de
- homepage: http://www.helli.de
- ==============================================================================
- Installation:
- Copy MAPTEXTFILE.PAS into your library path. Use ist. 8-)
- ------------------------------------------------------------------------------
- In Delphi you have many ways to manipulate text files. But all of these have
- a handicap: You must read the whole file (StringList) or you can only access
- data sequential (Assign/Readln).
- The Windows API provides the so called Memory Mapped Files. Windows self
- uses MMFs to load EXE or DLL. Therefore the mechanism is very efficient, but
- simple to handle. The only handicap is, that you must know the size of the
- file before you access it. This means for typical text files, that the
- operation is normally limited to read from the file or manipulate inside.
- The class TMapTextfile wraps the neccesary API calls and povides efficent
- functions for accessing the data.
- TMapTextfiles provides the following properties and methods:
- Methodes:
- =========
- Create Creates an instace of the class
- Destroy Destroys the instance
- Open Opens a file as a memory mapped file.
- filename = name of the file
- mode = open mode:
- mmRead = Open only for read
- mmReadWrite = Open for read and wrie
- Returns INVALID_HANDLE_VALUE if the file not exist.
- If the result > 0 all is OK.
- NOTE:
- 1. The file must exist!
- 2. You cannot write behind the end of the file.
- Close Close the memory mapped file and frees all handles.
- EndOfFile Returns TRUE, if the End of the file is reached.
- GetSize Returns the Size of the file in Bytes
- GetPos Returns the actual file read/write position
- NOTE: Position 0 is the start of the file
- SetPos Sets the actual read/write position
- NOTE: Position 0 is the start of the file
- ReadChar Reads a character from the actual read/write position.
- The r/w position is incremented.
- ReadString Returns a string, starting at the actual r/w position.
- The string is delimited by characters < SPACE, but not
- by ESC (#27) and TAB. The r/w position moves to the
- end of the string, delimiter chararcters are skiped.
- ReadLn Same as ReadSring, but as a Procedure.
- ReadCharAt Reads a charater from an arbitray possition.
- The r/w position is NOT moved!
- pos = position to read from (0 = start of the file!)
- ReadChars Reads a line of charactes from the MMF.
- The r/w position is NOT moved!
- str = The buffer to read to
- pos = position to read from (0 = Start of the file!)
- len = number of characters to read.
- ReadStringAt Returns a string, starting at an arbitray possition.
- The string is delimited by characters < SPACE, but not
- by ESC (#27) and TAB. The r/w position is NOT moved.
- FindString Findes a substring in the MMF and Returns the position.
- str = rhe substring to search for
- pos = position to start the search (0 = start of the file)
- max = position to end the search. If this is 0 or less
- then 0 the end of the file is the limit.
- Returns the position of the substring or -1 if the
- substring is not found.
- FindWildCard Same as Findstring, but supports wildcard search.
- str = the substring to search for
- pos = position to start the search (0 = start of the file)
- max = position to end the search. If this is 0 or less
- then 0 the end of the file is the limit.
- wild = the character used as wildcard (i.e. "*")
- joker = the character used as joker (i.e. "?")
- Returns the position of the substring or -1 if the
- substring is not found.
- ReadBytes Reads a number of bytes to a anonymous variable.
- The r/w position is NOT moved!
- b = the anonymous variable
- pos = position to read from (0 = start of the file)
- len = number of bytes to read.
- WriteBytes Writes a number of bytes to the file.
- NOTE: You can not write behind the end of the file!!!
- b = the anonymous variable
- pos = position to write to (0 = start of the file)
- len = number of bytes to write
- ==============================================================================
- }
- unit uMapTextfile;
- interface
- uses Classes, Windows;
- type
- tMapMode = ( mmRead, mmReadWrite );
- type
- TMapTextfile = class
- private
- f_file : THandle;
- f_MMF : THandle;
- f_size : INTEGER;
- f_view : PByte;
- f_data : PChar;
- f_pos : INTEGER;
- f_open : BOOLEAN;
- function CalcPos( pos : INTEGER ) : PChar;
- public
- constructor Create;
- destructor Destroy; override;
- function Open( const filename : string; mode : tMapMode ) : INTEGER;
- procedure Close;
- function ReadChar : CHAR;
- function ReadString : string;
- procedure ReadLn( var str : string );
- function ReadCharAt( pos : LONGINT ) : CHAR;
- procedure ReadChars( str : PChar; pos, len : LONGINT );
- function ReadStringAt( pos : LONGINT ) : string;
- function GetSize : LONGINT;
- function GetPos : LONGINT;
- procedure SetPos( pos : LONGINT );
- function EndOfFile : BOOLEAN;
- function FindString( const str : string; pos, max : INTEGER ) : INTEGER;
- function FindWildCard( const str : string; pos, max : INTEGER;
- wild, joker : CHAR ) : INTEGER;
- procedure ReadBytes( var b; pos, len : LONGINT );
- procedure WriteBytes( var b; pos, len : LONGINT );
- end;
- implementation
- constructor TMapTextfile.Create;
- begin
- f_open := FALSE;
- end;
- function TMapTextfile.Open( const filename : string; mode : tMapMode )
- : INTEGER;
- var
- m1, m2, m3 : CARDINAL;
- begin
- f_open := FALSE;
- if mode = mmRead then
- begin
- m1 := GENERIC_READ;
- m2 := PAGE_READONLY;
- m3 := FILE_MAP_READ;
- end else begin
- m1 := GENERIC_READ + GENERIC_WRITE;
- m2 := PAGE_READWRITE;
- m3 := FILE_MAP_WRITE;
- end;
- f_file := CreateFile( PChar( filename ), m1, FILE_SHARE_READ, nil,
- OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, );
- if f_file = INVALID_HANDLE_VALUE then
- begin
- Result := INVALID_HANDLE_VALUE;
- EXIT;
- end;
- try
- f_size := GetFileSize( f_file, nil );
- f_MMF := CreateFileMapping( f_file, nil, m2, , f_size, nil );
- if f_MMF = then
- begin
- Result := -;
- EXIT;
- end;
- finally
- CloseHandle( f_file );
- end;
- try
- f_view := MapViewOfFile( f_MMF, m3, , , f_size );
- if f_view = nil then
- begin
- Result := -;
- EXIT;
- end;
- finally
- CloseHandle( f_MMF );
- end;
- f_data := PChar( f_view );
- f_pos := ;
- f_open := TRUE;
- Result := ;
- end;
- destructor TMapTextfile.Destroy;
- begin
- if f_open then
- Close;
- inherited;
- end;
- procedure TMapTextfile.Close;
- begin
- if f_open then
- begin
- UnmapViewOfFile( f_view );
- f_open := FALSE;
- end;
- end;
- function TMapTextfile.CalcPos( pos : INTEGER ) : PChar;
- begin
- Result := nil;
- if f_open then
- begin
- if pos < then
- pos := ;
- if pos > f_size then
- pos := f_size;
- Result := PChar( LONGINT( f_view ) + pos );
- end;
- end;
- function TMapTextfile.ReadChar : CHAR;
- begin
- Result := #;
- if f_open then
- begin
- f_data := PChar( LONGINT( f_view ) + f_pos );
- Result := f_data^;
- INC( f_pos );
- end;
- end;
- function TMapTextfile.ReadString : string;
- begin
- Result := '';
- if f_open then
- begin
- f_data := PChar( LONGINT( f_view ) + f_pos );
- while ( f_pos < f_size ) do
- begin
- case f_data^ of
- # .. # :
- case f_data^ of
- #, # :
- Result := Result + f_data^; // Tab und Escape weiterreichen
- # :
- begin
- INC( f_pos );
- EXIT;
- end;
- # :
- begin // Carriage Return terminiert
- INC( f_pos );
- INC( f_data );
- if f_data^ = # then
- INC( f_pos );
- EXIT;
- end;
- end;
- else
- Result := Result + f_data^;
- end;
- INC( f_pos );
- INC( f_data );
- end;
- end;
- end;
- function TMapTextfile.ReadCharAt( pos : LONGINT ) : CHAR;
- begin
- if f_open then
- Result := CalcPos( pos )^
- else
- Result := #;
- end;
- procedure TMapTextfile.ReadChars( str : PChar; pos, len : LONGINT );
- var
- i : INTEGER;
- p : PChar;
- begin
- if f_open then
- begin
- if len <= then
- EXIT;
- i := ;
- p := CalcPos( pos );
- while ( i <= f_size ) and ( i <= len ) do
- begin
- str^ := p^;
- INC( str );
- INC( p );
- INC( i );
- end;
- end;
- end;
- procedure TMapTextfile.ReadBytes( var b; pos, len : LONGINT );
- var
- p : PChar;
- begin
- if f_open then
- begin
- p := CalcPos( pos );
- Move( p^, b, len );
- end;
- end;
- procedure TMapTextfile.WriteBytes( var b; pos, len : LONGINT );
- var
- p : PChar;
- begin
- if f_open then
- begin
- p := CalcPos( pos );
- Move( b, p^, len );
- end;
- end;
- function TMapTextfile.ReadStringAt( pos : LONGINT ) : string;
- var
- i : INTEGER;
- p : PChar;
- begin
- Result := '';
- if f_open then
- begin
- p := CalcPos( pos );
- i := ;
- while ( i <= f_size ) do
- begin
- case p^ of
- # .. # :
- case p^ of
- #, # :
- Result := Result + p^; // Tabs und Escape weiterreichen
- #, # :
- EXIT; // Linefeed and Carriage Return terminiert
- end;
- else
- Result := Result + p^;
- end;
- INC( p );
- end;
- end;
- end;
- procedure TMapTextfile.ReadLn( var str : string );
- begin
- str := ReadString;
- end;
- function TMapTextfile.GetSize : LONGINT;
- begin
- if f_open then
- Result := f_size
- else
- Result := -;
- end;
- function TMapTextfile.GetPos : LONGINT;
- begin
- if f_open then
- Result := f_pos
- else
- Result := -;
- end;
- procedure TMapTextfile.SetPos( pos : LONGINT );
- begin
- if f_open then
- begin
- if pos < then
- pos := ;
- if pos > f_size then
- pos := f_size;
- f_pos := pos;
- end;
- end;
- function TMapTextfile.EndOfFile : BOOLEAN;
- begin
- if f_open then
- Result := f_pos >= f_size
- else
- Result := TRUE;
- end;
- function TMapTextfile.FindString( const str : string; pos, max : INTEGER )
- : INTEGER;
- var
- s, l1, j : INTEGER;
- p, x : PChar;
- begin
- Result := -;
- if f_open then
- begin
- if max <= then
- max := f_size;
- if pos < then
- pos := f_pos;
- if pos > max then
- EXIT;
- x := PChar( str );
- p := PChar( f_view );
- l1 := ;
- while ( x[ l1 ] > # ) do
- INC( l1 );
- if ( l1 > ) then
- begin
- s := pos;
- repeat (* 1 *)
- j := ;
- while ( s + j < max ) and ( j < l1 ) and ( x[ j ] = p[ s + j ] ) do
- begin
- INC( j );
- if ( j = l1 ) then
- begin
- Result := s;
- EXIT;
- end;
- end;
- INC( s );
- until s >= f_size;
- end;
- end;
- end;
- function TMapTextfile.FindWildCard( const str : string; pos, max : INTEGER;
- wild, joker : CHAR ) : INTEGER;
- var
- s, l1, j : INTEGER;
- p, x : PChar;
- begin
- Result := -;
- if f_open then
- begin
- if max <= then
- max := f_size;
- if pos < then
- pos := f_pos;
- if pos > max then
- EXIT;
- x := PChar( str );
- p := PChar( f_view );
- l1 := ;
- while ( x[ l1 ] > # ) do
- INC( l1 );
- if ( l1 > ) then
- begin
- s := pos;
- repeat (* 1 *)
- j := ;
- while ( s + j < max ) and ( j < l1 ) and
- ( ( x[ j ] = p[ s + j ] ) or ( x[ j ] = joker ) ) do
- begin
- INC( j );
- if ( x[ j ] = wild ) or ( j >= l1 ) then
- begin
- Result := s;
- EXIT;
- end;
- end;
- INC( s );
- until s >= f_size;
- end;
- end;
- end;
- end.
TMapTextfile v.99/1的更多相关文章
- 关于更新发布CSS和JS文件的缓存问题
现如今,浏览器大战下,各个浏览器也是拼了命的提高性能,升级JS解析引擎,更好的处理浏览器的页面缓存,让用户的浏览体验更快,占用更小的PC资源.那么,问题就出现在JS和CSS缓存,甚至页面缓存上.至于浏 ...
- java基础学习总结-接口
原文链接:http://www.cnblogs.com/xdp-gacl/p/3651121.html 一.接口的概念 JAVA是只支持单继承的,但现实之中存在多重继承这种现象,如"金丝猴是 ...
- aes加密C语言
/** * \file aes.h * * \brief AES block cipher * * Copyright (C) 2006-2010, Brainspark B.V. * * This ...
- CentOS_7 OpenWrt Eclipse 环境搭建与 Dr.com 开发笔记
一:内核的编译. 1,linux 编译环境的搭建与源码的准备工作 2,常用软件的安装 (make menuconfig) 3, 针对TP-Link WR740N 一些软件的openwrt 的移植 4 ...
- 转:小白编译openwrt固件教程
原文地址 编译openwrt固件并没有想象的那么复杂,我也是个小白,以下内容是我将网络上的编译教程稍微进行了一下整合.因为我发现很多编译教程没有说明如何更改flash相关配置. 安装ubuntu, ...
- Openwrt Image Builder/SDK 初探
image builder和SDK既可以从官网上下载,又可以自己进行编译(make menuconfig).官网上下载的是预先帮你编译好的,这样可以大量节省自己编译源码花的时间,这两个东西相当于半成品 ...
- 搭建自己的OpenWrt开发环境
1. 安装环境Linux系统,如果在CentOS上操作,需安装如下依赖包:yum install binutils bzip2 gawk gcc gcc-c++ gettext makencurse ...
- js文件被浏览器缓存的思考
我们的用户量大,修改js文件后,用户反馈登录出现问题.实际上刷新一下就没事了.就是因为用户的浏览器使用的还是本地缓存的js代码. 强制刷新一般就会重新去服务器获取新的js代码.但不能让用户 ...
- Openwrt 初探
最近想研究一下Openwrt,于是开始搭建openwrt环境,虽然现在没有现成的板子,但是 可以先编译起来. openwrt的特点是基于下载 -> patch -> 编译 的一个工作模式, ...
随机推荐
- UVA题解三
UVA题解三 UVA 127 题目描述:\(52\)张扑克牌排成一列,如果一张牌的花色或者数字与左边第一列的最上面的牌相同,则将这张牌移到左边第一列的最上面,如果一张牌的花色或者数字与左边第三列的最上 ...
- Java Web 远程调试
Java Web 远程 调试 Tomcat 下载压缩版服务器 环境:Tomcat.Eclipse,做远程调试我们并不需要其他特殊插件 1.配置Tomcat/bin/startup.bat 在前面增加代 ...
- selenium grid应用1-多浏览器执行用例
driver =webdriver.Remote(command_executor=’http://127.0.0.1:4444/wd/hub’, desired_capabilities=Desir ...
- (MHA+MYSQL-5.7增强半同步)高可用架构设计与实现
架构使用mysql5.7版本基于GTD增强半同步并行复制配置 reploication 一主两从,使用MHA套件管理整个复制架构,实现故障自动切换高可用 优势: ...
- csu 1548(三分)
1548: Design road Time Limit: 2 Sec Memory Limit: 256 MBSubmit: 383 Solved: 200[Submit][Status][We ...
- 实现celery中出现拥挤队列时,及时发邮件通知
里面有几个常用的功能,以后值得借鉴. 如获取脚本目录,IP,获取shell返回值,发送邮件等.. 上午写完,中午测试,下午上线~~ #!/usr/bin/env python # -*- coding ...
- Zookeeper(二)Zookeeper原理与API应用
一 Zookeeper概述 1.1 概述 Zookeeper是Google的Chubby一个开源的实现.它是一个针对大型分布式系统的可靠协调系统,提供的功能包括:配置维护.名字服务. 分布式同步.组服 ...
- int类中的方法(二)
25.__pos__(self,*args,**kwargs) def __pos__(self, *args, **kwargs): # real signature unknown &qu ...
- 如何让EasyUI弹出层跳出框架
这个的解决方法其实挺简单的. 只要在最外面的框架页面加个div,然后用parent.div的id就可以的.但是必须得弹出框得是一个页面. <div id="div_info" ...
- 获取token
获取token 提示:openstack 这个是获取N版的方法 ,主要区别在于这个路径上(http://192.168.0.228:35357/v3/auth/tokens ),以前版本可能会是v2 ...