amath/src/system/filesystem_amiga.cpp

194 lines
4.8 KiB
C++
Raw Normal View History

2017-02-27 22:31:21 +00:00
/*-
2017-02-27 22:23:06 +00:00
* Copyright (c) 2014-2017 Carsten Sonne Larsen <cs@innolan.net>
2015-04-01 12:43:50 +00:00
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
2017-02-27 22:31:21 +00:00
* Project homepage:
* http://amath.innolan.net
*
2015-04-01 12:43:50 +00:00
*/
2017-02-27 22:23:06 +00:00
#include "amath.h"
#include "amathc.h"
2015-04-01 12:43:50 +00:00
#include "lib/charbuf.h"
#include "localize/text.h"
#include "system/program.h"
#include "system/filesystem_amiga.h"
#ifdef AMIGA
2017-02-27 22:23:06 +00:00
#include <clib/dos_protos.h>
2015-04-01 12:43:50 +00:00
/**
* @brief
*
* http://en.wikibooks.org/wiki/Aros/Developer/Docs/Libraries/DOS#Examine_Files_or_Directories
*
*/
2017-02-27 22:23:06 +00:00
CharBuffer* AmigaFilesystem::ListDirectory(const char* path)
2015-04-01 12:43:50 +00:00
{
static const long int pathsize = 256;
2017-02-27 22:23:06 +00:00
CharBuffer* pathbuf = new CharBuffer(pathsize);
2015-04-01 12:43:50 +00:00
2017-02-27 22:23:06 +00:00
if (path == nullptr)
{
if (GetCurrentDirName(pathbuf->GetString(), pathsize))
2015-04-01 12:43:50 +00:00
{
pathbuf->Append(path);
2017-02-27 22:23:06 +00:00
}
else
{
2015-04-01 12:43:50 +00:00
delete pathbuf;
2017-02-27 22:23:06 +00:00
return nullptr;
2015-04-01 12:43:50 +00:00
}
2017-02-27 22:23:06 +00:00
}
else
{
2015-04-01 12:43:50 +00:00
pathbuf->EnsureSize(StrLen(path) + 1);
pathbuf->Append(path);
}
BPTR lock;
BOOL success;
lock = Lock((STRPTR)pathbuf->GetString(), ACCESS_READ);
2017-02-27 22:23:06 +00:00
if (!lock)
{
CharBuffer* res = new CharBuffer();
const char* msg = MSGNODIR;
2015-04-01 12:43:50 +00:00
res->EnsureSize(StrLen(msg) + StrLen(pathbuf->GetString()) + StrLen(NEWLINE) + 1);
res->Empty();
res->Append(msg);
res->Append(pathbuf->GetString());
res->Append(NEWLINE);
delete pathbuf;
return res;
}
2017-02-27 22:23:06 +00:00
CharBuffer* lines = new CharBuffer();
2015-04-01 12:43:50 +00:00
lines->Empty();
bool first = true;
2017-02-27 22:23:06 +00:00
FileInfoBlock* info = new FileInfoBlock();
2015-04-01 12:43:50 +00:00
success = Examine(lock, info);
success = ExNext(lock, info);
2017-02-27 22:23:06 +00:00
while (success)
2015-04-01 12:43:50 +00:00
{
2017-02-27 22:23:06 +00:00
if (first)
{
const char* header = TXTLISTDIRHEADER;
2015-04-01 12:43:50 +00:00
lines->EnsureSize(StrLen(header) + 1);
lines->Empty();
lines->Append(header);
first = false;
}
2017-02-27 22:23:06 +00:00
const char* type;
if (info->fib_DirEntryType < 0)
{
2015-04-01 12:43:50 +00:00
type = TXTLISTDIRTFILE;
2017-02-27 22:23:06 +00:00
}
else
{
2015-04-01 12:43:50 +00:00
type = TXTLISTDIRTDIR;
}
const unsigned short colsize = 12;
unsigned int a = StrLen(type) > colsize ? colsize : StrLen(type);
unsigned int b = colsize - a;
lines->EnsureGrowth(colsize + StrLen((char*)info->fib_FileName) + StrLen(NEWLINE) + 1);
lines->Append(type);
lines->Append(' ', b);
lines->Append((char*)info->fib_FileName);
lines->Append(NEWLINE);
success = ExNext(lock, info);
}
UnLock(lock);
delete pathbuf;
return lines;
}
CharBuffer* AmigaFilesystem::LoadTextFile(const char* name)
{
2017-02-27 22:23:06 +00:00
BPTR file = Open(const_cast<char*>(name), MODE_OLDFILE);
if (!file)
{
return nullptr;
2015-04-01 12:43:50 +00:00
}
2017-02-27 22:23:06 +00:00
CharBuffer* text = new CharBuffer();
2015-04-01 12:43:50 +00:00
text->Empty();
int blocks = 0;
bool eof = false;
LONG c;
2017-02-27 22:23:06 +00:00
while (!eof)
{
2015-04-01 12:43:50 +00:00
blocks++;
text->EnsureSize(blocksize, blocks);
int count = 0;
do
{
c = FGetC(file);
eof = (c <= 0);
2017-02-27 22:23:06 +00:00
if (!eof)
{
text->Append(static_cast<char>(c));
2015-04-01 12:43:50 +00:00
count++;
}
2017-02-27 22:23:06 +00:00
}
while (!eof && count < blocksize);
2015-04-01 12:43:50 +00:00
}
Close(file);
return text;
}
2017-02-27 22:23:06 +00:00
bool AmigaFilesystem::SaveTextFile(const char* name, const char* text)
2015-04-01 12:43:50 +00:00
{
2017-02-27 22:23:06 +00:00
BPTR file = Open(const_cast<char*>(name), MODE_NEWFILE);
2015-04-01 12:43:50 +00:00
2017-02-27 22:23:06 +00:00
if (!file)
{
2015-04-01 12:43:50 +00:00
return false;
}
2017-02-27 22:23:06 +00:00
char* i = const_cast<char*>(text);
2015-04-01 12:43:50 +00:00
LONG r = 1;
2017-02-27 22:23:06 +00:00
while (r > 0 && *i)
{
2015-04-01 12:43:50 +00:00
r = FPutC(file, *i++);
}
Close(file);
return true;
}
#endif