home *** CD-ROM | disk | FTP | other *** search
- // Zinc Interface Library - STORAGE.CPP
- // COPYRIGHT (C) 1990, 1991. All Rights Reserved.
- // Zinc Software Incorporated. Pleasant Grove, Utah USA
-
- #include "ui_gen.hpp"
- #include <io.h>
- #include <dos.h>
- #include <dir.h>
- #include <stdio.h>
- #include <fcntl.h>
- #include <sys\stat.h>
- #include <string.h>
-
- #define MAGIC_NUMBER 0x05AF
- #define MAJOR_VERSION 0
- #define MINOR_VERSION 90
-
- struct ZINC_SIGNATURE
- {
- char copyrightNotice[64];
- UCHAR majorVersion;
- UCHAR minorVersion;
- USHORT magicNumber;
- };
-
- static ZINC_SIGNATURE _signature =
- { "Zinc Data File Version 0.9\032", MAJOR_VERSION, MINOR_VERSION, MAGIC_NUMBER };
- int UI_STORAGE::defaultCacheSize = 2048;
- UI_STORAGE *_storage = NULL;
-
- UI_STORAGE::UI_STORAGE(const char *_name, int readWrite) :
- UI_LIST(UI_STORAGE::CompareFunction), cache(NULL),
- cacheSize(defaultCacheSize), cacheOffset(0), cacheRemaining(0),
- stStatus(STS_NO_STATUS)
- {
- strcpy(name, _name);
- access = readWrite ? O_BINARY | O_RDWR : O_BINARY | O_RDONLY;
-
- // Open the file.
- file = _path->OpenFile(name, access);
- if (file != -1)
- {
- ZINC_SIGNATURE signature;
- read(file, &signature, sizeof(signature));
- if (signature.magicNumber != MAGIC_NUMBER)
- {
- close (file);
- file = -1;
- }
- majorVersion = signature.majorVersion;
- minorVersion = signature.minorVersion;
- }
- if (file != -1)
- {
- lseek(file, sizeof(_signature), SEEK_SET);
- short noOfObjects;
- read(file, &noOfObjects, sizeof(noOfObjects));
- for (int i = 0; i < noOfObjects; i++)
- {
- UI_STORAGE_ELEMENT *element = new UI_STORAGE_ELEMENT;
- read(file, &element->search, sizeof(element->search));
- if (element->search.size)
- Add(element);
- }
- }
- else if (readWrite)
- {
- file = open(name, O_CREAT | O_TRUNC | access, S_IREAD | S_IWRITE);
- majorVersion = MAJOR_VERSION;
- minorVersion = MINOR_VERSION;
- }
- if (file == -1)
- {
- stStatus |= STS_OPEN_ERROR;
- return;
- }
-
- // Initialize the cache sheme.
- if (cacheSize)
- cache = new char[cacheSize];
- }
-
- UI_STORAGE::~UI_STORAGE()
- {
- if (cache)
- delete cache;
- if (file != -1)
- close(file);
- if (_storage == this)
- _storage = NULL;
- }
-
- void UI_STORAGE::AppendFullPath(char *fullPath, const char *pathName, const char *fileName, const char *extension)
- {
- if (pathName != fullPath)
- strcpy(fullPath, pathName);
- if (fullPath[0] != '\0' && fullPath[strlen(fullPath)-1] != '\\')
- strcat(fullPath, "\\");
- strcat(fullPath, fileName);
- if (extension)
- UI_STORAGE::ChangeExtension(fullPath, extension);
- strupr(fullPath);
- }
-
- void UI_STORAGE::ChangeExtension(char *name, const char *newExtension)
- {
- char *oldExtension = strrchr(name, '.');
- if (oldExtension)
- *oldExtension = '\0';
- if (newExtension)
- strcat(name, newExtension);
- }
-
- int UI_STORAGE::CompareFunction(void *element1, void *element2)
- {
- return (strcmp(((UI_STORAGE_ELEMENT *)element1)->search.stringID, ((UI_STORAGE_ELEMENT *)element2)->search.stringID));
- }
-
- int UI_STORAGE::FindName(void *object, void *matchName)
- {
- return (strcmp(((UI_STORAGE_ELEMENT *)object)->search.stringID, (char *)matchName));
- }
-
- int UI_STORAGE::FindNumber(void *object, void *matchNumber)
- {
- return ((((UI_STORAGE_ELEMENT *)object)->search.numberID == *(USHORT *)matchNumber) ? 0 : -1);
- }
-
- int UI_STORAGE::Load(void *buffer, int length)
- {
- // See if cache is turned on.
- if (!cache)
- return (read(file, buffer, length));
-
- // See if there is enough cached to read.
- if (length > cacheRemaining)
- {
- memmove(cache, &cache[cacheOffset], cacheRemaining);
- read(file, &cache[cacheRemaining], cacheSize - cacheRemaining);
- cacheRemaining = cacheSize;
- cacheOffset = 0;
- }
-
- // Copy the cached buffer to the specified read buffer.
- memmove(buffer, &cache[cacheOffset], length);
- cacheOffset += length;
- cacheRemaining -= length;
- return (length);
- }
-
- int UI_STORAGE::Load(char **string)
- {
- USHORT size;
- UI_STORAGE::Load(&size, sizeof(short));
- if (size)
- {
- char *readString = new char[size + 1];
- UI_STORAGE::Load(readString, size);
- readString[size] = '\0';
- *string = readString;
- }
- else
- *string = NULL;
- return (size + sizeof(short));
- }
-
- void UI_STORAGE::ObjectSize(const char *name, UI_SEARCH_INFO &search)
- {
- search.size = (int)(lseek(file, 0L, SEEK_CUR) + cacheOffset - search.offset);
- if (!name)
- return;
- if (cacheOffset)
- {
- write(file, cache, cacheOffset);
- cacheOffset = 0;
- cacheRemaining = cacheSize;
- }
- UI_STORAGE_ELEMENT *element = (UI_STORAGE_ELEMENT *)UI_LIST::Get(UI_STORAGE::FindName, name);
- if (!element)
- element = (UI_STORAGE_ELEMENT *)UI_LIST::Get(UI_STORAGE::FindName, search.stringID);
- if (element)
- element->search = search;
- }
-
- long UI_STORAGE::Offset()
- {
- return(lseek(file, 0L, SEEK_CUR) + cacheOffset);
- }
-
- void UI_STORAGE::Save()
- {
- if (!FlagSet(access, O_RDWR))
- return;
-
- // Store the signature information.
- int saveFile = open("temp.$$$", O_CREAT | O_TRUNC | O_BINARY | O_RDWR, S_IREAD | S_IWRITE);
- write(saveFile, &_signature, sizeof(_signature));
-
- // Store the number of objects.
- short noOfObjects = Count();
- write(saveFile, &noOfObjects, sizeof(noOfObjects));
-
- // Save room for the search elements.
- lseek(saveFile, noOfObjects * sizeof(UI_SEARCH_INFO), SEEK_CUR);
-
- // Store out the object information.
- for (UI_STORAGE_ELEMENT *element = First(); element; )
- if (element->search.size > 0)
- {
- char *buffer = new char[element->search.size];
- lseek(file, element->search.offset, SEEK_SET);
- read(file, buffer, element->search.size);
- element->search.offset = lseek(saveFile, 0L, SEEK_CUR);
- write(saveFile, buffer, element->search.size);
- delete buffer;
- element = element->Next();
- }
-
- // Write the object offset information.
- lseek(saveFile, sizeof(_signature) + sizeof(noOfObjects), SEEK_SET);
- for (element = First(); element; element = element->Next())
- write(saveFile, &element->search, sizeof(element->search));
-
- // Update to the original file.
- close(file);
- close(saveFile);
-
- UI_STORAGE::ChangeExtension(name, ".DAT");
- remove(name);
- rename("temp.$$$", name);
- file = open(name, access);
- }
-
- UI_STORAGE_ELEMENT *UI_STORAGE::Seek(const char *objectName, int seekMode)
- {
- UI_STORAGE_ELEMENT *element = (UI_STORAGE_ELEMENT *)UI_LIST::Get(UI_STORAGE::FindName, objectName);
- if (!element && seekMode == SEEK_READ)
- return (NULL);
- else if (!element)
- {
- element = new UI_STORAGE_ELEMENT;
- memset(element->search.stringID, 0, sizeof(element->search.stringID));
- strcpy(element->search.stringID, objectName);
- element->search.offset = lseek(file, 0, SEEK_END);
- element->search.size = 0;
- element->search.type = 0;
- element->search.numberID = 0;
- UI_LIST::Add(element);
- }
- else if (seekMode == SEEK_WRITE)
- element->search.offset = lseek(file, 0, SEEK_END);
- else
- lseek(file, element->search.offset, SEEK_SET);
-
- cacheOffset = 0;
- cacheRemaining = (seekMode == SEEK_READ) ? 0 : cacheSize;
- return (element);
- }
-
- UI_STORAGE_ELEMENT *UI_STORAGE::Seek(USHORT objectNumber, int seekMode)
- {
- UI_STORAGE_ELEMENT *element = (UI_STORAGE_ELEMENT *)UI_LIST::Get(UI_STORAGE::FindNumber, &objectNumber);
-
- if (!element)
- return (NULL);
- else if (seekMode == SEEK_WRITE)
- element->search.offset = lseek(file, 0L, SEEK_END);
- else
- lseek(file, element->search.offset, SEEK_SET);
-
- cacheOffset = 0;
- cacheRemaining = (seekMode == SEEK_READ) ? 0 : cacheSize;
- return (element);
- }
-
- int UI_STORAGE::Store(void *buffer, int length)
- {
- // Make sure write is enabled.
- if (!FlagSet(access, O_RDWR))
- return (0);
-
- // See if cache is turned on.
- if (!cache)
- return (write(file, buffer, length));
-
- // See if flushing is requested or if there is enough cached to write.
- if (length > cacheRemaining)
- {
- write(file, cache, cacheOffset);
- cacheOffset = 0;
- cacheRemaining = cacheSize;
- }
-
- // Copy the file write buffer to the cached buffer.
- memmove(&cache[cacheOffset], buffer, length);
- cacheOffset += length;
- cacheRemaining -= length;
- return (length);
- }
-
- int UI_STORAGE::Store(const char *string)
- {
- USHORT size = string ? strlen(string) : 0;
- UI_STORAGE::Store(&size, sizeof(size));
- if (size)
- size = UI_STORAGE::Store(string, size);
- return (size + sizeof(short));
- }
-
- void UI_STORAGE::StripFullPath(const char *fullPath, char *pathName, char *fileName, char *objectName)
- {
- // Determine the path/file split area.
- char *name = strrchr(fullPath, '\\');
- if (!name)
- name = strrchr(fullPath, ':');
-
- // Construct the path name.
- if (pathName)
- {
- if (name)
- {
- strcpy(pathName, fullPath);
- pathName[(int)(name - fullPath)] = '\0';
- strupr(pathName);
- }
- else
- pathName[0] = '\0';
- }
-
- // Construct the file name.
- if (fileName)
- {
- if (name)
- strcpy(fileName, ++name);
- else
- strcpy(fileName, fullPath);
- name = strrchr(fileName, '~');
- if (name)
- fileName[(int)(name - fileName)] = '\0';
- strupr(fileName);
- }
-
- // Construct the object name.
- if (objectName)
- {
- name = strrchr(fullPath, '~');
- if (name)
- strcpy(objectName, ++name);
- else
- objectName[0] = '\0';
- strupr(objectName);
- }
- }
-
- int UI_STORAGE::ValidName(const char *name, int createStorage)
- {
- char path[MAXPATH];
- if (createStorage)
- {
- UI_STORAGE::StripFullPath(name, path);
- strcat(path, "\\*.*");
- }
- else
- strcpy(path, name);
- struct ffblk fileBlock;
- return (!findfirst(path, &fileBlock, FA_DIREC));
- }
-