コード例 #1
0
ファイル: compiler.cpp プロジェクト: jimfinnis/lana-2
void Compiler::feedFile(const char *fileName){
    FILE *a = fopen(fileName,"r");
    
    if(!a){
        throw Exception(NULL).set("cannot open file %s",fileName);
    }
    
    try {
        char buf[1024];
        notifyNewFile(fileName);
        while(fgets(buf,1024,a)!=NULL) {
            feed(buf);
        }
        fclose(a);
    } catch(...) {
        fclose(a);
        notifyNewFile(NULL);
        throw;
    }
    notifyNewFile(NULL);
}
コード例 #2
0
void DirectoryListingService::updateEntries() {
	ScopeLock lock(_mutex);

	// stat directory for modification date
	struct stat dirStat;
	if (stat(_dir.c_str(), &dirStat) != 0) {
		UM_LOG_ERR("Error with stat on directory '%s': %s", _dir.c_str(), strerror(errno));
		return;
	}

	if ((unsigned)dirStat.st_mtime >= (unsigned)_lastChecked) {
		// there are changes in the directory
		set<string> currEntries;

#ifndef WIN32
		DIR *dp;
		dp = opendir(_dir.c_str());
		if (dp == NULL) {
			UM_LOG_ERR("Error opening directory '%s': %s", _dir.c_str(), strerror(errno));
			return;
		}
		// iterate all entries and see what changed
		struct dirent* entry;
		while((entry = readdir(dp))) {
			string dname = entry->d_name;
#else
		WIN32_FIND_DATA ffd;
		HANDLE hFind = INVALID_HANDLE_VALUE;
		TCHAR szDir[MAX_PATH];
		StringCchCopy(szDir, MAX_PATH, _dir.c_str());
		StringCchCat(szDir, MAX_PATH, TEXT("\\*"));

		hFind = FindFirstFile(szDir, &ffd);
		do {
			string dname = ffd.cFileName;
#endif

			// see if the file was changed
			char* filename;
			asprintf(&filename, "%s/%s", _dir.c_str(), dname.c_str());

			struct stat fileStat;
			if (stat(filename, &fileStat) != 0) {
				UM_LOG_ERR("Error with stat on directory entry '%s': %s", filename, strerror(errno));
				free(filename);
				continue;
			}

			if (fileStat.st_mode & S_IFDIR) {
				// ignore directories
				free(filename);
				continue;
			}

			// are we interested in such a file?
			if (!filter(dname)) {
				free(filename);
				continue;
			}
			currEntries.insert(dname);

			if (_knownEntries.find(dname) != _knownEntries.end()) {
				// we have seen this entry before
				struct stat oldStat = _knownEntries[dname];
				if (oldStat.st_mtime < fileStat.st_mtime) {
					notifyModifiedFile(dname, fileStat);
				}
			} else {
				// we have not yet seen this entry
				notifyNewFile(dname, fileStat);
			}

			free(filename);
			_knownEntries[dname] = fileStat; // gets copied on insertion
#ifndef WIN32
		}
		closedir(dp);
#else
		}
		while (FindNextFile(hFind, &ffd) != 0);
		FindClose(hFind);
#endif
		// are there any known entries we have not seen this time around?
		map<string, struct stat>::iterator fileIter = _knownEntries.begin();
		while(fileIter != _knownEntries.end()) {
			if (currEntries.find(fileIter->first) == currEntries.end()) {
				// we used to know this file
				notifyRemovedFile(fileIter->first, fileIter->second);
				_knownEntries.erase(fileIter->first);
			}
			fileIter++;
		}
		// remember when we last checked the directory for modifications
#ifndef WIN32
		time(&_lastChecked);
#else
		// TODO: this will fail with subsecond updates to the directory
		_lastChecked = dirStat.st_mtime + 1;
#endif
	}