📄 filecache.cpp
字号:
/* <LIC_AMD_STD> * Copyright (c) 2005 Advanced Micro Devices, Inc. * * 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 * * The full GNU General Public License is included in this distribution in the * file called COPYING * </LIC_AMD_STD> *//* <CTL_AMD_STD> * </CTL_AMD_STD> *//* <DOC_AMD_STD> * </DOC_AMD_STD> */#pragma warning(disable: 4786)#define CFUNC "C"#include "filecache.h"#ifndef OS_Linux #include <windows.h>#endif#include <stdio.h>#include <string.h>#include <stdlib.h>#include <string>#include <iostream>#include <fstream>#include <list>#include <algorithm>#include <signal.h>#ifndef OS_Linux#include <direct.h>#endifstatic int init = 0;#define CACHE_LENGTH 12static CachedFILE filecache[CACHE_LENGTH];static char* filenamecache[CACHE_LENGTH];std::list<std::string> discard;class DiscardPredicate {private: std::string m_s;public: DiscardPredicate(std::string s) : m_s(s) {} int operator()(std::string other) { if (m_s.find(other) == 0) { return 1; } return 0; }};extern "C" void FileCacheAtExit(){ int i; std::cerr << "FileCache: Closing files\n"; for (i=0; i < CACHE_LENGTH; i++) { if (filecache[i]) { fclose((FILE*)filecache[i]); free(filenamecache[i]); } filecache[i] = 0; filenamecache[i] = 0; }}extern "C" void FileCacheAtSignal(int arg){ std::cerr << "FileCache: Signal caught\n"; FileCacheAtExit(); exit(1);}void FileCacheInitialize(){ std::string pattern, yesno; std::ifstream cfg; for (int i=0; i < CACHE_LENGTH; i++) { filecache[i] = 0; filenamecache[i] = strdup(""); }#ifndef OS_Linux { char xCurDir[1024]; getcwd(xCurDir, 255); printf("\nFileCache: Current Directory %s",xCurDir); }#endif cfg.open("decoder.cfg"); if (!cfg) printf("\nFileCache: Din't find decoder.cfg!!"); while (cfg) { cfg >> pattern >> yesno; if (yesno == "no") { discard.push_back(pattern); } } std::cerr << "FileCache: Registering atexit()\n"; atexit(FileCacheAtExit); std::cerr << "FileCache: Registering SIGHUP, SIGINT\n"; signal(SIGINT, &FileCacheAtSignal);#ifdef SIGHUP signal(SIGHUP, &FileCacheAtSignal);#endif init = 1;}// File cachingextern "C" CachedFILE FileCacheGet(const char *xFileName){ char *pFileName = 0; int i; CachedFILE pFile = 0; if (!init) FileCacheInitialize(); std::list<std::string>::iterator it = std::find_if(discard.begin(), discard.end(), DiscardPredicate(xFileName)); if (it != discard.end()) { return 0; } // Get a file from the cache for (i = 0; i < CACHE_LENGTH; i++) { if (strcmp(xFileName, filenamecache[i]) == 0) { pFileName = filenamecache[i]; pFile = filecache[i]; // Shift it to the end to make cache LRU for (; i < CACHE_LENGTH-1; i++) { filecache[i] = filecache[i+1]; filenamecache[i] = filenamecache[i+1]; } filecache[CACHE_LENGTH-1] = pFile; filenamecache[CACHE_LENGTH-1] = pFileName; break; } } if (!pFile) { // Rename file on Linux char buf[256];#if defined(OS_Linux_HACK) sprintf(buf, "%s.gz", xFileName);#else sprintf(buf, "%s", xFileName);#endif pFile = fopen(buf, "a+"); if (pFile) { // Insert in cache, discarding oldest entry. for (i = CACHE_LENGTH - 1; i > 0 && filecache[i]; i--) { } if (filecache[i]) fclose((FILE*)filecache[i]); filecache[i] = pFile; free(filenamecache[i]); filenamecache[i] = strdup(xFileName); } } return pFile;}extern "C" void FileCacheDone(CachedFILE f){}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -