By Hellinger Software.

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.

  1. {
  2. ==============================================================================
  3. MapTextfiles - Release 99/1 14.03.1999
  4. for Delphi 4, should also run with Delphi 3
  5.  
  6. ------------------------------------------------------------------------------
  7.  
  8. MapTextfiles is FREEWARE. Freeware means, that you can use this software
  9. without paying anything for it, but it is not public domain! This software
  10. is protected through the law of the Federal Republic of Germany, the European
  11. Union and other countries.
  12.  
  13. (C)1999 by Peter Hellinger Software, All rights are reserved.
  14.  
  15. Peter Hellinger Software, Zerzabelshofstrasse 41, D-90480 Nnberg
  16.  
  17. email: mail@helli.de
  18. homepage: http://www.helli.de
  19.  
  20. ==============================================================================
  21.  
  22. Installation:
  23. Copy MAPTEXTFILE.PAS into your library path. Use ist. 8-)
  24.  
  25. ------------------------------------------------------------------------------
  26.  
  27. In Delphi you have many ways to manipulate text files. But all of these have
  28. a handicap: You must read the whole file (StringList) or you can only access
  29. data sequential (Assign/Readln).
  30.  
  31. The Windows API provides the so called Memory Mapped Files. Windows self
  32. uses MMFs to load EXE or DLL. Therefore the mechanism is very efficient, but
  33. simple to handle. The only handicap is, that you must know the size of the
  34. file before you access it. This means for typical text files, that the
  35. operation is normally limited to read from the file or manipulate inside.
  36.  
  37. The class TMapTextfile wraps the neccesary API calls and povides efficent
  38. functions for accessing the data.
  39.  
  40. TMapTextfiles provides the following properties and methods:
  41.  
  42. Methodes:
  43. =========
  44.  
  45. Create Creates an instace of the class
  46.  
  47. Destroy Destroys the instance
  48.  
  49. Open Opens a file as a memory mapped file.
  50.  
  51. filename = name of the file
  52. mode = open mode:
  53. mmRead = Open only for read
  54. mmReadWrite = Open for read and wrie
  55.  
  56. Returns INVALID_HANDLE_VALUE if the file not exist.
  57. If the result > 0 all is OK.
  58.  
  59. NOTE:
  60. 1. The file must exist!
  61. 2. You cannot write behind the end of the file.
  62.  
  63. Close Close the memory mapped file and frees all handles.
  64.  
  65. EndOfFile Returns TRUE, if the End of the file is reached.
  66.  
  67. GetSize Returns the Size of the file in Bytes
  68.  
  69. GetPos Returns the actual file read/write position
  70. NOTE: Position 0 is the start of the file
  71.  
  72. SetPos Sets the actual read/write position
  73. NOTE: Position 0 is the start of the file
  74.  
  75. ReadChar Reads a character from the actual read/write position.
  76. The r/w position is incremented.
  77.  
  78. ReadString Returns a string, starting at the actual r/w position.
  79. The string is delimited by characters < SPACE, but not
  80. by ESC (#27) and TAB. The r/w position moves to the
  81. end of the string, delimiter chararcters are skiped.
  82.  
  83. ReadLn Same as ReadSring, but as a Procedure.
  84.  
  85. ReadCharAt Reads a charater from an arbitray possition.
  86. The r/w position is NOT moved!
  87.  
  88. pos = position to read from (0 = start of the file!)
  89.  
  90. ReadChars Reads a line of charactes from the MMF.
  91. The r/w position is NOT moved!
  92.  
  93. str = The buffer to read to
  94. pos = position to read from (0 = Start of the file!)
  95. len = number of characters to read.
  96.  
  97. ReadStringAt Returns a string, starting at an arbitray possition.
  98. The string is delimited by characters < SPACE, but not
  99. by ESC (#27) and TAB. The r/w position is NOT moved.
  100.  
  101. FindString Findes a substring in the MMF and Returns the position.
  102.  
  103. str = rhe substring to search for
  104. pos = position to start the search (0 = start of the file)
  105. max = position to end the search. If this is 0 or less
  106. then 0 the end of the file is the limit.
  107.  
  108. Returns the position of the substring or -1 if the
  109. substring is not found.
  110.  
  111. FindWildCard Same as Findstring, but supports wildcard search.
  112.  
  113. str = the substring to search for
  114. pos = position to start the search (0 = start of the file)
  115. max = position to end the search. If this is 0 or less
  116. then 0 the end of the file is the limit.
  117. wild = the character used as wildcard (i.e. "*")
  118. joker = the character used as joker (i.e. "?")
  119.  
  120. Returns the position of the substring or -1 if the
  121. substring is not found.
  122.  
  123. ReadBytes Reads a number of bytes to a anonymous variable.
  124. The r/w position is NOT moved!
  125.  
  126. b = the anonymous variable
  127. pos = position to read from (0 = start of the file)
  128. len = number of bytes to read.
  129.  
  130. WriteBytes Writes a number of bytes to the file.
  131. NOTE: You can not write behind the end of the file!!!
  132.  
  133. b = the anonymous variable
  134. pos = position to write to (0 = start of the file)
  135. len = number of bytes to write
  136.  
  137. ==============================================================================
  138. }
  139.  
  140. unit uMapTextfile;
  141.  
  142. interface
  143.  
  144. uses Classes, Windows;
  145.  
  146. type
  147. tMapMode = ( mmRead, mmReadWrite );
  148.  
  149. type
  150. TMapTextfile = class
  151. private
  152. f_file : THandle;
  153. f_MMF : THandle;
  154. f_size : INTEGER;
  155. f_view : PByte;
  156. f_data : PChar;
  157. f_pos : INTEGER;
  158. f_open : BOOLEAN;
  159. function CalcPos( pos : INTEGER ) : PChar;
  160. public
  161. constructor Create;
  162. destructor Destroy; override;
  163. function Open( const filename : string; mode : tMapMode ) : INTEGER;
  164. procedure Close;
  165. function ReadChar : CHAR;
  166. function ReadString : string;
  167. procedure ReadLn( var str : string );
  168. function ReadCharAt( pos : LONGINT ) : CHAR;
  169. procedure ReadChars( str : PChar; pos, len : LONGINT );
  170. function ReadStringAt( pos : LONGINT ) : string;
  171. function GetSize : LONGINT;
  172. function GetPos : LONGINT;
  173. procedure SetPos( pos : LONGINT );
  174. function EndOfFile : BOOLEAN;
  175. function FindString( const str : string; pos, max : INTEGER ) : INTEGER;
  176. function FindWildCard( const str : string; pos, max : INTEGER;
  177. wild, joker : CHAR ) : INTEGER;
  178. procedure ReadBytes( var b; pos, len : LONGINT );
  179. procedure WriteBytes( var b; pos, len : LONGINT );
  180. end;
  181.  
  182. implementation
  183.  
  184. constructor TMapTextfile.Create;
  185. begin
  186. f_open := FALSE;
  187. end;
  188.  
  189. function TMapTextfile.Open( const filename : string; mode : tMapMode )
  190. : INTEGER;
  191. var
  192. m1, m2, m3 : CARDINAL;
  193. begin
  194. f_open := FALSE;
  195. if mode = mmRead then
  196. begin
  197. m1 := GENERIC_READ;
  198. m2 := PAGE_READONLY;
  199. m3 := FILE_MAP_READ;
  200. end else begin
  201. m1 := GENERIC_READ + GENERIC_WRITE;
  202. m2 := PAGE_READWRITE;
  203. m3 := FILE_MAP_WRITE;
  204. end;
  205.  
  206. f_file := CreateFile( PChar( filename ), m1, FILE_SHARE_READ, nil,
  207. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, );
  208.  
  209. if f_file = INVALID_HANDLE_VALUE then
  210. begin
  211. Result := INVALID_HANDLE_VALUE;
  212. EXIT;
  213. end;
  214. try
  215.  
  216. f_size := GetFileSize( f_file, nil );
  217.  
  218. f_MMF := CreateFileMapping( f_file, nil, m2, , f_size, nil );
  219.  
  220. if f_MMF = then
  221. begin
  222. Result := -;
  223. EXIT;
  224. end;
  225. finally
  226.  
  227. CloseHandle( f_file );
  228. end;
  229.  
  230. try
  231. f_view := MapViewOfFile( f_MMF, m3, , , f_size );
  232. if f_view = nil then
  233. begin
  234. Result := -;
  235. EXIT;
  236. end;
  237. finally
  238. CloseHandle( f_MMF );
  239. end;
  240. f_data := PChar( f_view );
  241. f_pos := ;
  242. f_open := TRUE;
  243. Result := ;
  244. end;
  245.  
  246. destructor TMapTextfile.Destroy;
  247. begin
  248. if f_open then
  249. Close;
  250. inherited;
  251. end;
  252.  
  253. procedure TMapTextfile.Close;
  254. begin
  255. if f_open then
  256. begin
  257. UnmapViewOfFile( f_view );
  258. f_open := FALSE;
  259. end;
  260. end;
  261.  
  262. function TMapTextfile.CalcPos( pos : INTEGER ) : PChar;
  263. begin
  264. Result := nil;
  265. if f_open then
  266. begin
  267. if pos < then
  268. pos := ;
  269. if pos > f_size then
  270. pos := f_size;
  271. Result := PChar( LONGINT( f_view ) + pos );
  272. end;
  273. end;
  274.  
  275. function TMapTextfile.ReadChar : CHAR;
  276. begin
  277. Result := #;
  278. if f_open then
  279. begin
  280. f_data := PChar( LONGINT( f_view ) + f_pos );
  281. Result := f_data^;
  282. INC( f_pos );
  283. end;
  284. end;
  285.  
  286. function TMapTextfile.ReadString : string;
  287. begin
  288. Result := '';
  289. if f_open then
  290. begin
  291. f_data := PChar( LONGINT( f_view ) + f_pos );
  292. while ( f_pos < f_size ) do
  293. begin
  294. case f_data^ of
  295. # .. # :
  296. case f_data^ of
  297. #, # :
  298. Result := Result + f_data^; // Tab und Escape weiterreichen
  299. # :
  300. begin
  301. INC( f_pos );
  302. EXIT;
  303. end;
  304. # :
  305. begin // Carriage Return terminiert
  306. INC( f_pos );
  307. INC( f_data );
  308. if f_data^ = # then
  309. INC( f_pos );
  310. EXIT;
  311. end;
  312. end;
  313. else
  314. Result := Result + f_data^;
  315. end;
  316. INC( f_pos );
  317. INC( f_data );
  318. end;
  319. end;
  320. end;
  321.  
  322. function TMapTextfile.ReadCharAt( pos : LONGINT ) : CHAR;
  323. begin
  324. if f_open then
  325. Result := CalcPos( pos )^
  326. else
  327. Result := #;
  328. end;
  329.  
  330. procedure TMapTextfile.ReadChars( str : PChar; pos, len : LONGINT );
  331. var
  332. i : INTEGER;
  333. p : PChar;
  334. begin
  335. if f_open then
  336. begin
  337. if len <= then
  338. EXIT;
  339. i := ;
  340. p := CalcPos( pos );
  341. while ( i <= f_size ) and ( i <= len ) do
  342. begin
  343. str^ := p^;
  344. INC( str );
  345. INC( p );
  346. INC( i );
  347. end;
  348. end;
  349. end;
  350.  
  351. procedure TMapTextfile.ReadBytes( var b; pos, len : LONGINT );
  352. var
  353. p : PChar;
  354. begin
  355. if f_open then
  356. begin
  357. p := CalcPos( pos );
  358. Move( p^, b, len );
  359. end;
  360. end;
  361.  
  362. procedure TMapTextfile.WriteBytes( var b; pos, len : LONGINT );
  363. var
  364. p : PChar;
  365. begin
  366. if f_open then
  367. begin
  368. p := CalcPos( pos );
  369. Move( b, p^, len );
  370. end;
  371. end;
  372.  
  373. function TMapTextfile.ReadStringAt( pos : LONGINT ) : string;
  374. var
  375. i : INTEGER;
  376. p : PChar;
  377. begin
  378. Result := '';
  379. if f_open then
  380. begin
  381. p := CalcPos( pos );
  382. i := ;
  383. while ( i <= f_size ) do
  384. begin
  385. case p^ of
  386. # .. # :
  387. case p^ of
  388. #, # :
  389. Result := Result + p^; // Tabs und Escape weiterreichen
  390. #, # :
  391. EXIT; // Linefeed and Carriage Return terminiert
  392. end;
  393. else
  394. Result := Result + p^;
  395. end;
  396. INC( p );
  397. end;
  398. end;
  399. end;
  400.  
  401. procedure TMapTextfile.ReadLn( var str : string );
  402. begin
  403. str := ReadString;
  404. end;
  405.  
  406. function TMapTextfile.GetSize : LONGINT;
  407. begin
  408. if f_open then
  409. Result := f_size
  410. else
  411. Result := -;
  412. end;
  413.  
  414. function TMapTextfile.GetPos : LONGINT;
  415. begin
  416. if f_open then
  417. Result := f_pos
  418. else
  419. Result := -;
  420. end;
  421.  
  422. procedure TMapTextfile.SetPos( pos : LONGINT );
  423. begin
  424. if f_open then
  425. begin
  426. if pos < then
  427. pos := ;
  428. if pos > f_size then
  429. pos := f_size;
  430. f_pos := pos;
  431. end;
  432. end;
  433.  
  434. function TMapTextfile.EndOfFile : BOOLEAN;
  435. begin
  436. if f_open then
  437. Result := f_pos >= f_size
  438. else
  439. Result := TRUE;
  440. end;
  441.  
  442. function TMapTextfile.FindString( const str : string; pos, max : INTEGER )
  443. : INTEGER;
  444. var
  445. s, l1, j : INTEGER;
  446. p, x : PChar;
  447. begin
  448. Result := -;
  449. if f_open then
  450. begin
  451. if max <= then
  452. max := f_size;
  453. if pos < then
  454. pos := f_pos;
  455. if pos > max then
  456. EXIT;
  457. x := PChar( str );
  458. p := PChar( f_view );
  459. l1 := ;
  460. while ( x[ l1 ] > # ) do
  461. INC( l1 );
  462. if ( l1 > ) then
  463. begin
  464. s := pos;
  465. repeat (* 1 *)
  466. j := ;
  467. while ( s + j < max ) and ( j < l1 ) and ( x[ j ] = p[ s + j ] ) do
  468. begin
  469. INC( j );
  470. if ( j = l1 ) then
  471. begin
  472. Result := s;
  473. EXIT;
  474. end;
  475. end;
  476. INC( s );
  477. until s >= f_size;
  478. end;
  479. end;
  480. end;
  481.  
  482. function TMapTextfile.FindWildCard( const str : string; pos, max : INTEGER;
  483. wild, joker : CHAR ) : INTEGER;
  484. var
  485. s, l1, j : INTEGER;
  486. p, x : PChar;
  487. begin
  488. Result := -;
  489. if f_open then
  490. begin
  491. if max <= then
  492. max := f_size;
  493. if pos < then
  494. pos := f_pos;
  495. if pos > max then
  496. EXIT;
  497.  
  498. x := PChar( str );
  499. p := PChar( f_view );
  500. l1 := ;
  501. while ( x[ l1 ] > # ) do
  502. INC( l1 );
  503.  
  504. if ( l1 > ) then
  505. begin
  506. s := pos;
  507. repeat (* 1 *)
  508. j := ;
  509. while ( s + j < max ) and ( j < l1 ) and
  510. ( ( x[ j ] = p[ s + j ] ) or ( x[ j ] = joker ) ) do
  511. begin
  512. INC( j );
  513. if ( x[ j ] = wild ) or ( j >= l1 ) then
  514. begin
  515. Result := s;
  516. EXIT;
  517. end;
  518. end;
  519. INC( s );
  520. until s >= f_size;
  521. end;
  522. end;
  523. end;
  524.  
  525. end.

TMapTextfile v.99/1的更多相关文章

  1. 关于更新发布CSS和JS文件的缓存问题

    现如今,浏览器大战下,各个浏览器也是拼了命的提高性能,升级JS解析引擎,更好的处理浏览器的页面缓存,让用户的浏览体验更快,占用更小的PC资源.那么,问题就出现在JS和CSS缓存,甚至页面缓存上.至于浏 ...

  2. java基础学习总结-接口

    原文链接:http://www.cnblogs.com/xdp-gacl/p/3651121.html 一.接口的概念 JAVA是只支持单继承的,但现实之中存在多重继承这种现象,如"金丝猴是 ...

  3. aes加密C语言

    /** * \file aes.h * * \brief AES block cipher * * Copyright (C) 2006-2010, Brainspark B.V. * * This ...

  4. CentOS_7 OpenWrt Eclipse 环境搭建与 Dr.com 开发笔记

    一:内核的编译. 1,linux 编译环境的搭建与源码的准备工作 2,常用软件的安装 (make menuconfig) 3,  针对TP-Link WR740N 一些软件的openwrt 的移植 4 ...

  5. 转:小白编译openwrt固件教程

    原文地址 编译openwrt固件并没有想象的那么复杂,我也是个小白,以下内容是我将网络上的编译教程稍微进行了一下整合.因为我发现很多编译教程没有说明如何更改flash相关配置.   安装ubuntu, ...

  6. Openwrt Image Builder/SDK 初探

    image builder和SDK既可以从官网上下载,又可以自己进行编译(make menuconfig).官网上下载的是预先帮你编译好的,这样可以大量节省自己编译源码花的时间,这两个东西相当于半成品 ...

  7. 搭建自己的OpenWrt开发环境

    1.  安装环境Linux系统,如果在CentOS上操作,需安装如下依赖包:yum install binutils bzip2 gawk gcc gcc-c++ gettext makencurse ...

  8. js文件被浏览器缓存的思考

        我们的用户量大,修改js文件后,用户反馈登录出现问题.实际上刷新一下就没事了.就是因为用户的浏览器使用的还是本地缓存的js代码.   强制刷新一般就会重新去服务器获取新的js代码.但不能让用户 ...

  9. Openwrt 初探

    最近想研究一下Openwrt,于是开始搭建openwrt环境,虽然现在没有现成的板子,但是 可以先编译起来. openwrt的特点是基于下载 -> patch -> 编译 的一个工作模式, ...

随机推荐

  1. UVA题解三

    UVA题解三 UVA 127 题目描述:\(52\)张扑克牌排成一列,如果一张牌的花色或者数字与左边第一列的最上面的牌相同,则将这张牌移到左边第一列的最上面,如果一张牌的花色或者数字与左边第三列的最上 ...

  2. Java Web 远程调试

    Java Web 远程 调试 Tomcat 下载压缩版服务器 环境:Tomcat.Eclipse,做远程调试我们并不需要其他特殊插件 1.配置Tomcat/bin/startup.bat 在前面增加代 ...

  3. selenium grid应用1-多浏览器执行用例

    driver =webdriver.Remote(command_executor=’http://127.0.0.1:4444/wd/hub’, desired_capabilities=Desir ...

  4. (MHA+MYSQL-5.7增强半同步)高可用架构设计与实现

           架构使用mysql5.7版本基于GTD增强半同步并行复制配置 reploication 一主两从,使用MHA套件管理整个复制架构,实现故障自动切换高可用        优势:       ...

  5. csu 1548(三分)

    1548: Design road Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: 383  Solved: 200[Submit][Status][We ...

  6. 实现celery中出现拥挤队列时,及时发邮件通知

    里面有几个常用的功能,以后值得借鉴. 如获取脚本目录,IP,获取shell返回值,发送邮件等.. 上午写完,中午测试,下午上线~~ #!/usr/bin/env python # -*- coding ...

  7. Zookeeper(二)Zookeeper原理与API应用

    一 Zookeeper概述 1.1 概述 Zookeeper是Google的Chubby一个开源的实现.它是一个针对大型分布式系统的可靠协调系统,提供的功能包括:配置维护.名字服务. 分布式同步.组服 ...

  8. int类中的方法(二)

        25.__pos__(self,*args,**kwargs) def __pos__(self, *args, **kwargs): # real signature unknown &qu ...

  9. 如何让EasyUI弹出层跳出框架

    这个的解决方法其实挺简单的. 只要在最外面的框架页面加个div,然后用parent.div的id就可以的.但是必须得弹出框得是一个页面. <div id="div_info" ...

  10. 获取token

    获取token 提示:openstack 这个是获取N版的方法 ,主要区别在于这个路径上(http://192.168.0.228:35357/v3/auth/tokens ),以前版本可能会是v2 ...