//*********************************************************
// Labwindows/cvi8.5 读写 ini 文件
// 改写自 Labwindows 自带的例程
// szlihongtao
// 2010-06-13
//*********************************************************
/*---------------------------------------------------------------------------*/
/* */
/* FILE: ini.c */
/* */
/* This example opens an existing *.ini file and allows you to */
/* display, add, and remove sections and items from the inifile. */
/* */
/* NOTES: The Inifile instrument driver treats empty tag items as if they */
/* are not defined. For example, if the Ini file contains, */
/* [Ports] */
/* COM1 = 9600 */
/* COM2 = */
/* then the COM2 tag will not be read in as a value. */
/* */
/*---------------------------------------------------------------------------*/
#include <utility.h>
#include <userint.h>
#include "inifile.h"
#include "ini.h"
//******************************************************************************
#define MAX_NAME_SIZE 256
//******************************************************************************
static int g_changesMade = 0;
static char g_fileName[MAX_PATHNAME_LEN];
static IniText g_myInifile = 0;
//******************************************************************************
int UpdateUIR (int panelHandle);
//******************************************************************************
int main (int argc, char *argv[])
{
int panelHandle;
if (InitCVIRTE (0, argv, 0) == 0)
return -1;
if ((panelHandle = LoadPanel (0, "ini.uir", PANEL)) < 0)
return -1;
DisplayPanel (panelHandle);
/* 一打开软件,就提醒用户选择某个INI文件 */
FilenameBrowse (panelHandle, PANEL_FILENAME_BROWSE, EVENT_COMMIT, 0, 0, 0);
RunUserInterface ();
/* Write the file back out if the user has made changes */
if ((g_myInifile) // 内存中有ini文件
&& (g_changesMade)) // ini文件有了改变
{
if (ConfirmPopup ("Save Changes",
"Do you want to save changes?"))
Ini_WriteToFile (g_myInifile, g_fileName);
}
if (g_myInifile) // Disposes of the in-memory list of tag/value pairs identified
Ini_Dispose (g_myInifile); /* Destroy the Inifile memory object*/
DiscardPanel (panelHandle);
CloseCVIRTE ();
return 0;
}
//******************************************************************************
/* Allow the user to browse for an INI file, and load it into memory. */
/* 选择ini文件,并且装入内存 */
//******************************************************************************
int CVICALLBACK FilenameBrowse (int panel, int control, int event,
void *callbackData, int eventData1,
int eventData2)
{
char tempFileName[MAX_PATHNAME_LEN];
switch (event)
{
case EVENT_COMMIT:
{
/* Ask user which file to open */
if (FileSelectPopup ("", "*.ini", "*.ini", /* 默认为当前工作目录 */
"只能选择INI文件",
VAL_SELECT_BUTTON, /* 按钮显示为 "select" */
0, /* 0-不严格限制文件路径 */
0, /* 0-不严格限制文件扩展名 */
1, /* 1-允许不选择文件,直接退出 */
1, /* 1-允许创建文件夹,不知道是什么原因,选择0也可以创建文件夹???????? */
tempFileName) /* 文件名 */
!= VAL_EXISTING_FILE_SELECTED)
return 0;
/*
0-------VAL_NO_FILE_SELECTED 没有选择文件
1-------VAL_EXISTING_FILE_SELECTED 还是选择以前的那个文件
2-------VAL_NEW_FILE_SELECTED 选择了一个新的文件
*/
/* Write the currently loaded file, if any, back out */
// 如果刚才打开并且更改了一个ini文件,则提示保存
if ((g_myInifile) // 打开过ini文件吗?
&& (g_changesMade)) // ini文件已经更改过
{
if (ConfirmPopup ("Save Changes",
"Do you want to save changes before opening "
"a new file?"))
Ini_WriteToFile (g_myInifile, g_fileName); // 保存ini文件
}
// 新打开的文件名
strcpy (g_fileName, tempFileName); /* 字符串赋值,完全copy */
/* Destroy the Inifile if it currently exists */
if (g_myInifile) // 从内存中删除ini文件
{
Ini_Dispose (g_myInifile); // Disposes of the in-memory list of tag/value pairs identified
g_myInifile = 0; // 句柄复位
}
g_changesMade = 0; // 表示文件刚刚打开,还没有更改过
/* Create a new Inifile object and read it from a file */
if (!(g_myInifile = Ini_New (0)))
{
MessagePopup("Inifile","Error allocating memory for Inifile");
goto Error;
}
// g_myInifile=0则表明出错
// g_myInifile!=0则表示ini文件的句柄
if (Ini_ReadFromFile (g_myInifile, g_fileName))
{
MessagePopup("Inifile","Error reading Inifile");
goto Error;
}
SetCtrlVal (panel, PANEL_FILENAME, g_fileName);
UpdateUIR (panel); /* 使用当前信息更新界面上的显示 */
SetCtrlAttribute (panel, PANEL_BUTTON_SET, ATTR_DIMMED, 0); // 设置为正常显示
break;
}
}
return 0;
Error:
if (g_myInifile);
Ini_Dispose (g_myInifile); // 从内存中删除ini文件
return 0;
}
//******************************************************************************
/* Update the UIR with the current information. */
/* 使用当前信息更新界面上的显示 */
//******************************************************************************
int UpdateUIR (int panelHandle)
{
int sections = 0;
int items = 0;
int section = 0;
int item = 0;
int booleanValue;
int integerValue;
char *sectionName = NULL;
char *itemName = NULL;
char *stringValue;
double doubleValue;
if (g_myInifile)
{
/* Set total section information */
sections = Ini_NumberOfSections (g_myInifile); /* 文件中的总节数 */
SetCtrlVal (panelHandle, PANEL_TOTAL_SECTIONS, sections); /* 显示总节数 */
SetCtrlAttribute (panelHandle, PANEL_SECTION, ATTR_MIN_VALUE,
(sections > 0));
/*
设置列表框的最小值
如果是合法的ini文件,sections>=1,则最小值为1
如果是非法的ini文件,例如空白的文件或者.HEX等等文件,sections=0,则最小值为0
*/
SetCtrlAttribute (panelHandle, PANEL_SECTION, ATTR_MAX_VALUE,sections);/* 设置列表框的最大值 */
/* Set sections and item information */
if (sections) /* 至少有1个节 */
{
GetCtrlVal (panelHandle, PANEL_SECTION, §ion); /* 读取节选择 */
Ini_NthSectionName (g_myInifile, section, §ionName);
if (sectionName)
{
SetCtrlVal (panelHandle, PANEL_SECTION_NAME, sectionName); /* 选择节的名称 */
items = Ini_NumberOfItems (g_myInifile, sectionName); /* 读取项目数 */
SetCtrlVal (panelHandle, PANEL_TOTAL_ITEMS, items); /* 显示项目数 */
SetCtrlAttribute (panelHandle, PANEL_ITEM, ATTR_MIN_VALUE,
(items > 0));
/*
设置列表框的最小值
如果是合法的ini文件,items>=1,则最小值为1
如果是非法的ini文件,例如空白的文件或者.HEX等等文件,items=0,则最小值为0
*/
SetCtrlAttribute (panelHandle, PANEL_ITEM, ATTR_MAX_VALUE,
items); /* 设置列表框的最大值 */
if (items) /* 至少有1个项目 */
{
GetCtrlVal (panelHandle, PANEL_ITEM, &item);/* 选择项目 */
Ini_NthItemName (g_myInifile, sectionName, item,
&itemName); /* 项目名字 */
if (itemName) /* 项目名字非空的话 */
{
SetCtrlVal(panelHandle, PANEL_ITEM_NAME, itemName); /* 显示项目名字 */
/* 判定项目值的类型 */
if (Ini_GetBoolean (g_myInifile, sectionName, itemName,
&booleanValue) > 0)
{
SetCtrlVal (panelHandle, PANEL_ITEM_TYPE, 1); /* 显示为 boolen 型 */
SetCtrlAttribute (panelHandle,
PANEL_ITEM_VALUE_STRING,
ATTR_VISIBLE, 0);
SetCtrlAttribute (panelHandle,
PANEL_ITEM_VALUE_NUMERIC,
ATTR_VISIBLE, 1);
SetCtrlAttribute (panelHandle,
PANEL_ITEM_VALUE_NUMERIC,
ATTR_DATA_TYPE, VAL_INTEGER);
SetCtrlAttribute (panelHandle,
PANEL_ITEM_VALUE_NUMERIC,
ATTR_MIN_VALUE, 0);
SetCtrlAttribute (panelHandle,
PANEL_ITEM_VALUE_NUMERIC,
ATTR_MAX_VALUE, 1);
SetCtrlVal (panelHandle, PANEL_ITEM_VALUE_NUMERIC,
booleanValue);
}
else if (Ini_GetInt (g_myInifile, sectionName,
itemName, &integerValue) > 0)
{
SetCtrlVal (panelHandle, PANEL_ITEM_TYPE, 2); /* 显示为 int 型 */
SetCtrlAttribute (panelHandle,
PANEL_ITEM_VALUE_STRING,
ATTR_VISIBLE, 0);
SetCtrlAttribute (panelHandle,
PANEL_ITEM_VALUE_NUMERIC,
ATTR_VISIBLE, 1);
SetCtrlAttribute (panelHandle,
PANEL_ITEM_VALUE_NUMERIC,
ATTR_DATA_TYPE, VAL_INTEGER);
SetCtrlAttribute (panelHandle,
PANEL_ITEM_VALUE_NUMERIC,
ATTR_MIN_VALUE, -2147483647);
SetCtrlAttribute (panelHandle,
PANEL_ITEM_VALUE_NUMERIC,
ATTR_MAX_VALUE, 2147483647);
SetCtrlVal (panelHandle, PANEL_ITEM_VALUE_NUMERIC,
integerValue);
}
else if (Ini_GetDouble (g_myInifile, sectionName,
itemName, &doubleValue) > 0)
{
SetCtrlVal (panelHandle, PANEL_ITEM_TYPE, 3); /* 显示为 double 型 */
SetCtrlAttribute (panelHandle,
PANEL_ITEM_VALUE_STRING,
ATTR_VISIBLE, 0);
SetCtrlAttribute (panelHandle,
PANEL_ITEM_VALUE_NUMERIC,
ATTR_VISIBLE, 1);
SetCtrlAttribute (panelHandle,
PANEL_ITEM_VALUE_NUMERIC,
ATTR_DATA_TYPE, VAL_DOUBLE);
SetCtrlAttribute (panelHandle,
PANEL_ITEM_VALUE_NUMERIC,
ATTR_MIN_VALUE, -HUGE_VAL);
SetCtrlAttribute (panelHandle,
PANEL_ITEM_VALUE_NUMERIC,
ATTR_MAX_VALUE, HUGE_VAL);
SetCtrlVal (panelHandle, PANEL_ITEM_VALUE_NUMERIC,
doubleValue);
}
else if (Ini_GetPointerToRawString (g_myInifile,
sectionName, itemName, &stringValue) > 0)
{
SetCtrlVal (panelHandle, PANEL_ITEM_TYPE, 4); /* 显示为 string 型 */
SetCtrlAttribute (panelHandle,
PANEL_ITEM_VALUE_STRING,
ATTR_VISIBLE, 1);
SetCtrlAttribute (panelHandle,
PANEL_ITEM_VALUE_NUMERIC,
ATTR_VISIBLE, 0);
SetCtrlVal (panelHandle, PANEL_ITEM_VALUE_STRING,
stringValue);
}
else
MessagePopup ("Inifile",
"Error retrieving Inifile value.");
}
}
}
}
}
/* Set Dimming of Controls */
SetCtrlAttribute (panelHandle, PANEL_NEW_SECTION, ATTR_DIMMED,
(!g_myInifile));
SetCtrlAttribute (panelHandle, PANEL_DELETE_SECTION, ATTR_DIMMED,
(!g_myInifile) || (!sections));
SetCtrlAttribute (panelHandle, PANEL_SECTION, ATTR_DIMMED,
(!g_myInifile) || (!sections));
SetCtrlAttribute (panelHandle, PANEL_SECTION_NAME, ATTR_DIMMED,
(!g_myInifile) || (!sections));
SetCtrlAttribute (panelHandle, PANEL_TOTAL_ITEMS, ATTR_DIMMED,
(!g_myInifile) || (!sections));
SetCtrlAttribute (panelHandle, PANEL_NEW_ITEM, ATTR_DIMMED,
(!g_myInifile) || (!sections));
SetCtrlAttribute (panelHandle, PANEL_DELETE_ITEM, ATTR_DIMMED,
(!g_myInifile) || (!sections) || (!items));
SetCtrlAttribute (panelHandle, PANEL_ITEM, ATTR_DIMMED,
(!g_myInifile) || (!sections) || (!items));
SetCtrlAttribute (panelHandle, PANEL_ITEM_NAME, ATTR_DIMMED,
(!g_myInifile) || (!sections) || (!items));
SetCtrlAttribute (panelHandle, PANEL_ITEM_TYPE, ATTR_DIMMED,
(!g_myInifile) || (!sections) || (!items));
SetCtrlAttribute (panelHandle, PANEL_ITEM_VALUE_STRING , ATTR_DIMMED,
(!g_myInifile) || (!sections) || (!items));
SetCtrlAttribute (panelHandle, PANEL_ITEM_VALUE_NUMERIC, ATTR_DIMMED,
(!g_myInifile) || (!sections) || (!items));
SetCtrlAttribute (panelHandle, PANEL_TOTAL_SECTIONS, ATTR_DIMMED,
!g_myInifile);
SetCtrlAttribute (panelHandle, PANEL_TEXTMSG_VALUE, ATTR_DIMMED,
(!g_myInifile) || (!sections) || (!items));
return 0;
}
//******************************************************************************
/* Create a new section in the INI object.增加节 */
//******************************************************************************
int CVICALLBACK NewSection (int panel, int control, int event,
void *callbackData, int eventData1,
int eventData2)
{
int section;
int sections;
int found;
char *sectionName;
char userSectionName[MAX_NAME_SIZE];
switch (event)
{
case EVENT_COMMIT:
{
userSectionName[0] = 0;
if ((GenericMessagePopup ("New Section",
"Please specify name of new section",
"OK", "Cancel", "", userSectionName,
MAX_NAME_SIZE-1, 0,
VAL_GENERIC_POPUP_INPUT_STRING,
VAL_GENERIC_POPUP_BTN1,
VAL_GENERIC_POPUP_BTN2) == VAL_GENERIC_POPUP_BTN1)
&& (userSectionName[0] != 0))
{
if (Ini_SectionExists (g_myInifile, userSectionName)) // 已经有了这个节名
MessagePopup("Inifile","Error, section already exists");
else // 新的节名字
{
/* We have to add a dummy item and then remove to create */
/* a new section */ // 创建新的节
Ini_PutString (g_myInifile, userSectionName, "Dummy_tag_name", "");
Ini_RemoveItem (g_myInifile, userSectionName,"Dummy_tag_name");
g_changesMade = 1;
}
sections = Ini_NumberOfSections (g_myInifile); // 重新得到总的节数
found = 0; // 0表示没有找到这个节
for (section = 1; (section <= sections) && (!found); section++)
{
if ((Ini_NthSectionName (g_myInifile, section, §ionName) >0)
&& (!strcmp(sectionName, userSectionName)))
found = section;
}
if (found) // 非0 表示刚刚输入的节名称已经在ini文件中有了
{
section = found;
sections = Ini_NumberOfSections (g_myInifile); // 总的项目数
SetCtrlAttribute (panel, PANEL_SECTION, ATTR_MAX_VALUE, sections);
SetCtrlVal (panel, PANEL_SECTION, section);
UpdateUIR (panel);
}
}
}
break;
}
return 0;
}
//******************************************************************************
/* Delete a Section from the INI object. 删除节 */
//******************************************************************************
int CVICALLBACK DeleteSection (int panel, int control, int event,
void *callbackData, int eventData1,
int eventData2)
{
char sectionName[MAX_NAME_SIZE];
int result;
switch (event)
{
case EVENT_COMMIT:
{
sectionName[0] = 0;
GetCtrlVal (panel, PANEL_SECTION_NAME, sectionName); // 空白显示
result=Ini_RemoveSection (g_myInifile, sectionName); // 1表示成功,0表示没有找到这个节
if (result==0)
MessagePopup("Inifile","Error removing item from section");
UpdateUIR(panel);
g_changesMade = 1; // 1表明ini文件有变化
break;
}
}
return 0;
}
//******************************************************************************
/* Change which Section is currently displayed. 改变项 */
//******************************************************************************
int CVICALLBACK ChangeCurrentSection (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
if (event == EVENT_COMMIT)
UpdateUIR (panel);
return 0;
}
//******************************************************************************
/* Create a new Item in the INI object. 增加项 */
//******************************************************************************
int CVICALLBACK NewItem (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int item;
int items;
int found;
char *itemName;
char sectionName[MAX_NAME_SIZE];
char userItemName[MAX_NAME_SIZE];
switch (event)
{
case EVENT_COMMIT:
userItemName[0] = 0;
if ((GenericMessagePopup ("New Item",
"Please specify name of new item",
"OK", "Cancel", "", userItemName,
MAX_NAME_SIZE-1, 0,
VAL_GENERIC_POPUP_INPUT_STRING,
VAL_GENERIC_POPUP_BTN1,
VAL_GENERIC_POPUP_BTN2)== VAL_GENERIC_POPUP_BTN1)
&& (userItemName[0] != 0))
{
GetCtrlVal (panel, PANEL_SECTION_NAME, sectionName);
if (Ini_ItemExists (g_myInifile, sectionName, userItemName))
{
MessagePopup ("Inifile", "Error, item already exists");
goto Error;
}
Ini_PutString (g_myInifile, sectionName, userItemName, "");
g_changesMade = 1;
items = Ini_NumberOfItems (g_myInifile, sectionName);
found = 0;
for (item = 1; (item <= items) && (!found); item++)
{
if ((Ini_NthItemName (g_myInifile, sectionName, item,
&itemName)
> 0) && (!strcmp (itemName, userItemName)))
found = item;
}
if (found)
{
item = found;
items = Ini_NumberOfItems (g_myInifile, sectionName);
SetCtrlAttribute (panel, PANEL_ITEM, ATTR_MAX_VALUE,
items);
SetCtrlVal (panel, PANEL_ITEM, item);
UpdateUIR (panel);
}
}
break;
}
Error:
return 0;
}
//******************************************************************************
/* Delete an Item from the INI object. 删除项 */
//******************************************************************************
int CVICALLBACK DeleteItem (int panel, int control, int event,
void *callbackData, int eventData1,
int eventData2)
{
char sectionName[MAX_NAME_SIZE];
char itemName[MAX_NAME_SIZE];
switch (event)
{
case EVENT_COMMIT:
{
sectionName[0] = 0;
itemName[0] = 0;
GetCtrlVal (panel, PANEL_SECTION_NAME, sectionName);
GetCtrlVal (panel, PANEL_ITEM_NAME, itemName);
if (!Ini_RemoveItem (g_myInifile, sectionName, itemName))
MessagePopup("Inifile", "Error removing item from section");
UpdateUIR (panel);
g_changesMade = 1;
break;
}
}
return 0;
}
//******************************************************************************
/* ChangeCurrentItem */
//******************************************************************************
int CVICALLBACK ChangeCurrentItem (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
UpdateUIR (panel);
break;
}
return 0;
}
//******************************************************************************
/* QuitCallback */
//******************************************************************************
int CVICALLBACK QuitCallback (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
QuitUserInterface (0);
break;
}
return 0;
}
//******************************************************************************
//这个函数是自行编写的
//可以修改/增加项目
//******************************************************************************
int CVICALLBACK Callback_set (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
{
g_changesMade = 1;
Ini_PutInt (g_myInifile, "sectt", "PageControl1Index", 56);
//Ini_PutInt (g_myInifile, "sectt", "N2Checked",88);
Ini_PutString (g_myInifile, "sectt", "新增加的项1", "str");
Ini_PutBoolean (g_myInifile, "sectt", "新增加的布尔", 1);
break;
}
default:
break;
}
return 0;
}
//******************************************************************************
//******************************************************************************
//******************************************************************************
文章评论(0条评论)
登录后参与讨论