inifiles.cpp.svn-base
来自「ffshow源码」· SVN-BASE 代码 · 共 803 行 · 第 1/2 页
SVN-BASE
803 行
/* * Profile functions * * Copyright 1993 Miguel de Icaza * Copyright 1996 Alexandre Julliard * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */#include "stdafx.h"#include "inifiles.h"#include "ffdebug.h"#include "Tstream.h"Tinifile::Tinifile(const char_t *Iflnm,int Iencoding):CurProfile(NULL){ strcpy(filename,Iflnm); encoding=Iencoding==-1?(sizeof(char_t)==sizeof(char)?Tstream::ENC_ASCII:Tstream::ENC_LE16):Iencoding;}Tinifile::~Tinifile(){ if (CurProfile) { PROFILE_ReleaseFile(); free(CurProfile); }}void Tinifile::PROFILE_WriteMarker(Tstream &hFile){}void Tinifile::PROFILE_WriteLine( Tstream &hFile, char_t * szLine, size_t len){ //DWORD dwBytesWritten; //WriteFile(hFile, szLine, (DWORD)len*sizeof(char_t), &dwBytesWritten, NULL); hFile.fputs(szLine);}void Tinifile::PROFILE_Save( Tstream &hFile, const PROFILESECTION *section){ PROFILEKEY *key; char_t *buffer, *p; PROFILE_WriteMarker(hFile); for ( ; section; section = section->next) { size_t len = 0; if (section->name[0]) len += strlen(section->name) + 6; for (key = section->key; key; key = key->next) { len += strlen(key->name) + 2; if (key->value) len += strlen(key->value) + 1; } buffer = (char_t*)alloca(len * sizeof(char_t)); if (!buffer) return; p = buffer; if (section->name[0]) { //*p++ = '\r'; *p++ = '\n'; *p++ = '['; strcpy( p, section->name ); p += strlen(p); *p++ = ']'; //*p++ = '\r'; *p++ = '\n'; } for (key = section->key; key; key = key->next) { strcpy( p, key->name ); p += strlen(p); if (key->value) { *p++ = '='; strcpy( p, key->value ); p += strlen(p); } //*p++ = '\r'; *p++ = '\n'; } *p='\0'; PROFILE_WriteLine( hFile, buffer, len); }}BOOL Tinifile::PROFILE_FlushFile(void){ HANDLE hFile = NULL; FILETIME LastWriteTime; if(!CurProfile) { return FALSE; } if (!CurProfile->changed) return TRUE; TstreamFile *file=new TstreamFile(CurProfile->filename,false,true,(Tstream::ENCODING)encoding); PROFILE_Save( *file, CurProfile->section); delete file; hFile = CreateFile(CurProfile->filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) return FALSE; if(GetFileTime(hFile, NULL, NULL, &LastWriteTime)) CurProfile->LastWriteTime=LastWriteTime; CloseHandle( hFile ); CurProfile->changed = FALSE; return TRUE;}void Tinifile::PROFILE_Free( PROFILESECTION *section ){ PROFILESECTION *next_section; PROFILEKEY *key, *next_key; for ( ; section; section = next_section) { for (key = section->key; key; key = next_key) { next_key = key->next; if (key->value) free(key->value ); free(key ); } next_section = section->next; free(section ); }}void Tinifile::PROFILE_ReleaseFile(void){ PROFILE_FlushFile(); PROFILE_Free( CurProfile->section ); if (CurProfile->filename) free(CurProfile->filename ); CurProfile->changed = FALSE; CurProfile->section = NULL; CurProfile->filename = NULL; ZeroMemory(&CurProfile->LastWriteTime, sizeof(CurProfile->LastWriteTime));}const char_t* Tinifile::PROFILE_GetLine(const char_t * szStart, const char_t * szEnd){ return (char_t*)memchr(szStart, _l('\n'), szEnd - szStart);}int Tinifile::PROFILE_isspace(char_t c){ if (tchar_traits<char_t>::isspace((tchar_traits<char_t>::uchar_t)c)) return 1; if (c=='\r' || c==0x1a) return 1; /* CR and ^Z (DOS EOF) are spaces too (found on CD-ROMs) */ return 0;}const char_t* Tinifile::memrchr (const char_t *block, char_t c, size_t size){ for (const char_t *p = block + size; p != block; p--) if (*p == c) return p; return 0;}void Tinifile::PROFILE_DetectTextEncoding(const void * buffer, size_t * len){ *len = 0;}Tinifile::PROFILESECTION* Tinifile::PROFILE_Load(HANDLE hFile0){ uint8_t *pBuffer; //char_t * szFile; //const char_t *szLineStart, *szLineEnd; const char_t *szValueStart, *szNameEnd;//, *szEnd; int line = 0; size_t len; PROFILESECTION *section, *first_section; PROFILESECTION **next_section; PROFILEKEY *key, *prev_key, **next_key; DWORD dwFileSize; dwFileSize = GetFileSize(hFile0, NULL); if (dwFileSize == INVALID_FILE_SIZE) return NULL; pBuffer = (uint8_t*)malloc(dwFileSize); if (!pBuffer) return NULL; if (!ReadFile(hFile0, pBuffer, dwFileSize, &dwFileSize, NULL)) { free(pBuffer); return NULL; } len = dwFileSize; TstreamMem hFile(pBuffer,len,Tstream::ENC_AUTODETECT);hFile.stripEOLN(true); encoding=hFile.encoding; //PROFILE_DetectTextEncoding(pBuffer, &len); /* len is set to the number of bytes in the character marker. * we want to skip these bytes */ //pBuffer = (char_t*)((char *)pBuffer + len); //dwFileSize -= (DWORD)len; //szFile = (char_t *)pBuffer; //szEnd = (char_t *)((char *)pBuffer + dwFileSize); first_section = (PROFILESECTION*)malloc(sizeof(*section) ); if(first_section == NULL) { //if (szFile != pBuffer) //free( szFile); free(pBuffer); return NULL; } first_section->name[0] = 0; first_section->key = NULL; first_section->next = NULL; next_section = &first_section->next; next_key = &first_section->key; prev_key = NULL; //szLineEnd = szFile - 1; /* will be increased to correct value in loop */ char_t *szLineStart=(char_t*)alloca((dwFileSize+3)*sizeof(char_t)); while (hFile.fgets(szLineStart,dwFileSize)) { const char_t *szLineEnd=strchr(szLineStart,'\0'); //szLineStart = szLineEnd + 1; //if (szLineStart >= szEnd) //break; //szLineEnd = PROFILE_GetLine(szLineStart, szEnd); //if (!szLineEnd) //szLineEnd = szEnd; //line++; while (*szLineStart && PROFILE_isspace(*szLineStart)) szLineStart++; if (!*szLineStart /*>= szLineEnd*/) continue; if (*szLineStart == '[') /* section start */ { const char_t * szSectionEnd; if ((szSectionEnd = memrchr( szLineStart, _l(']'), szLineEnd - szLineStart ))==NULL) { //WARN("Invalid section header at line %d: %s\n",line, debugstr_wn(szLineStart, (int)(szLineEnd - szLineStart)) ); } else { szLineStart++; len = (int)(szSectionEnd - szLineStart); /* no need to allocate +1 for NULL terminating character as * already included in structure */ if ((section = (PROFILESECTION*)malloc(sizeof(*section) + len * sizeof(char_t) ))==NULL) break; memcpy(section->name, szLineStart, len * sizeof(char_t)); section->name[len] = '\0'; section->key = NULL; section->next = NULL; *next_section = section; next_section = §ion->next; next_key = §ion->key; prev_key = NULL; //TRACE("New section: %s\n", debugstr_w(section->name)); continue; } } /* get rid of white space at the end of the line */ while ((szLineEnd > szLineStart) && ((*szLineEnd == '\n') || PROFILE_isspace(*szLineEnd))) szLineEnd--; /* line end should be pointing to character *after* the last wanted character */ szLineEnd++; /* get rid of white space after the name and before the start * of the value */ if ((szNameEnd = szValueStart = (const char_t*)memchr( szLineStart, _l('='), szLineEnd - szLineStart )) != NULL) { szNameEnd = szValueStart - 1; while ((szNameEnd > szLineStart) && PROFILE_isspace(*szNameEnd)) szNameEnd--; szValueStart++; while (szValueStart < szLineEnd && PROFILE_isspace(*szValueStart)) szValueStart++; } if (!szNameEnd) szNameEnd = szLineEnd - 1; /* name end should be pointing to character *after* the last wanted character */ szNameEnd++; len = (int)(szNameEnd - szLineStart); if (len || !prev_key || *prev_key->name) { /* no need to allocate +1 for NULL terminating character as * already included in structure */ if ((key = (PROFILEKEY*)malloc(sizeof(*key) + len * sizeof(char_t) ))==NULL) break; memcpy(key->name, szLineStart, len * sizeof(char_t)); key->name[len] = '\0'; if (szValueStart) { len = (int)(szLineEnd - szValueStart); key->value = (char_t*)malloc((len + 1) * sizeof(char_t) ); memcpy(key->value, szValueStart, len * sizeof(char_t)); key->value[len] = '\0'; } else key->value = NULL; key->next = NULL; *next_key = key; next_key = &key->next; prev_key = key; //TRACE("New key: name=%s, value=%s\n", debugstr_w(key->name), key->value ? debugstr_w(key->value) : "(none)"); } } //if (szFile != pBuffer) //free(szFile); free(pBuffer); return first_section;}void Tinifile::PROFILE_CopyEntry( LPTSTR buffer, LPCTSTR value, size_t len, BOOL strip_quote ){ char_t quote = '\0'; if(!buffer) return; if (strip_quote && ((*value == '\'') || (*value == '\"'))) { if (value[1] && (value[strlen(value)-1] == *value)) quote = *value++; } strncpy( buffer, value, len ); if (quote && (len >= (int)strlen(value))) buffer[strlen(buffer)-1] = '\0';}INT Tinifile::PROFILE_GetSection( PROFILESECTION *section, LPCTSTR section_name, LPTSTR buffer, size_t len, BOOL return_values ){ PROFILEKEY *key; if(!buffer) return 0; while (section) { if (section->name[0] && !strcmp( section->name, section_name )) { size_t oldlen = len; for (key = section->key; key; key = key->next) { if (len <= 2) break; if (!*key->name) continue; /* Skip empty lines */ if (key->name[0] == ';') continue; /* Skip comments */ PROFILE_CopyEntry( buffer, key->name, len - 1, 0 ); len -= strlen(buffer) + 1; buffer += strlen(buffer) + 1; if (len < 2) break; if (return_values && key->value) { buffer[-1] = '='; PROFILE_CopyEntry ( buffer, key->value, len - 1, 0 ); len -= strlen(buffer) + 1; buffer += strlen(buffer) + 1; } } *buffer = '\0'; if (len <= 1) /*If either lpszSection or lpszKey is NULL and the supplied destination buffer is too small to hold all the strings, the last string is truncated and followed by two null characters. In this case, the return value is equal to cchReturnBuffer minus two. */ { buffer[-1] = '\0'; return INT(oldlen) - 2; } return INT(oldlen - len); } section = section->next; } buffer[0] = buffer[1] = '\0'; return 0;}BOOL Tinifile::PROFILE_Open(void){ char_t windirW[MAX_PATH]; char_t buffer[MAX_PATH]; HANDLE hFile = INVALID_HANDLE_VALUE;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?