inifiles.cpp.svn-base

来自「ffshow源码」· SVN-BASE 代码 · 共 803 行 · 第 1/2 页

SVN-BASE
803
字号
    FILETIME LastWriteTime;        ZeroMemory(&LastWriteTime, sizeof(LastWriteTime));    /* First time around */    if(!CurProfile)       {          CurProfile=(PROFILE*)malloc(sizeof(PROFILE) );          if(CurProfile == NULL) return FALSE;          CurProfile->changed=FALSE;          CurProfile->section=NULL;          CurProfile->filename=NULL;          ZeroMemory(&CurProfile->LastWriteTime, sizeof(FILETIME));       }    GetWindowsDirectory( windirW, MAX_PATH );/*    if ((RtlDetermineDosPathNameType_U(filename) == RELATIVE_PATH) &&        !strchrW(filename, '\\') && !strchrW(filename, '/'))    {        static const CHAR wszSeparator[] = {'\\', 0};        strcpy(buffer, windirW);        strcat(buffer, wszSeparator);        strcat(buffer, filename);    }    else*/    {        LPTSTR dummy;        GetFullPathName(filename, sizeof(buffer)/sizeof(buffer[0]), buffer, &dummy);    }            hFile = CreateFile(buffer, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);        if ((hFile == INVALID_HANDLE_VALUE) && (GetLastError() != ERROR_FILE_NOT_FOUND))    {        return FALSE;    }       if ((CurProfile->filename && !strcmp( buffer, CurProfile->filename )))       {          GetFileTime(hFile, NULL, NULL, &LastWriteTime);          if(memcmp(&CurProfile->LastWriteTime, &LastWriteTime, sizeof(FILETIME)))             ;//DPRINTF("(%s): already opened (mru=%d)\n", buffer,0);          else              ;//DPRINTF("(%s): already opened, needs refreshing (mru=%d)\n",buffer,0);          CloseHandle(hFile);          return TRUE;        }    /* Flush the old current profile */    PROFILE_FlushFile();    /* Make the oldest profile the current one only in order to get rid of it *//*    if(i==N_CACHED_PROFILES)      {       tempProfile=MRUProfile[N_CACHED_PROFILES-1];       for(i=N_CACHED_PROFILES-1;i>0;i--)          MRUProfile[i]=MRUProfile[i-1];       CurProfile=tempProfile;      }    if(CurProfile->filename) PROFILE_ReleaseFile();*/    /* OK, now that CurProfile is definitely free we assign it our new file */    CurProfile->filename  = (char_t*)malloc((strlen(buffer)+1) * sizeof(char_t) );    strcpy( CurProfile->filename, buffer );    if (hFile != INVALID_HANDLE_VALUE)    {        CurProfile->section = PROFILE_Load(hFile);        GetFileTime(hFile, NULL, NULL, &CurProfile->LastWriteTime);        CloseHandle(hFile);    }    else    {        /* Does not exist yet, we will create it in PROFILE_FlushFile */    }    return TRUE;}BOOL Tinifile::PROFILE_DeleteSection( PROFILESECTION **section, LPCTSTR name ){    while (*section)    {        if ((*section)->name[0] && !strcmp( (*section)->name, name ))        {            PROFILESECTION *to_del = *section;            *section = to_del->next;            to_del->next = NULL;            PROFILE_Free( to_del );            return TRUE;        }        section = &(*section)->next;    }    return FALSE;}BOOL Tinifile::PROFILE_DeleteKey( PROFILESECTION **section,LPCTSTR section_name, LPCTSTR key_name ){    while (*section)    {        if ((*section)->name[0] && !strcmp( (*section)->name, section_name ))        {            PROFILEKEY **key = &(*section)->key;            while (*key)            {                if (!strcmp( (*key)->name, key_name ))                {                    PROFILEKEY *to_del = *key;                    *key = to_del->next;                    if (to_del->value) free(to_del->value);                    free(to_del );                    return TRUE;                }                key = &(*key)->next;            }        }        section = &(*section)->next;    }    return FALSE;}Tinifile::PROFILEKEY* Tinifile::PROFILE_Find( PROFILESECTION **section, LPCTSTR section_name, LPCTSTR key_name, BOOL create, BOOL create_always ){    LPCTSTR p;    size_t seclen, keylen;    while (PROFILE_isspace(*section_name)) section_name++;    p = section_name + strlen(section_name) - 1;    while ((p > section_name) && PROFILE_isspace(*p)) p--;    seclen = p - section_name + 1;    while (PROFILE_isspace(*key_name)) key_name++;    p = key_name + strlen(key_name) - 1;    while ((p > key_name) && PROFILE_isspace(*p)) p--;    keylen = p - key_name + 1;    while (*section)    {        if ( ((*section)->name[0])             && (!(strncmp( (*section)->name, section_name, seclen )))             && (((*section)->name)[seclen] == '\0') )        {            PROFILEKEY **key = &(*section)->key;            while (*key)            {                /* If create_always is FALSE then we check if the keyname                 * already exists. Otherwise we add it regardless of its                 * existence, to allow keys to be added more than once in                 * some cases.                 */                if(!create_always)                {                    if ( (!(strncmp( (*key)->name, key_name, keylen )))                         && (((*key)->name)[keylen] == '\0') )                        return *key;                }                key = &(*key)->next;            }            if (!create) return NULL;            if ((*key = (PROFILEKEY*)malloc(sizeof(PROFILEKEY) + strlen(key_name) * sizeof(char_t) ))==NULL)                return NULL;            strcpy( (*key)->name, key_name );            (*key)->value = NULL;            (*key)->next  = NULL;            return *key;        }        section = &(*section)->next;    }    if (!create) return NULL;    *section = (PROFILESECTION*)malloc(sizeof(PROFILESECTION) + strlen(section_name) * sizeof(char_t) );    if(*section == NULL) return NULL;    strcpy( (*section)->name, section_name );    (*section)->next = NULL;    if (((*section)->key  = (PROFILEKEY*)malloc(sizeof(PROFILEKEY) + strlen(key_name) * sizeof(char_t) ))==NULL)    {        free(*section);        return NULL;    }    strcpy( (*section)->key->name, key_name );    (*section)->key->value = NULL;    (*section)->key->next  = NULL;    return (*section)->key;}BOOL Tinifile::PROFILE_SetString( LPCTSTR section_name, LPCTSTR key_name, LPCTSTR value, BOOL create_always ){    if (!key_name)  /* Delete a whole section */    {        CurProfile->changed |= PROFILE_DeleteSection( &CurProfile->section,                                                      section_name );        return TRUE;         /* Even if PROFILE_DeleteSection() has failed,                                this is not an error on application's level.*/    }    else if (!value)  /* Delete a key */    {        CurProfile->changed |= PROFILE_DeleteKey( &CurProfile->section,                                                  section_name, key_name );        return TRUE;          /* same error handling as above */    }    else  /* Set the key value */    {        PROFILEKEY *key = PROFILE_Find(&CurProfile->section, section_name,                                        key_name, TRUE, create_always );        if (!key) return FALSE;        /* strip the leading spaces. We can safely strip \n\r and         * friends too, they should not happen here anyway. */        while (PROFILE_isspace(*value)) value++;        if (key->value)        {            if (!strcmp( key->value, value ))            {                //TRACE("  no change needed\n" );                return TRUE;  /* No change needed */            }            //TRACE("  replacing %s\n", debugstr_w(key->value) );            free(key->value );        }        else /*TRACE("  creating key\n" )*/;        key->value = (char_t*)malloc((strlen(value)+1) * sizeof(char_t) );        strcpy( key->value, value );        CurProfile->changed = TRUE;    }    return TRUE;}INT Tinifile::PROFILE_GetSectionNames( LPTSTR buffer, size_t len ){    LPTSTR buf;    size_t f,l;    PROFILESECTION *section;    if (!buffer || !len)        return 0;    if (len==1) {        *buffer='\0';        return 0;    }    f=len-1;    buf=buffer;    section = CurProfile->section;    while ((section!=NULL)) {        if (section->name[0]) {            l = strlen(section->name)+1;            if (l > f) {                if (f>0) {                    strncpy(buf, section->name, f-1);                    buf += f-1;                    *buf++='\0';                }                *buf='\0';                return INT(len)-2;            }            strcpy(buf, section->name);            buf += l;            f -= l;        }        section = section->next;    }    *buf='\0';    return INT(buf-buffer);}INT Tinifile::PROFILE_GetString( LPCTSTR section, LPCTSTR key_name, LPCTSTR def_val, LPTSTR buffer, size_t len ){    PROFILEKEY *key = NULL;    static const char_t empty_str[] = { 0 };    if(!buffer) return 0;    if (!def_val) def_val = empty_str;    if (key_name)    {	if (!key_name[0])        {            /* Win95 returns 0 on keyname "". Tested with Likse32 bon 000227 */            return 0;        }        key = PROFILE_Find( &CurProfile->section, section, key_name, FALSE, FALSE);        PROFILE_CopyEntry( buffer, (key && key->value) ? key->value : def_val,                           len, TRUE );        return (INT)strlen( buffer );    }    /* no "else" here ! */    if (section && section[0])    {        INT ret = PROFILE_GetSection(CurProfile->section, section, buffer, len, FALSE);        if (!buffer[0]) /* no luck -> def_val */        {            PROFILE_CopyEntry(buffer, def_val, len, TRUE);            ret = (INT)strlen(buffer);        }        return ret;    }    buffer[0] = '\0';    return 0;}int Tinifile::PROFILE_GetPrivateProfileString( LPCTSTR section, LPCTSTR entry,LPCTSTR def_val, LPTSTR buffer, size_t len, BOOL allow_section_name_copy ){    int		ret;    LPCTSTR	pDefVal = NULL;    if (!filename)	    return 0;    /* strip any trailing ' ' of def_val. */    if (def_val)    {        LPCTSTR p = &def_val[strlen(def_val)]; /* even "" works ! */	while (p > def_val)	{	    p--;	    if ((*p) != ' ')		break;	}	if (*p == ' ') /* ouch, contained trailing ' ' */	{	    int l = (int)(p - def_val);            LPTSTR pp;	    pp = (LPTSTR)malloc((l + 1) * sizeof(char_t));	    strncpy(pp, def_val, l);	    pp[l] = '\0';            pDefVal = pp;	}    }    if (!pDefVal)	pDefVal = /*(LPCTSTR)*/def_val;    if (PROFILE_Open()) {	if ((allow_section_name_copy) && (section == NULL))            ret = PROFILE_GetSectionNames(buffer, len);	else	    /* PROFILE_GetString already handles the 'entry == NULL' case */            ret = PROFILE_GetString( section, entry, pDefVal, buffer, len );    } else {       strncpy( buffer, pDefVal, len );       ret = (INT)strlen( buffer );    }    if (pDefVal != def_val) /* allocated */	free((void*)pDefVal);    return ret;}DWORD Tinifile::getPrivateProfileSection(const char_t *section, char_t *buffer,DWORD len){ DWORD	ret=0; if (PROFILE_Open())  ret=PROFILE_GetSection(CurProfile->section,section,buffer,len,TRUE); return ret;}BOOL Tinifile::writePrivateProfileString( const char_t * section, const char_t *entry, const char_t *string){    BOOL ret = FALSE;    if (PROFILE_Open( ))    {        if (!section && !entry && !string) /* documented "file flush" case */        {            PROFILE_FlushFile();            PROFILE_ReleaseFile();  /* always return FALSE in this case */        }	else {	    if (!section) {	    } else {		ret = PROFILE_SetString( section, entry, string, FALSE);		PROFILE_FlushFile();	    }	}    }    return ret;}DWORD Tinifile::getPrivateProfileSectionNames( char_t *buffer, DWORD size){    DWORD ret = 0;    if (PROFILE_Open( ))        ret = PROFILE_GetSectionNames(buffer, size);    return ret;}DWORD Tinifile::getPrivateProfileString( const char_t *section, const char_t * entry, const char_t * def_val,  char_t * buffer, DWORD len){    return PROFILE_GetPrivateProfileString( section, entry, def_val,                                            buffer, len, TRUE );}int Tinifile::getPrivateProfileInt(const char_t *section,const char_t *entry,int def_val){ char_t def_valS[60],retS[256]; getPrivateProfileString(section,entry,_itoa(def_val,def_valS,10),retS,256); return atoi(retS);}

⌨️ 快捷键说明

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