High Performance Solution Ini File Class with MMF

Environment: VC6, WinNT/2k/XP (Having been tested in English/Japanese Win2k/XP)

Special Note: This Unicode Version is full-fledged, while the ANSI version does not support the section header map

Key Technology Used: Memory Map File (MMF), Win32 Memory Management API, Unicode, Data Structure

Applicable Article Category in CodeGuru: Miscellaneous Data Technique

Summary

All is from my previous project, which saves the data of the coordinate, text contents, color, font name, and size information of nearly 30000 dialog controls (listbox, static, edit, and so on) distributed on more than 100 windows. So, the total size of these controls will be at least 10*n Mega bytes and it's not suitable to use the Windows Registry. You see, using a native Ini file Win32 API is terribly impractical because the performance is too low. So, I designed and implemented the following Ini class using MMF; with the pre-allocated buffer and section header map technology, it should be the ultimate solution to the 5-50 Mega byte level Ini File reading and writing situations.

Implementation Description

Pre-allocated buffer

Please refer the following figure:



Click here for a larger image.

When I load the Ini File from the disk to the MMF, depending on the case, I allocate a bigger MMF than the original disk file. When the user just reads the Ini MMF, the Pre-Buffer (see the red bar part) will not be touched. Say, if the user writes more data into the Ini MMF, I can expand the Ini MMF into the Pre-Buffer part. Only when the Pre-Buffer is nearly used up (I call it a Critical Point, see the slim green line), I unload the whole MMF to flush the disk, and load it again with a larger MMF. When measuring carefully in your case, you can get a fairly good performance upgrade with this extra buffer.

Section head position map

When loading an Ini File from the disk file, I make a map of Section Head Pointer and the Section Name. This will take time, but later, when the user searches a section in the end part of the Ini MMF file, it will save considerable time. In my Ini File implementation, I used the CMap MFC class, so this class needs MFC library support. Because my previos project is an MFC-based application, it is reasonable, but to be more universal, I plan to implement it again later with STL.

Special Note with Unicode Ini File:

It is very important to know that Unicode text files have two formats in the Windows OS. And what makes things worse is that Microsoft changed its behavior when it issued Win2000 SP1. In the original Windows 2000, if you open an ANSI text file with Notepad and save it as Unicode text, it just calls the MultiByteToWideChar WinAPI to make changes and save the text; that's it. After you install Win2k SP1 and do the same thing, this time, Notepad adds heading 0xFF FE to the Unicode text. This lack of congruity belabored me a whole day because some member's machine in my team did not upgrade with Win2k SP. (BTW, some specification on the Internet says that in Mac machines the heading will be 0xFE FF.) My Ini File class will check the heading and cope with it.

Usage:

I just want to kiss it (keep it simple, stupid). Unlike some previous Ini File classes on this site, I use just two functions that have the same signature of the Win32 API—GetPrivateProfileString and WritePrivateProfileString to wrap my Ini File class. For example, instead of adding a RemoveSection function, you pass NULL to lpAppName in JXWritePrivateProfileString and you get it. When you want to get all key names in a given section, pass NULL to lpKeyName and get a double NULL ending buffer in lpReturnString. (I include a full-fledged demo project to show this.)

DWORD JXGetPrivateProfileString(
  LPCTSTR lpAppName,          // section name
  LPCTSTR lpKeyName,          // key name
  LPCTSTR lpDefault,          // default string
  LPTSTR lpReturnedString,    // destination buffer
  DWORD nSize,                // size of destination buffer
  LPCTSTR lpFileName          // initialization file name
) ; 

BOOL JXWritePrivateProfileString(
LPCTSTR lpAppName,            // section name
LPCTSTR lpKeyName,            // key name
LPCTSTR lpString,             // string to add
LPCTSTR lpFileName            // initialization file
);

Both support ANSI and Unicode versions. Just include JXIniFile.h in your file and add both the JXIniFile.h and .cpp to your project. Check MSDN to get the detailed information because they are the same signature as the Win32 API.

There's just one thing I decided to be different from the Win32 API. When you want to get all the section or key names, and you pass a buffer to JXGetPrivateProfileString, a return value of 0 means your buffer is too small, while the Win32 API will return the buffer size needed. So, when you get the return of 0, just reallocate your buffer and try it until you pass. I did this because the program usually knows the rough size it takes to get the list of section/key names, and a mid-way return 0 in my Ini File class saves time.

Another thing is that I permit the user to unload the Ini MMF at any time by calling

BOOL JXUnloadPrivateProfile();

One possible use of it is that when you debug the program, you can unload the MMF, use a text editor to change some value, and ask your program read the Ini disk file again to see the effect.

Demo

I provide a simple, dialog-based program to show the usage of these two functions. The code to enumerate sections and keys is used to read a Ini file; usually, the program has knowledge of the section name and key name. I enumerate all the section and key names before fetching the value, just to give a worst-case example.

You press the buttons from left to right, generating 100 (by default) sections, each including 100 keys, the value is randomly generated; then, writing the data to disk, reading it back to the right side tree to confirm. The popped-up time message box tells you the total time to read the Ini File and fill the tree. (You can comment the tree-filling code and see the time of reading exclusively.)

Are You Ready To Use It?

  1. If your application does not use MFC, you have to link with the MFC library because I use CMap to implement the section head map. I am considering implementing it again with STL to make it more universal.
  2. If your application is not Unicode based, by default you will use its ANSI version. In that case, no section header map support is available. (Just because I have no time and need to use it.) It means the performance is not as good as its Unicode counterpart, but still better than the Win32 API (at least >> 10 times faster). Besides, you can still explicitly call JXGetPrivateProfileStringW and JXWritePrivateProfileStringW.
  3. Consider your data structure. Instead of using 10 sections each including 1000 values, 100 sections with 100 values will be better.
  4. Decide your pre-buffer size. I use 1M by default; maybe it is overkill in your case. At least it should keep MMF from reloading in a few seconds in most cases. Actually, if the program just reads the Ini data, you can use 4K (1 memory page).

Acknowledgements

I would like to express my thanks to the following persons and institute: Prof. Moriya Shinji, Human Interaction Lab of Tokyo Denki University, Media Center of Tokyo Denki University, for their academic direction, warm-hearted help, and continuous encouragements to my career life.

Also thanks go to FujiSoftware ABC, Ltd., for the developer position offered, plus their wage.

Downloads

Download demo project - 54 Kb (JXIniFile source file includes <about 4000 lines of code to deal characters one by one>, + exe)
Download Demo Exe File Only - 44 Kb (Only exe demo, MFC dynamic linked)

Version History

Version Release Date Features
1.0+ Nov. 5, 2002 (Add a demo, finish this article)
1.0 Sept. 27, 2001 Heading 0xFF FE handling


Comments

  • cheap wholesale jerseys,cheap new york giants jerseys,cheap custom hockey jerseys

    Posted by claicyroott on 06/21/2013 06:06am

    like it or not jbfbgjh this country was founded on the words that are said in the AMERICAN anthem and the AMERICAN pledge of allegiance. Yes the men and women who have fought for our country did fight for the freedom you have, but DO NOT TAKE THAT FOR GRANTED! Next time you hear the national anthem or the pledge of allegiance maybe you should think of the people who gave it all for them words. Freedom is a privilege. No Obama did not break any laws by not putting his hand over his heart, however it shows a lack of respect for them words and those who have fought and or died for them. I believe it also shows a lack of respect for the history of this great nation. P. S. Maybe you should go back to high school until you learn how to spell you own name. February 27, 2012 at 7:39 am 414 keyonna says: Amen hannah, I agree with you on all that you said. So it goes to show that obvously, some ppl just don't like obama completely. For many reasons. ppl place this man for the former president bush mistakes instead of seeing that obama has done waaay better than bush. ppl are accussing this man for being a so called muslim rather than a christian. and he never said he was a muslim its the republicans saying this stuff about him so that ppl can disvote obama. Well another thing is coming and OBAMA WILL BE OUR PRESIDENT AGAIN FOR ANOTHER 4 MORE YEARS. weather american ppl like it or [url=http://iloveshirt.webs.com]cheap soccer jerseys online[/url] not for those who are against obama change has come and I will not go back to the way things use to be sorry. And hannah once again ur correct HE IS HUMAN AND WILL MAKE MISTAKES 8230 so we can't beat him up for his wrong. everyone makes mistakes in their life time if [url=http://iloveshirt.webs.com]cheap soccer jerseys wholesale[/url] not then wow 8230 but that doesn't mean the person is perfect cuse no one is. March 12, 2012 at 11:05 am 415 Michael says: The question is this Are you better off today than you were 4 years ago? Mortgage rates are down but can you get one? Gasoline is 4. 00 per gallon vs under 2. 00 per gallon. Are you one of the 16 20% unemployed? A

    Reply
  • Suffering From Erectile Dysfunction? Control It Naturally

    Posted by adoeffifielm on 06/21/2013 04:55am

    Is Erectile Dysfunction Organic Cure Helpful in Gaining Powerful Erections? For more information regarding Viagra and related health concerns, visit [url=http://rxhealth.pw ] is it safe to take viagra with high blood pressure medication [/url] see far more Lovemaking Dysfunction Treatment

    Reply
  • tuppenny soccer jerseys indwelling reorganize up wishes coming this biggest soccer party.

    Posted by Merejensejage on 06/16/2013 01:05am

    For the Saints: cornerback Leigh Torrence, linebacker Nate Bussey, guard Eric Olsen, tight end Tory Humphrey, tight end John Gilmore and defensive end Turk McBride. Also inactive for the 49ers: quarterback Scott Tolzien, receiver Joe Hastings, cornerback Shawntae Spencer, guard Daniel Kilgore, guard Mike Person and nose tackle Ian Williams. Niners tight end Delanie Walker (broken jaw) and Saints receiver Lance Moore (hamstring) will not play. The Saints ruled out Moore on Friday. The 49ers did not officially rule [url=http://worldcupsoccerfacts.info/]2014 world cup jersey[/url] out Walker at that time, but coach Jim Harbaugh had previously said Walker would almost certainly miss the game. The 49ers will have receivers Ted Ginn Jr. and Kyle Williams after both missed late-season games with injuries. Their presence [url=http://worldcupsoccerfacts.info/]brazil world cup jerseys[/url] upgrades the 49ers on special teams as well. Walker's absence makes the 49ers' less dynamic [url=http://worldcupsoccerfacts.info/]http://worldcupsoccerfacts.info/[/url] in their two-tight end personnel packages. Walker was also a solid contributor on special teams. SAN FRANCISCO — The San Francisco 49ers and New Orleans Saints offered no surprises when declaring which players would be inactive for their NFC divisional playoff game Saturday.

    Reply
  • http://www.oakleysunglassesoutc.com/ bkrmgp

    Posted by http://www.oakleysunglassesoutc.com/ Suttonjmm on 03/29/2013 05:45am

    ghd sale,ghd hair straightener,The Sino-Japanese war Yaishan Southern Ocean, Battle of Dadonggou Beiyang Navy hated Japan, coupled with the Tan Yan intends to guide the entire Beiyang Navy from top to bottom the whole army are hostile to the heart of Japan growing accumulation. Even the Northern Navy in the Yellow Sea after three Longwei level cruiser is equipped lunar met the Japanese combined fleet Yoshino destroyer squadron, linte was the first time they ordered the fleet into a state of war, all the artillery all turned to targeting the Japanese The Joint Fleet warships, the scenes between the two sides is extremely hot. Sino-Japanese war since the end of three years, the Beiyang Navy and Japan combined fleet repeatedly encounter on the sea, both sides whenever such conditions must be muzzle first time to target the other shells loaded both natural reaction, there is no need to ask for instructions. Tan Yan also believes that this approach is very good, but Northern Navy combat effectiveness in the end or not a good way to deal with two battleships, the last Northern Navy sea also made corresponding adjustments to avoid this from happening, ghd,hairstraighteneraul.ghd straightener,com/" title="ghd australia"ghd australia also promised linte with Deng Shichang.

    Reply
  • Zhou Li Xiu 场图 时装 å·´ grow and summer 2013 women registered frame Wei Yi Road follicle follicle LV 2013 LV new Subsection

    Posted by woshizifengRWd on 03/25/2013 04:34am

    Rated 棋盘 题为 main spring-summer 2013 a 发布 Zhou 时装 黎 巴 Louis Vuitton registered Wei Yi Road, decorative alignment a diminutive non-为必 exert oneself, primordial issue 开格 not 也离 ??nature contrive a shell helpmeet climbing Wei, lattice portly lattice young, Acts Metropolitan 无刻 无时 easy path LV Affinity of joined appeal 份女 您更. fdfdf dfgdfgdf Zhou Li Xiu 场图 时装 巴 stem from and summer 2013 women registered shell Wei Yi Entr俥 follicle follicle LV 2013 LV recent Subsection Rated 棋盘 题为 effort spring-summer 2013 a 发布 Zhou 时装 黎 巴 Louis Vuitton registered Wei Yi Road, decorative set-up a little non-为必 work, fundamental lass 开格 not 也离 ??nature envision a hull better half climbing Wei, lattice mammoth lattice immature, Acts Metropolitan 无刻 无时 indulgent path LV Friendliness of whole be attractive to 份女 您更 Dior 迪 终于 开秀 behindhand a [b][/b] large Hideyuki anyone expected a lap cover receiver 时装 黎 Tomoe, let someone in on 时尚 站在 triumph again next spring-summer 2013 away 秀 transvestite 迪 Dior. Method of arriving 种穿 stave, major trends 眥漕|磬赅眢礤|镳彐溴|镥疱鋧?now 语言 时尚 锋的 于先 genus satisfactorily starting, 拼接 hypsochromic force shearing policy needlessly child 龄女 weird if 很适 交融 Perfect 传统 noted unborn, become 演变 veil instrumentation west 经典 牌 goods; mold Dior 经典 a 缎面 闪亮 裙 half. Floret 哨 Yayu 极简 Yes, 圈可 point 也可 细节 sum shearing interface charge.

    Reply
  • cheap ugg boots oNzz bUym

    Posted by Mandyicc on 02/13/2013 03:16pm

    rEfo chaussures christian louboutin uNva longchamp tote dBua michael kors purses 3sFkk 0jGub chi hair straightener 2kFms michael kors purses 4qRzb cheap adidas Oklahoma City Thunder Russell Westbrook Revolution 30 Swingman Home Jersey 7sHwe nike air max 90 4uMjq ghd 6uUsw ugg 2bZrd toms sale 5eWik Tory Burch Fuchsia Peeptoe Wedge High Heel CheapChic Tory Burch Purple Shoulderbag CheapTory Burch Black Pump Sandal CheapNew Tory Burch Lady Khaki Handbags CheapTory Burch Miller Black Thong Sandals Cheap 0iAdi hollister paris 4uCja ghd planchas 3oYnq ugg boots

    Reply
  • ugg boots rpgepz http://www.cheapfashionshoesas.com/

    Posted by Mandytkq on 01/27/2013 06:36am

    1wSkz nike shoes cGtt Michael Kors outlet xQrr ugg boots 8iIhp cheap monster beats 3kCfb Cheap nfl jerseys 4eBlu uggs australia 6zKlg burberry bags 6nWlv longchamp bags 4cAri cheap nike air max 8sUer ugg boots sale 0xDmo beats headphones 0nDcm ugg espa?a 7wFnv GHD Australia 5uGqc 8jNlp

    Reply
  • ugg boots dbftcu http://www.cheapfashionshoesas.com/

    Posted by Mandyoia on 01/26/2013 10:44pm

    4rDup cheap nike shoes sNck Michael Kors outlet jDmv ugg boots 9jJsi monster beats 4dZep Cheap nfl jerseys 3yJhw ugg norge 7hAls burberry outlet 9nLjx longchamp 1eFyk cheap nike shoes 6qFzx cheap uggs 2bJpb monster beats 7vAeg ugg 8mEgd GHD Australia 3oXdn 6qSpl

    Reply
  • doudoune moncler sfWuX abercrombie

    Posted by Foewrishrorse on 11/13/2012 06:00pm

    moncler outlet ehVa2W moncler epUu1N abercrombie zzVu8Z abercrombie kjQi3C http://www.frzmdoudounesmagasinn.eu http://www.frnabercromfitchmagasin.com

    Reply
  • cheap beats by dre ibeats

    Posted by Ralclabycer on 11/13/2012 01:46pm

    ygcic wqpcm ugg outlet new york city uggs ugg boots baby etaey xwqxgj High Performance Solution Ini File Class with MMF rrbmtgh louis vuitton handbags gallery louis vuitton bags louis vuitton outlet north carolina llethvw sqqcd beats by dre gaming cheap beats dr dre cheap beats by dre amazon yeemabvc coach outlet camarillo coach outlet online coach handbags promo codes tpkzitja

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

  • The impact of a data loss event can be significant. Real-time data is essential to remaining competitive. Many companies can no longer afford to rely on a truck arriving each day to take backup tapes offsite. For most companies, a cloud backup and recovery solution will eliminate, or significantly reduce, IT resources related to the mundane task of backup and allow your resources to be redeployed to more strategic projects. The cloud - can now be comfortable for you – with 100% recovery from anywhere all …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds