개발메모(형식에 매이지 않는 짤막한 개발 낙서)

글 수 174

Reading/Writing .ini-Style files Help

조회 수 3336 추천 수 0 2005.01.26 11:30:59
허창원 *.19.44.231
Reading/Writing .ini-Style files 라는 Instrument Help 입니다.
사실 Instrument라고 하기는 좀 그렇지만, CVI의 Help에 있는 게 아니라서 여기 올려놓고 참고합니다.


This instrument driver contains functions for storing and accessing hierarchical configuration information using .ini–style files or Windows Registry Keys.

To increase speed, this instrument driver has not been implemented with any multithread–safe locking. National Instruments recommends that you do not share access to the same .ini file among multiple threads. You can have multiple .ini files coexisting with multiple threads, as long has no single .ini file is shared among multiple threads.

.ini–style files have the following format:

[section 1]
tag 1 = "string value 1"
tag 2 = "string value 2"
tag 3 = 53


[section 2]
tag 1 = True
tag 2 = –12.3

where section 1, section 2, tag 1, tag 2, and tag 3 are user–defined names. The number of sections and tags is unlimited.

The Windows Registry has the following format:

Root Key


SubKey 1


Value Name 1     Value Data


SubKey 2


Value Name 2     Value Data

where "SubKey 1", "SubKey 2", "Value Name 1", and "Value Name 2" are user–defined names, but "Root Key" is one of a number of system–defined values.

This instrument driver helps you access, store, and manipulate Sections of configuration information, each containing tag/value pairs. For .ini files, Sections correspond to bracketed entries in the file, where each Section usually groups logically a number of tag=value entries (tag/value pairs).

For Windows Registry branches, contain all configuration information for an application in a branch starting at a user–defined Base Key that descends from one of the system–defined Root Keys. Each SubKey under the Base Key corresponds to a Section, where each value–name/value–data pair under that Key is considered a tag/value pair.

This instrument driver supports the following logical value types:


strings
integers
unsigned integers
doubles
boolean
Strings can be of unlimited size. If strings in a .ini–style file are more than 80 characters in length, they are broken into multiple lines. Each line has a tag suffix, which the following example demonstrates:

[section 1]
tag 1 line 0001 = "This is the first line of a very long "
tag 1 line 0002 = "paragraph. This is the second line."
Unprintable characters are written using hexadecimal escape codes.

For Windows Registry storage, all data is actually string data. Your application is free to interpret the data in any supported format.

For writing this type of configuration information to the system, the general approach is to build up the tag/value pairs in memory and then to write the entire file or Registry branch at once. For reading, the general approach is to read the entire file or Key branch into memory at once and then to interrogate individual tag/value pairs.

To write a file with two sections, each containing two tags, the code without error checking might look similar to the following code:

IniText iniText;
char pathName[MAX_PATHNAME_LEN];
char dirName[MAX_PATHNAME_LEN];

/* set up the pathName for the .ini file */
GetProjectDir (dirName);
MakePathname (dirName, "myconfig.ini", pathName);

/* create object for holding the value/tag pairs */
iniText = Ini_New (TRUE); /* TRUE for automatic sorting */

/* create the in–memory tag/value pairs */
Ini_PutString (iniText, "section 1", "tag 1", "string 1");
Ini_PutString (iniText, "section 1", "tag 2", "string 2");
Ini_PutInt (iniText, "section 2", "tag 1", 53);
Ini_PutBoolean (iniText, "section 2", "tag 2", TRUE);

/* write out the tag/value pairs */
Ini_WriteToFile (iniText, pathName);

/* dispose of the in–memory tag/value pairs */
Ini_Dispose (iniText);

To read the same file, the code without error checking might look similar to the following code:

IniText iniText;
char pathName[MAX_PATHNAME_LEN];
char dirName[MAX_PATHNAME_LEN];
char *str1, *str2;
int intVal, boolVal;

/* set up the pathName for the .ini file */
GetProjectDir (dirName);
MakePathname (dirName, "myconfig.ini", pathName);

/* create object for holding the value/tag pairs */
iniText = Ini_New (TRUE); /* TRUE for automatic sorting */

/* read in the tag/value pairs */
Ini_ReadFromFile (iniText, pathName);

/* create the in–memory tag/value pairs */
Ini_GetStringCopy (iniText, "section 1", "tag 1", &str1);
Ini_GetStringCopy (iniText, "section 1", "tag 2", &str2);
Ini_GetInt (iniText, "section 2", "tag 1", &intVal);
Ini_GetBoolean (iniText, "section 2", "tag 2", &boolVal);

/* dispose of the in–memory tag/value pairs */
Ini_Dispose (iniText);

You gain the following advantages by storing information in this type of standard fashion.

Humans can read, and potentially edit, the files and Keys.
Adding new information to the file does not change its format. This makes backwards compatibility virtually automatic.
Multiple independent modules or programs can use the same file, as long as each module or program uses unique section names.
Storing information in this way is platform–independent, which means there are no byte–packing, alignment, OS–dependence, or endianization issues.
The disadvantage of storing information in this fashion is that binary format has the maximum potential for size and speed optimization.

While it may seem that the section/item organization of this type of storage limits the data to two levels of hierarchy, it is possible to obtain multiple levels of hierarchy by adding a prefix for each level of hierarchy to the section names.

Note  This instrument driver makes extensive use of dynamic memory and therefore runs much slower if the project's debugging level is set to extended. Set the debugging level in the Build Options dialog box, which you can access by selecting Options»Build Options.
List of Articles
번호 제목 글쓴이 날짜 조회 수
34 Control Menu는 table과 tree 컨트롤만 해당되네요 허창원 2005-01-31 2907
33 자주 쓰는 타이머(Timer) 컨트롤 Setting 문장 허창원 2005-01-29 3405
» Reading/Writing .ini-Style files Help 허창원 2005-01-26 3336
31 MakeShortFileName() 함수 쓸만 하군요 file 허창원 2005-01-22 2964
30 CVI 프로그램 내에서 다른 프로그램을 실행하는 예 허창원 2005-01-19 3184
29 테이블의 행을 위로, 혹은 아래로 이동시키는 예 file 허창원 2005-01-19 2963
28 Panel 위치 및 크기 설정값 예 [1] 허창원 2005-01-18 3949
27 그래프의 배경과 선을 흑백모드로 전환하는 예 허창원 2005-01-18 2852
26 컨트롤 배열 사용을 위한 예 허창원 2005-01-17 2982
25 DebugPrintf() 함수도 있습니다 허창원 2005-01-12 3599
24 수신된 문자열을 16진수로 스트링 컨트롤에 출력하는 예 허창원 2005-01-11 3297
23 그래프에서 기준선을 그어주는 함수 예 허창원 2005-01-05 2995
22 Bitmap 이미지를 여러개 load하여 그때 그때 Picture에 출력하기 허창원 2005-01-04 3652
21 여러 라인이 있는 텍스트박스를 초기화할 때 [1] 허창원 2005-01-04 3203
20 Table 컨트롤에 추가하고 갱신하는 함수 예 허창원 2005-01-04 4305
19 Panel의 크기가 변경되었을 때, Text Box도 크기 조절하는 예 허창원 2004-12-30 7625
18 프로그램 종료시에 파일 저장여부, 종료여부 물어보는 예 허창원 2004-12-30 3321
17 현재 날짜,시간 정보를 이용한 기본 데이터 파일명 구성하기 허창원 2004-12-30 3355
16 현재 날짜와 시간을 각각의 스트링 컨트롤에 출력하는 부분 file 허창원 2004-12-30 3613
15 테이블의 전체 셀을 스캔하며 해당 값을 얻는 콜백함수 예 허창원 2004-12-29 3352