原创 Labwindows/cvi8.5 读 ini 文件

2010-6-13 11:39 5660 11 14 分类: 软件与OS

//*********************************************************
// Labwindows/cvi8.5 读 ini 文件
// szlihongtao
// 2010-06-13
// 参照 王建新的<Labwindows/cvi测试技术及工程应用> page304
// 稍有改写
//*********************************************************
#include <utility.h>
#include <formatio.h>
#include <cvirte.h>  
#include <userint.h>
#include "inifile.h"         
#include "ini.h"
//*********************************************************
static int panelHandle;
static IniText inihandle;
void getitems (void);
int gettype (char *itemname, char *sectionname);
//*********************************************************
int main (int argc, char *argv[])
{
 if (InitCVIRTE (0, argv, 0) == 0)
  return -1; /* out of memory */
 if ((panelHandle = LoadPanel (0, "ini.uir", PANEL)) < 0)
  return -1;
 DisplayPanel (panelHandle);
 RunUserInterface ();
 DiscardPanel (panelHandle);
 return 0;
}
//*********************************************************
// 选择文件
//*********************************************************
int CVICALLBACK FilenameBrowse (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 long filesize;
 char filename[MAX_FILENAME_LEN];
 char pathname[MAX_PATHNAME_LEN];
 int sections;
 char *sectionname;
 int items;
 char *itemname;
 FILE *filestream;
 char *initext;
 int FileHandle;
 
 switch (event)
 {
  case EVENT_COMMIT:
  { 
   if ((FileSelectPopup ("", "*.ini", "*.ini;*.cws;*.prj", "打开", VAL_OK_BUTTON, 0, 0, 0, 0, pathname)) > 0)
   {
    //由于带有路径的完整的文件名经常超出文本框显示范围,设置tooltip属性
    //鼠标停留在此时会显示提示信息
    SetCtrlToolTipAttribute (panelHandle, PANEL_FILENAME, CTRL_TOOLTIP_ATTR_TEXT, pathname);
   
    ResetTextBox (panelHandle, PANEL_TEXTBOX, "");  
    if (GetFileSize (pathname, &filesize) == 0)   // 文件存在吗?
    {   
     // Replaces all text in the specified text box with the string you specify
     // 将文件内容文本框用空格代替
     
     //去除路径名,获得文件名
     SplitPath (pathname, NULL, NULL, filename);
     
     //设置TextBox的Label属性为当前打开文件的文件名
     SetCtrlAttribute (panelHandle, PANEL_TEXTBOX, ATTR_LABEL_TEXT, filename);
//---------------------------------------------------------------------    
     //动态分配内存,大小为文件大小加1,最后一位为结束符“\0”
     initext = malloc (filesize * sizeof (char) +1);


#if 1  
     FileHandle=OpenFile (pathname, VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII);
     ReadFile (FileHandle, initext,filesize);
     
     //在文件的末尾加上结束符“\0”,注意这句话,一定要加结束符,否则 SetCtrlVal 显示出错     
     initext[filesize] = '\0';
     SetCtrlVal (panelHandle, PANEL_TEXTBOX, initext);
     
     CloseFile (FileHandle);  //关闭文件 
#else 
     filestream = fopen (pathname, "r");   //以只读方式打开文件 
     
     //读取文件,将文件内容放入动态分配的内存中
     fread (initext, sizeof (char), filesize, filestream);
     
     //在文件的末尾加上结束符“\0”
     initext[filesize] = '\0';
     SetCtrlVal (panelHandle, PANEL_TEXTBOX, initext);
             
     fclose (filestream);//关闭文件 
#endif  
     free (initext);    // 释放动态分配的内存空间 
//---------------------------------------------------------------------     
    }
    else
     SetCtrlAttribute (panelHandle, PANEL_TEXTBOX, ATTR_LABEL_TEXT, "");
   
    SetCtrlVal (panelHandle, PANEL_FILENAME, pathname);
        
    inihandle = Ini_New (0); //创建一个ini文件句柄    
    Ini_ReadFromFile (inihandle, pathname);  //读取ini文件    
         
    //获得ini文件总节数
    sections = Ini_NumberOfSections (inihandle);
    SetCtrlVal (panelHandle, PANEL_TOTAL_SECTIONS, sections);
    
    //若总节数不为0,则执行以下操作
    if (sections)
    {  
     SetCtrlAttribute (panelHandle, PANEL_SECTION, ATTR_DIMMED, 0);
     SetCtrlAttribute (panelHandle, PANEL_ITEM, ATTR_DIMMED, 0); 
              
     //设置SECTION控件的最大、最小值
     SetCtrlAttribute (panelHandle, PANEL_SECTION, ATTR_MIN_VALUE, 1);
     SetCtrlAttribute (panelHandle, PANEL_SECTION, ATTR_MAX_VALUE, sections); 
       
     if (Ini_NthSectionName (inihandle, 1, &sectionname)) //获得第一节的名称 
     {
      SetCtrlVal (panelHandle, PANEL_SECTION_NAME, sectionname);
       
      items = Ini_NumberOfItems (inihandle, sectionname);//根据节的名称,获得某一节下总的项目数
      SetCtrlVal (panelHandle, PANEL_TOTAL_ITEMS, items);
      
      SetCtrlAttribute (panelHandle, PANEL_ITEM, ATTR_MIN_VALUE, 1);  //设置S项目控件的最大、最小值  
      SetCtrlAttribute (panelHandle, PANEL_ITEM, ATTR_MAX_VALUE, items);
      
      //获得某一节下第一个项目的项目名称
      if (Ini_NthItemName (inihandle, sectionname, 1, &itemname))
       gettype(itemname, sectionname);  //调用自定义类型函数,获得某一项目的数据类型    
     }
    }
    else
    {
     SetCtrlVal (panelHandle, PANEL_SECTION_NAME, "");
     SetCtrlVal (panelHandle, PANEL_ITEM_NAME, "");  
     SetCtrlVal (panelHandle, PANEL_ITEM_VALUE_STRING, "");
     SetCtrlVal (panelHandle, PANEL_TOTAL_ITEMS, 0);
     SetCtrlAttribute (panelHandle, PANEL_SECTION, ATTR_DIMMED, 1);
     SetCtrlAttribute (panelHandle, PANEL_ITEM, ATTR_DIMMED, 1); 
    }  
   }
   break;
  }
 }
 return 0;
}
//*********************************************************
// 更改节
//*********************************************************
int CVICALLBACK ChangeCurrentSection (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 char *itemname;
 char *sectionname;
 int sectionindex;


 switch (event)
 {
  case EVENT_COMMIT:
  {        
   getitems ();   //调用自定义函数,得到项目  
   
   SetCtrlVal (panelHandle, PANEL_ITEM, 1);
   GetCtrlVal (panelHandle, PANEL_SECTION, &sectionindex);
   Ini_NthSectionName (inihandle, sectionindex, &sectionname);
   if (Ini_NthItemName (inihandle, sectionname, 1, &itemname))
    gettype(itemname, sectionname);  //传递itemname和sectionname参数值给自定义函数 
   break;
  } 
 }
 return 0;
}
//*********************************************************
// 更改项
//*********************************************************
int CVICALLBACK ChangeCurrentItem (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 switch (event)
 {
  case EVENT_COMMIT:
   getitems ();
   break;
 }
 return 0;
}
//*********************************************************
int CVICALLBACK panelCB (int panel, int event, void *callbackData,
  int eventData1, int eventData2)
{
 switch (event)
 {
  case EVENT_CLOSE:
   QuitUserInterface (0);
   break;
 }
 return 0;
}
//*********************************************************
void getitems (void)
{
 char pathname[MAX_PATHNAME_LEN];
 int sections;
 char *sectionname;
 int items;
 char *itemname;
 int sectionindex;
 int itemindex;
 
 GetCtrlVal (panelHandle, PANEL_FILENAME, &pathname[0]);
 Ini_ReadFromFile (inihandle, pathname);
 sections = Ini_NumberOfSections (inihandle);
 if (sections)
 {
  GetCtrlVal (panelHandle, PANEL_SECTION, &sectionindex);
  
  if (Ini_NthSectionName (inihandle, sectionindex, &sectionname))
  {
    SetCtrlVal (panelHandle, PANEL_SECTION_NAME, sectionname);
    items = Ini_NumberOfItems (inihandle, sectionname);
    SetCtrlVal (panelHandle, PANEL_TOTAL_ITEMS, items);
    SetCtrlAttribute (panelHandle, PANEL_ITEM, ATTR_MIN_VALUE, 1);
    SetCtrlAttribute (panelHandle, PANEL_ITEM, ATTR_MAX_VALUE, items);
    
    GetCtrlVal (panelHandle, PANEL_ITEM, &itemindex);
    if (Ini_NthItemName (inihandle, sectionname, itemindex, &itemname))
    {
     gettype (itemname, sectionname);
    }
  }
 } 
}
//*********************************************************
// 获得某一项目的数据类型 
// itemname    项的名称
// sectionname 节的名称
//*********************************************************
int gettype (char *itemname, char *sectionname)
{
    int booleanvalue;  
 char stringvalue[260];    
 int integervalue; 
 double doublevalue;  
 char *pointertostring;  


 SetCtrlVal (panelHandle, PANEL_ITEM_NAME, itemname);  // 显示项目的名称
 
 //获得项目值,并判断是否为布尔类型
 if (Ini_GetBoolean (inihandle, sectionname, itemname, &booleanvalue) >0 )
 {
  SetCtrlVal (panelHandle, PANEL_ITEM_TYPE, 1);
  Fmt (stringvalue, "%s<%i", booleanvalue);
  SetCtrlVal (panelHandle, PANEL_ITEM_VALUE_STRING, stringvalue);
  
  SetCtrlToolTipAttribute (panelHandle, PANEL_ITEM_VALUE_STRING, CTRL_TOOLTIP_ATTR_TEXT, "");
 }  
 //获得项目值,并判断是否为整型
 else if (Ini_GetInt (inihandle, sectionname, itemname, &integervalue) >0 )
 {
  SetCtrlVal (panelHandle, PANEL_ITEM_TYPE, 2);
  Fmt (stringvalue, "%s<%i", integervalue);
  SetCtrlVal (panelHandle, PANEL_ITEM_VALUE_STRING, stringvalue);


  SetCtrlToolTipAttribute (panelHandle, PANEL_ITEM_VALUE_STRING, CTRL_TOOLTIP_ATTR_TEXT, "");
 }
 //获得项目值,并判断是否为双精度类型
 else if (Ini_GetDouble (inihandle, sectionname, itemname, &doublevalue) >0 )
 {
  SetCtrlVal (panelHandle, PANEL_ITEM_TYPE, 3);
  Fmt (stringvalue, "%s<%f", doublevalue);
  SetCtrlVal (panelHandle, PANEL_ITEM_VALUE_STRING, stringvalue);


  SetCtrlToolTipAttribute (panelHandle, PANEL_ITEM_VALUE_STRING, CTRL_TOOLTIP_ATTR_TEXT, "");
 }  
 //获得项目值,并判断是否为字符串类型
 else if (Ini_GetPointerToRawString (inihandle, sectionname, itemname, &pointertostring) >0 )
 {
  SetCtrlVal (panelHandle, PANEL_ITEM_TYPE, 4);
  SetCtrlVal (panelHandle, PANEL_ITEM_VALUE_STRING, pointertostring);
    
  //由于值经常超出文本框显示范围,设置tooltip属性
  SetCtrlToolTipAttribute (panelHandle, PANEL_ITEM_VALUE_STRING, CTRL_TOOLTIP_ATTR_TEXT, pointertostring);
 }
 return 0;      
}
//*********************************************************
//*********************************************************
//*********************************************************


 

PARTNER CONTENT

文章评论3条评论)

登录后参与讨论

用户1847287 2015-8-3 08:14

说了半天,到底最终去的是哪家公司啊?

youmeidequlv_421054107 2015-7-9 09:51

加油!

用户1696769 2015-7-3 09:05

每个人都是在一段段历程中不断成长并强大起来的;博主加油,祝每一段工作旅程都有好的收获!

用户1834416 2015-4-9 15:19

很不错,楼主写的很好。

用户442212 2015-2-26 14:46

很不错,楼主写的很好。

用户1721805 2013-11-21 18:51

static IniText inihandle; 报错:28, 17 syntax error; found 'identifier' expecting ';'.是什么原因?
相关推荐阅读
sz_lihongtao 2011-04-20 21:32
32bit无符号数快速开平方根
//*******************************************************************************// 32bit无符号数开平方根// ...
sz_lihongtao 2011-04-20 12:12
STM32学习日志(24)----使用dsp库的FFT函数测相位
attachment download/**  ****************************************************************************...
sz_lihongtao 2011-04-19 14:57
STM32学习日志(23)----使用dsp库的FFT函数.rar
attachment download/**  ****************************************************************************...
sz_lihongtao 2011-04-19 10:39
stm32 dsp lib V2.0
attachment downloadattachment download...
sz_lihongtao 2011-04-19 10:37
STM32学习日志(22)----使用DMA功能自动更新PWM的输出
attachment download/*******************************************************************************编...
sz_lihongtao 2010-09-08 21:59
Labwindows/cvi8.5学习日志(56)----任意波形发生器
//******************************************************************************// Labwindows/cvi8.5...
EE直播间
更多
我要评论
3
11
关闭 站长推荐上一条 /3 下一条