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.h"
|
|
|
|
#include "system/filesystem_stdc.h"
|
|
|
|
|
2017-02-27 22:23:06 +00:00
|
|
|
#if !defined(AMIGA)
|
2015-04-01 12:43:50 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2017-02-27 22:23:06 +00:00
|
|
|
CharBuffer* StandardFilesystem::ListDirectory(const char* path)
|
2015-04-01 12:43:50 +00:00
|
|
|
{
|
2017-02-02 21:58:07 +00:00
|
|
|
#if defined(UNIX) || defined(HAIKU)
|
2015-04-08 09:05:41 +00:00
|
|
|
CharBuffer *pathbuf = new CharBuffer();
|
2015-04-01 12:43:50 +00:00
|
|
|
pathbuf->Empty();
|
2017-02-27 22:23:06 +00:00
|
|
|
if (path == nullptr) {
|
2015-04-01 12:43:50 +00:00
|
|
|
pathbuf->Append(".");
|
|
|
|
} else {
|
|
|
|
pathbuf->EnsureSize(StrLen(path) + 1);
|
|
|
|
pathbuf->Append(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
DIR *dir;
|
|
|
|
if ((dir = opendir(pathbuf->GetString())) == NULL) {
|
|
|
|
CharBuffer *res = new CharBuffer();
|
|
|
|
const char *msg = MSGNODIR;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
CharBuffer *lines = new CharBuffer();
|
|
|
|
lines->Empty();
|
|
|
|
|
|
|
|
bool first = true;
|
|
|
|
struct dirent *entry;
|
|
|
|
while ((entry = readdir(dir)) != NULL) {
|
|
|
|
if (StrIsEqual(entry->d_name, ".") || StrIsEqual(entry->d_name, "..")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (first) {
|
|
|
|
const char *header = TXTLISTDIRHEADER;
|
|
|
|
lines->EnsureSize(StrLen(header) + 1);
|
|
|
|
lines->Empty();
|
|
|
|
lines->Append(header);
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *type;
|
|
|
|
|
2017-02-02 21:58:07 +00:00
|
|
|
#ifdef HAIKU
|
|
|
|
type = TXTLISTDIRTUNKNOWN;
|
|
|
|
#else
|
2015-04-01 12:43:50 +00:00
|
|
|
switch (entry->d_type) {
|
|
|
|
case DT_REG:
|
|
|
|
type = TXTLISTDIRTFILE;
|
|
|
|
break;
|
|
|
|
case DT_DIR:
|
|
|
|
type = TXTLISTDIRTDIR;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
type = TXTLISTDIRTUNKNOWN;
|
|
|
|
}
|
2017-02-02 21:58:07 +00:00
|
|
|
#endif
|
2015-04-01 12:43:50 +00:00
|
|
|
|
|
|
|
const unsigned short colsize = 12;
|
|
|
|
unsigned int a = StrLen(type) > colsize ? colsize : StrLen(type);
|
|
|
|
unsigned int b = colsize - a;
|
|
|
|
|
|
|
|
lines->EnsureGrowth(colsize + StrLen(entry->d_name) + StrLen(NEWLINE) + 1);
|
|
|
|
lines->Append(type);
|
|
|
|
lines->Append(' ', b);
|
|
|
|
lines->Append(entry->d_name);
|
|
|
|
lines->Append(NEWLINE);
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dir);
|
|
|
|
delete pathbuf;
|
|
|
|
return lines;
|
2015-04-07 16:51:26 +00:00
|
|
|
#else
|
2017-02-27 22:23:06 +00:00
|
|
|
CharBuffer* lines = new CharBuffer();
|
2015-04-08 09:05:41 +00:00
|
|
|
lines->Empty();
|
|
|
|
return lines;
|
2015-04-07 16:51:26 +00:00
|
|
|
#endif
|
2015-04-01 12:43:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CharBuffer* StandardFilesystem::LoadTextFile(const char* name)
|
|
|
|
{
|
2017-02-27 22:23:06 +00:00
|
|
|
FILE* file;
|
2015-04-14 12:00:01 +00:00
|
|
|
|
|
|
|
#if !defined(_WIN32)
|
|
|
|
file = fopen(name, "r");
|
|
|
|
#else
|
|
|
|
fopen_s(&file, name, "r");
|
|
|
|
#endif
|
|
|
|
|
2017-02-27 22:23:06 +00:00
|
|
|
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;
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
int c = fgetc(file);
|
|
|
|
eof = c == EOF;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2017-02-27 22:23:06 +00:00
|
|
|
bool StandardFilesystem::SaveTextFile(const char* name, const char* text)
|
2015-04-01 12:43:50 +00:00
|
|
|
{
|
2017-02-27 22:23:06 +00:00
|
|
|
FILE* file;
|
2015-04-14 12:00:01 +00:00
|
|
|
|
|
|
|
#if !defined(_WIN32)
|
|
|
|
file = fopen(name, "w");
|
|
|
|
#else
|
|
|
|
fopen_s(&file, name, "w");
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
int r = EOF + 11;
|
2017-02-27 22:23:06 +00:00
|
|
|
while (r != EOF && *i)
|
|
|
|
{
|
2015-04-01 12:43:50 +00:00
|
|
|
r = fputc(*i++, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|