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

📄 misc.c

📁 在ADS环境下MiniGUI的源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
** $Id: misc.c,v 1.53 2004/07/30 07:05:28 weiym Exp $
**
** misc.c: This file include some miscelleous functions. 
**
** Copyright (C) 2003 Feynman Software.
** Copyright (C) 1999 ~ 2002 Wei Yongming.
**
** Create date: 1998/12/31
**
** Current maintainer: Wei Yongming.
*/

/*
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/*
** TODO:
*/ 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>

#include "common.h"
#include "minigui.h"
#include "gdi.h"
#include "window.h"
#include "misc.h"

#ifndef __NOLINUX__
#    include <unistd.h>
#    include <time.h>
#    include <pwd.h>
#    include <sys/ioctl.h>
#    include <sys/types.h>
#endif

#ifdef _CLIPBOARD_SUPPORT
#include "clipboard.h"
#endif

/* Handle of MiniGUI etc file object */
GHANDLE hMgEtc = 0;

#ifndef _INCORE_RES

char ETCFILEPATH [MAX_PATH + 1];
#define ETCFILENAME "MiniGUI.cfg"

static BOOL LookForEtcFile (void)
{
    char etcfile [MAX_PATH + 1];
    char buff [10];
    struct passwd *pwd;

    if ((pwd = getpwuid (geteuid ())) != NULL) {
        strcpy (etcfile, pwd->pw_dir);
        if (etcfile [strlen (etcfile) - 1] != '/')
            strcat (etcfile, "/");
        strcat (etcfile, ".");
        strcat (etcfile, ETCFILENAME);

        if (GetValueFromEtcFile (etcfile, "system", "gal_engine", buff, 8) ==  ETC_OK) {
            strcpy (ETCFILEPATH, etcfile);
            return TRUE;
        }
    }

    strcpy (etcfile, "/usr/local/etc/" ETCFILENAME);
    if (GetValueFromEtcFile (etcfile, "system", "gal_engine", buff, 8) ==  ETC_OK) {
        strcpy (ETCFILEPATH, etcfile);
        return TRUE;
    }

    strcpy (etcfile, "/etc/" ETCFILENAME);
    if (GetValueFromEtcFile (etcfile, "system", "gal_engine", buff, 8) ==  ETC_OK) {
        strcpy (ETCFILEPATH, etcfile);
        return TRUE;
    }
    
    return FALSE;
}

#endif /* _INCORE_RES */

BOOL InitMisc (void)
{
#ifndef _INCORE_RES
    if (!LookForEtcFile ())
    {
        fprintf (stderr, "MISC: Can not locate your MiniGUI.cfg file or bad files!\n");
        return FALSE;
    }
#endif

#ifdef _CLIPBOARD_SUPPORT
    InitClipBoard ();
#endif

    return InitMgEtc ();
}

void TerminateMisc (void)
{
#ifdef _CLIPBOARD_SUPPORT
    TerminateClipBoard ();
#endif

    TerminateMgEtc ();
}

/****************************** ETC file support ******************************/

static char* get_section_name (char *section_line)
{
    char* current;
    char* tail;
    char* name;

    if (!section_line)
        return NULL;

    current = section_line;

    while (*current == ' ' ||  *current == '\t') current++; 

    if (*current == ';' || *current == '#')
        return NULL;

    if (*current++ == '[')
        while (*current == ' ' ||  *current == '\t') current ++;
    else
        return NULL;

    name = tail = current;
    while (*tail != ']' && *tail != '\n' &&
          *tail != ';' && *tail != '#' && *tail != '\0')
          tail++;
    *tail = '\0';
    while (*tail == ' ' || *tail == '\t') {
        *tail = '\0';
        tail--; 
    }

    return name;
}

static int get_key_value (char *key_line, char **mykey, char **myvalue)
{
    char* current;
    char* tail;
    char* value;

    if (!key_line)
        return -1;

    current = key_line;

    while (*current == ' ' ||  *current == '\t') current++; 

    if (*current == ';' || *current == '#')
        return -1;

    if (*current == '[')
        return 1;

    if (*current == '\n' || *current == '\0')
        return -1;

    tail = current;
    while (*tail != '=' && *tail != '\n' &&
          *tail != ';' && *tail != '#' && *tail != '\0')
          tail++;

    value = tail + 1;
    if (*tail != '=')
        *value = '\0'; 

    *tail-- = '\0';
    while (*tail == ' ' || *tail == '\t') {
        *tail = '\0';
        tail--; 
    }
        
    tail = value;
    while (*tail != '\n' && *tail != '\0') tail++;
    *tail = '\0'; 

    if (mykey)
        *mykey = current;
    if (myvalue)
        *myvalue = value;

    return 0;
}


/* This function locate the specified section in the etc file. */
static int etc_LocateSection(FILE* fp, const char* pSection, FILE* bak_fp)
{
    char szBuff[ETC_MAXLINE + 1];
    char *name;

    while (TRUE) {
        if (!fgets(szBuff, ETC_MAXLINE, fp)) {
            if (feof (fp))
                return ETC_SECTIONNOTFOUND;
            else
                return ETC_FILEIOFAILED;
        }
        else if (bak_fp && fputs (szBuff, bak_fp) == EOF)
            return ETC_FILEIOFAILED;
        
        name = get_section_name (szBuff);
        if (!name)
            continue;

        if (strcmp (name, pSection) == 0)
            return ETC_OK; 
    }

    return ETC_SECTIONNOTFOUND;
}

/* This function locate the specified key in the etc file. */
static int etc_LocateKeyValue(FILE* fp, const char* pKey, 
                               BOOL bCurSection, char* pValue, int iLen,
                               FILE* bak_fp, char* nextSection)
{
    char szBuff[ETC_MAXLINE + 1 + 1];
    char* current;
    char* value;
    int ret;

    while (TRUE) {
        int bufflen;

        if (!fgets(szBuff, ETC_MAXLINE, fp))
            return ETC_FILEIOFAILED;
        bufflen = strlen (szBuff);
        if (szBuff [bufflen - 1] == '\n')
            szBuff [bufflen - 1] = '\0';

        ret = get_key_value (szBuff, &current, &value);
        if (ret < 0)
            continue;
        else if (ret > 0) {
            fseek (fp, -bufflen, SEEK_CUR);
            return ETC_KEYNOTFOUND;
        }
            
        if (strcmp (current, pKey) == 0) {
            if (pValue)
                strncpy (pValue, value, iLen);

            return ETC_OK; 
        }
        else if (bak_fp && *current != '\0')
            fprintf (bak_fp, "%s=%s\n", current, value);
    }

    return ETC_KEYNOTFOUND;
}

static int etc_ReadSection (FILE* fp, PETCSECTION psect)
{
    char szBuff[ETC_MAXLINE + 1 + 1];
    char* sect_name;
    int max_key_nr = 5;

    psect->name = NULL;
    psect->key_nr = 0;

    while (TRUE) {
        int bufflen;

        if (!fgets(szBuff, ETC_MAXLINE, fp)) {
            if (feof (fp)) {
                if (psect->name)
                    break;
                else
                    return ETC_SECTIONNOTFOUND;
            }
            else
                return ETC_FILEIOFAILED;
        }

        bufflen = strlen (szBuff);
        if (szBuff [bufflen - 1] == '\n')
            szBuff [bufflen - 1] = '\0';

        if (!psect->name) { /* read section name */
            sect_name = get_section_name (szBuff);
            if (!sect_name)
                continue;

            psect->name = strdup (sect_name);
            psect->key_nr = 0;
            psect->keys = malloc (sizeof(char*)*max_key_nr);
            psect->values = malloc (sizeof(char*)*max_key_nr);
        }
        else { /* read key and value */
            int ret;
            char *key, *value;

            if (psect->key_nr == max_key_nr) {
                max_key_nr += 5;
                psect->keys = (char **) realloc (psect->keys, sizeof(char*)*max_key_nr);
                psect->values = (char **) realloc (psect->values, sizeof(char*)*max_key_nr);
            }

            ret = get_key_value (szBuff, &key, &value);
            if (ret < 0)
                continue;
            else if (ret > 0) {  /* another section begins */
                fseek (fp, -bufflen, SEEK_CUR);
                break;
            }

            *(psect->keys + psect->key_nr) = strdup (key);
            *(psect->values + psect->key_nr) = strdup (value);
            psect->key_nr ++;
        }
    }

    return ETC_OK;
}

GHANDLE GUIAPI LoadEtcFile (const char * pEtcFile)
{
    FILE* fp;
    int max_sect_nr = 15;
    ETC_S *petc;

    if (!(fp = fopen(pEtcFile, "r")))
         return ETC_FILENOTFOUND;

    petc = (ETC_S*) malloc (sizeof(ETC_S));
    petc->section_nr = 0;
    /* we allocate 15 sections first */
    petc->sections = (PETCSECTION) malloc (sizeof(ETCSECTION)*max_sect_nr);

    while (etc_ReadSection (fp, petc->sections + petc->section_nr) == ETC_OK) {
        petc->section_nr ++;
        if (petc->section_nr == max_sect_nr) {
            /* add 5 sections each time we realloc */
            max_sect_nr += 5;
            petc->sections = realloc (petc->sections, sizeof(ETCSECTION)*max_sect_nr);
        }
    }

    fclose (fp);

⌨️ 快捷键说明

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