|
CGlobalMan g_Global; int ncnt; nsize && *pb != ’/n’; pb++, nsize -- ) // 对外接口: g_pGlobal->mapName2MemDb.end() ); done = vsprintf (s, szFormat, arg); {
break; // 获取目录名,文件名 } if ( !pmd ) return false; pfile.fclose(); return 0; pb ++; // 2.可以删除[内存"磁盘"]内的某个文件。 CMemoryDb* pmd = CMemoryDb::MemDbLookup( szDirName ); { if ( !m_bReady ) return false; }; std::map<std::string, FINFO>::iterator it; m_nNodCnt(0), m_nIsRead(0), m_nReadLen(0), m_nOffset(0), m_pSream(NULL) { // 按行读取"1235.bxk"下的内容. str = std::string( sz, i ); #define AFX_MEMORYFILE_H__0A6B4842_05D3_4B2A_A1EC_BAF340135266__INCLUDED_ return; class CMemoryDb; } printf( "file:%s", szfn ); std::map<std::string, FINFO> mapFn2Content; CGlobalMan::~CGlobalMan() bool flush() } return r; // MemoryFile.h: interface for the CMemoryFile class. if ( !m_pSream ) return NULL; RemoveMemDbfile( "c:/www/1235.bxk" ); } std::string strFName; } { return true; } { else char szline[256]; delete mapFn2Content[fn].pdata; { it != mapName2MemDb.end(); it ++ ) virtual ~CGlobalMan(); if ( mapFn2Content.find(fn) != mapFn2Content.end() ) printf("%s", szline); m_pSream = (FINFO*)m_pMDb->FindFile(strFName.c_str(),新2现金网, NULL); CGlobalMan() 具何实现:
//#define pprintf printf }; // 删除文件
blkIter.getBlockFn( szfn ); bool bret = ( m_it != mapFn2Content.end() ); { return &(m_it->second); FINFO* m_pSream; if ( m_nIsRead ) return 0; bool setcnt( int segcnt, int nodcnt ) if ( m_pSream ) // added by wuwenwen pfile0.fprintf( "aaa/nbbb/n" ); CMemoryFile pfile1; va_end (arg); { while ( pfile1.fgets(szline, sizeof(szline))) *pl = ’/0’; } int done = 0; { return ( sz[i] == ’/0’ ) ? NULL : &sz[i+1]; } #if _MSC_VER > 1000 int m_nIsRead; { char s[65000]; } { { m_bReady = false; { // [内存"磁盘"]:模拟一个目录 { } { return false; strcpy( m_dbName, szDirName ); strcpy( item.szfn, fn ); m_pSream = (FINFO*)m_pMDb->CreateFile(strFName.c_str(), 0); #include "MemoryFile.h" { // 存在就先删掉 if ( m_it == mapFn2Content.end() ) } // 获取目录名,文件名 virtual ~CMemoryFile() } } CMemoryFile pfile0; ////////////////////////////////////////////////////////////////////// } bool fopen( const char* fname, const char* mode ) } } static CMemoryDb* CreateMemDb( const char* dbname ) m_strData.reserve( 6 * 1024 * 1024 ); { int m_nOffset;
delete it->second.pdata; std::map<std::string, CMemoryDb*>::iterator it; } scnt = 0; } }
{ it != mapFn2Content.end(); it ++ ) int m_nSegCnt; if(m_pMDb m_pSream) { } return true; { const char* h = szDirName; m_it = mapFn2Content.begin(); static CMemoryDb* MemDbLookup( const char* dbname ) void* FindFile( const char fn[], int* nLen ) g_pGlobal->mapName2MemDb[dbname] = pDb; finalize(); ASSERT( m_pSream->pdata == NULL ); { } #include <string> while ( pfinf = (FINFO*)m_pMDb->Get_DbItem() ) } m_vctBlks.clear(); { int nlen; if (nLen) *nLen = pinfo.nlen; return m_vctBlks[m_index].scnt; { #define ASSERT assert } m_pMDb->DeleteFile(strFName.c_str());
// [内存"磁盘"]文件 读写类 tagFINFO() int fprintf( const char* szFormat,菲律宾太阳城, ... ) void* pdata;
class CGlobalMan // MemoryFile.cpp: implementation of the CMemoryFile class. m_pSream->scnt = m_nSegCnt; { {
pfile1.fclose(); } *pl = ’/0’; } m_pMDb = CMemoryDb::CreateMemDb( m_dbName ); if ( !m_nIsRead ) flush(); m_nOffset ++;
// 1.根据文件名,往[内存"磁盘"]读写文件. strcpy( szDirName, szfn ); bool bRs = RemoveMemDbfile( "c:/www/1234.bxk" ); virtual ~CMemoryDb(); { pfile.fprintf( "aaa/nbbb/n" ); CBlockIter blkIter( "c:/www" ); g_pGlobal->mapName2MemDb.end()) // MemDb文件枚举类 m_vctBlks.push_back( *pfinf ); for( i=0; sz[i] != ’/0’; i++ ) m_nSegCnt = segcnt; private: pprintf( "delete file:%s/n", strFName.c_str( ) ); int fgets( char* line, int nsize ) 应用上是不是相称easy? 实在实现起来也不庞杂,出于下面目标所以拿出来共享:一便利假如能为你节俭一点点开发时光,自己将十分愉快;另一方面,进步程度的方法是"晒代码",有啥批抨的尽量提,洗耳恭听,新皇冠!
{ const char* szToken, std::string& str); typedef unsigned char BYTE; bool close() int getBlockFn( char fn[] ) // 3.可遍历[内存"磁盘"]内的所有文件名(同时获取文件属性m_nSegCnt和m_nNodCnt)。 m_index ++; for ( it = mapFn2Content.begin(); int m_nReadLen; // 全局析构 // 2.fgets或fprintf字符串读写,可写入文件属性m_nSegCnt跟m_nNodCnt。 }; #if !defined(AFX_MEMORYFILE_H__0A6B4842_05D3_4B2A_A1EC_BAF340135266__INCLUDED_) { private: if ( strchr( szToken, sz[i] ) != NULL ) int nlen = m_strData.length(); } if ( !m_pSream m_nIsRead { else { else if ( strcmp( mode, "rb" ) == 0 ) { if ( g_pGlobal->mapName2MemDb.find( dbname ) == m_pMDb = NULL; } m_pSream = NULL; // 对外接口: // return done; char* pl = line; pprintf("write/n"); h = safe_strtok(h, "///", strFName); 如果中间结果数据不超过2G(32位windows的内存地址上限).能够在内存中模仿一个文件系统.将这些信息以"内存文件"情势保存在"内存",同时也能供给"内存目录"(或"内存Db")支撑. // nLen为零:外局部配内存(目前只支持这种), pRet->pdata == NULL } return true; ASSERT(0); public: bool RemoveMemDbfile( const char szfn[] ); { { CMemoryDb *m_pMDb; return true; CMemoryDb* pDb = MemDbLookup(dbname); { pfile0.fclose(); return true; private: } return bret; class CMemoryDb { } if ( bret ) m_it ++; int getNodeCnt() bool DeleteFile( const char fn[] )
class CMemoryFile char* pszNum = m_vctBlks[m_index].szfn; while (h) bool fclose() return true; // 遍历 { virtual ~CBlockIter() }FINFO; } } } pprintf("CMemoryDb()"); return pmd->DeleteFile( strFName.c_str() ); pprintf( "fprintf %s/n", s ); while (h) { m_nIsRead = 0;
void finalize() { nlen = 0; { mapFn2Content.erase( fn ); } { while ( blkIter.next() ) return false; for( ; m_nOffset < m_nReadLen && return m_vctBlks[m_index].ncnt; ASSERT( g_pGlobal->mapName2MemDb.find( dbname ) == } return true; return false; mapFn2Content[fn] = item; for ( it = mapName2MemDb.begin(); CGlobalMan* g_pGlobal = &g_Global; // 内部没调配内存 } char szfn[64]; } 名目中经常须要将中间成果进行保留, 以供后续步骤进行处置. 如果以常设文件的方式往往轻易"裸露"旁边秘密信息. 同时,文件IO读写也较挥霍时间(说到这里,不得不BS一下windows的文件体系,当一个目录下的文件数量超过10000时,读写文件相称慢,Linux却不这样的问题).
{ if ( (pl - line) && *pb == ’/n’ ) { return false; int r = 0; m_bReady(true), m_index(-1)
::fprintf( stdout, "read:%s,not existed/n", strFName.c_str() ); if ( !m_pMDb ) } public: char* pb = &(((char *)m_pSream->pdata)[ m_nOffset ]); // 查找或创建[内存"磁盘"] int i = 0; CBlockIter( const char* db_name ):m_pMDb(NULL),
#include <stdarg.h> #include <map> { pprintf("~CGlobalMan()/n"); #include <assert.h> ////////////////////////////////////////////////////////////////////// } CMemoryDb() return (int)(pl - line); if ( m_pMDb ) m_pMDb = NULL; bool Next_DbItem() if ( strcmp( mode, "wb" ) == 0 ) } char szDirName[256]; m_pSream->nlen = nlen; } std::map<std::string, CMemoryDb*> mapName2MemDb; if (nLen) *nLen = 0; FINFO& pinfo = mapFn2Content[fn]; if ( m_index == (int)m_vctBlks.size() ) return false; if( pszNum ) szDirName[ strlen(szDirName) - strFName.length() - 1 ] = ’/0’; }; bool Begin_DbItem() { { } va_list arg;
bool next() } memcpy( pdata, m_strData.c_str(), nlen ); *pl++ = ’/n’; // 这里还要定义 sscanf( pszNum, "%x", &r ); pprintf("~CMemoryDb()/n"); if(pDb) return pDb; {
{
ASSERT( bRs ); return &pinfo; FINFO* pfinf = NULL; #include "stdafx.h" else char szfn[256]; // } // 遍历内存目录"c:/www"下的所有文件(输出文件名). bool m_bReady; const char* safe_strtok(const char* sz, char szDirName[256]; m_pSream->ncnt = m_nNodCnt; public: return pDb; } #define pprintf FINFO item; m_pSream->pdata = pdata; { #include <vector> { { ASSERT( nLen == 0 ); if ( !m_nIsRead ) int m_nNodCnt; m_strData += s; szDirName[ strlen(szDirName) - strFName.length() - 1 ] = ’/0’; m_pSream = (FINFO*)m_pMDb->FindFile(strFName.c_str(),皇冠备用网址, &m_nReadLen); if ( !m_nIsRead ) return 0; { CMemoryDb* m_pMDb; } } m_pMDb->Next_DbItem(); std::map<std::string, FINFO>::iterator m_it; m_strData.erase(); if ( mapFn2Content.find(fn) == mapFn2Content.end() ) delete pmd; } return NULL; ncnt = 0; close(); pprintf("read/n");
CMemoryFile():m_pMDb(NULL), m_nSegCnt(0), close(); pfile1.fopen( "c:/www/1235.bxk", "rb" ); h = safe_strtok(h, "///", strFName); ASSERT(0); void* Get_DbItem() } { memset( szfn, 0, sizeof(szfn) ); { m_pMDb = CMemoryDb::MemDbLookup( db_name ); {
finalize(); CMemoryDb::~CMemoryDb() *pl++ = *pb; typedef struct tagFINFO private: BYTE* pdata = new BYTE[nlen]; int scnt; return NULL; } { { 下面是"内存文件类"的使用示例(使用上和个别的文件类没任何差别,皇冠搏彩,只是不在磁盘上天生文件): std::string m_strData; bool RemoveMemDbfile( const char szfn[] ) // 读写文件 m_nIsRead = 1; } void* CreateFile( const char fn[], const int nLen ) if ( !m_pMDb ) m_nOffset++; close(); #pragma once #endif // !defined(AFX_MEMORYFILE_H__0A6B4842_05D3_4B2A_A1EC_BAF340135266__INCLUDED_) pfile.fopen( "c:/www/1235.bxk", "wb" ); } 面对上面情形,本人想到了内存机制: } class CBlockIter // 4.根据目录名,皇冠足球投注,可创建或查找[内存"磁盘"],皇冠足球投注备用网。 int getBlockId() return (CMemoryDb*)g_pGlobal->mapName2MemDb[dbname];
public: { h = safe_strtok(h, "///", strFName); pfile0.fopen( "c:/www/1234.bxk", "wb" ); } // 文件存在? } { return &(mapFn2Content[fn]); CMemoryFile pfile; std::vector<FINFO> m_vctBlks; // 删除文件"1234.bxk" CMemoryDb* pmd = (CMemoryDb*)it->second; va_start (arg, szFormat); pDb = new CMemoryDb; extern CGlobalMan* g_pGlobal; const char* h = szDirName; { pdata= NULL;
int getSegCnt() h = safe_strtok(h, "///", strFName); if( m_nOffset == m_nReadLen ) return 0; strcpy( szDirName, fname ); const char* safe_strtok(const char* sz, const char* szToken, std::string& str) if( m_nOffset == m_nReadLen ) return 0; m_strData.length() == 0 ) return false; { } #endif // _MSC_VER > 1000
public: std::string strFName; int m_index; char m_dbName[256]; // 1.依据文件名,创立或翻开内存文件。 while( *pb == ’/n’ ) { m_nNodCnt = nodcnt; strcpy( fn, m_vctBlks[m_index].szfn ); m_pMDb->Begin_DbItem(); (责任编辑:admin) |
