Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Лабы / 2 / lab.01.by mice / fexchange / fx_fblk

.cpp
Скачиваний:
11
Добавлен:
17.04.2013
Размер:
2.04 Кб
Скачать
#include "fx_fblk.h"

HANDLE cfx_fblk::_mutex = NULL;

cfx_fblk::cfx_fblk() {
	if (_mutex == NULL) _mutex = CreateMutex(NULL, TRUE, NULL);
	else WaitForSingleObject(_mutex, 1000);
	_file = fopen("fx_fblks.---", "r+");
	if (NULL == _file) _file = fopen("fx_fblks.---", "w+");
}


cfx_fblk::~cfx_fblk() {
	fclose(_file);
	ReleaseMutex(_mutex);
}


int cfx_fblk::block(const char *const file_name, const FILE *const file) {
	rewind(_file);
	bool blocked = false;

	char *str = (char *)malloc(_MX_FN_LEN * sizeof(char));
	while (!feof(_file)) {
		if (NULL == fgets(str, _MX_FN_LEN, _file)) break;

		int len;
		for (len = (int)strlen(str); 0 < len; --len) {
			if (' ' < str[len-1]) break;
			str[len-1] = '\0';
		}
		char *fname = NULL;
		for (int i = 0; i < len; ++i) {
			switch(str[i]) {
				case '/':
					str[i] = '\0';
					fname = str + i + 1;
				case '#':
					break;

				default:
					continue;
			}
			
		}
		if (NULL != fname && 0 == stricmp(file_name, fname)) {
			SetLastError(32);
			blocked = true;
			break;
		}
	}
	free(str);

	if (blocked) return -1;

	fprintf(_file, "%p/%s\n", file, file_name);

	return 0;
}


int cfx_fblk::ublock(const char *const file_name) {
	rewind(_file);
	bool done = false;

	char *str = (char *)malloc(_MX_FN_LEN * sizeof(char));
	while (!feof(_file)) {
		int fpos = ftell(_file);
		if (NULL == fgets(str, _MX_FN_LEN, _file)) break;

		int len = (int)strlen(str);
		str[len-1] = '\0';
		
		char *fname = NULL; int i;
		for (i = 0; i < len; ++i) {
			switch(str[i]) {
				case '/':
					str[i] = '\0';
					fname = str + i + 1;
				case '#':
					break;

				default:
					continue;
			}
			break;
		}

		if (NULL == fname) continue;
		if (0 == stricmp(file_name, fname)) {
			str[i] = '#';
			fseek(_file, fpos, SEEK_SET);
			fprintf(_file, "%s\n", str);
			done = true;
			break;
		}
	}
	free(str);

	return (done)? 0: -1;
}


int cfx_fblk::ublock(const FILE *const file) {
	return -1;
}
Соседние файлы в папке fexchange