⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ini.c

📁 labwindow 编程的toolbox例程。
💻 C
📖 第 1 页 / 共 2 页
字号:
/*---------------------------------------------------------------------------*/
/*                                                                           */
/* FILE:    ini.c                                                            */
/*                                                                           */
/* PURPOSE: This example illustrates how to use the Inifile instrument driver*/
/*          to read, write, and modify INI-style tagged text files.  The     */
/*          instrument driver contains functions to:                         */
/*                                                                           */
/*          - create and dispose an object which can contain an in-memory    */
/*            list of tag/value pairs.                                       */
/*          - sort the in-memory list of tag/value pairs                     */
/*          - read a list of tag/value pairs from a file                     */
/*          - write the in-memory list of tag/value pairs to a file          */
/*                                                                           */
/*          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.                */
/*                                                                           */
/*---------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------*/
/* Includes                                                                 */
/*--------------------------------------------------------------------------*/
#include <utility.h>
#include <userint.h>
#include "inifile.h"
#include "ini.h"

/*--------------------------------------------------------------------------*/
/* Macros                                                                   */
/*--------------------------------------------------------------------------*/
#define MAX_NAME_SIZE 256

/*--------------------------------------------------------------------------*/
/* Module-globals                                                           */
/*--------------------------------------------------------------------------*/
static int     g_changesMade   = 0;
static char    g_fileName[MAX_PATHNAME_LEN];
static IniText g_myInifile = 0;

/*---------------------------------------------------------------------------*/
/* Internal function prototypes                                              */
/*---------------------------------------------------------------------------*/
int UpdateUIR (int panelHandle);
    
/*---------------------------------------------------------------------------*/
/* This is the application's entry-point.                                    */
/*---------------------------------------------------------------------------*/
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);
    
    /* Use Browse button callback to ask for an initial file */
    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) && (g_changesMade))
        {
        if (ConfirmPopup ("Save Changes",
            "Do you want to save changes?"))
            Ini_WriteToFile (g_myInifile, g_fileName);
        }    
    
    /* Destroy the Inifile memory object*/
    if (g_myInifile)
        {
        Ini_Dispose (g_myInifile);
        g_myInifile = 0;
        }    
    DiscardPanel (panelHandle);
    CloseCVIRTE ();
    return 0;
}

/*--------------------------------------------------------------------------*/
/* Allow the user to browse for an INI file, and load it into memory.       */
/*--------------------------------------------------------------------------*/
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",
                                 "Specify INI file to open", VAL_LOAD_BUTTON,
                                 0, 0, 1, 1, tempFileName)
                != VAL_EXISTING_FILE_SELECTED)
                return 0;                               

            /* Write the currently loaded file, if any, back out */
            if ((g_myInifile) && (g_changesMade))
                {
                if (ConfirmPopup ("Save Changes",
                                  "Do you want to save changes before opening "
                                  "a new file?"))
                    Ini_WriteToFile (g_myInifile, g_fileName);
                }    

            strcpy (g_fileName, tempFileName);
            
            /* Destroy the Inifile if it currently exists */
            if (g_myInifile)
                {
                Ini_Dispose (g_myInifile);
                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;
                }   
            if (Ini_ReadFromFile (g_myInifile, g_fileName))
                {
                MessagePopup("Inifile","Error reading Inifile");
                goto Error;
                }   
            SetCtrlVal (panel, PANEL_FILENAME, g_fileName);    
            UpdateUIR (panel);
            break;
        }
        
    return 0;
    
Error:        
    if (g_myInifile);
        Ini_Dispose (g_myInifile);
        

    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));
        SetCtrlAttribute (panelHandle, PANEL_SECTION, ATTR_MAX_VALUE,
                          sections);
    
        /* Set sections and item information */
        if (sections) 
            {    
            GetCtrlVal (panelHandle, PANEL_SECTION, &section);
            Ini_NthSectionName (g_myInifile, section, &sectionName);
            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));
                SetCtrlAttribute (panelHandle, PANEL_ITEM, ATTR_MAX_VALUE,
                                  items);
                if (items)
                    {
                    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);
                            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);
                            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);
                            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);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -