remove a few C++ modules that have been converted to D

This commit is contained in:
Josh Holtrop 2020-11-24 17:15:33 -05:00
parent 1b5a2d92af
commit b0adb417a0
6 changed files with 0 additions and 479 deletions

View File

@ -1,123 +0,0 @@
#include "File.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#ifdef _WIN32
#define JES_O_BINARY O_BINARY
#else
#define JES_O_BINARY 0
#endif
/**
* Open a file.
*
* @param[in] filename The filename.
* @param[in] writing Whether to open for writing.
*/
bool File::open(const char * filename, bool writing)
{
int fd;
if (writing)
{
/* TODO: handle mode */
fd = ::open(filename, O_WRONLY | O_CREAT | O_TRUNC | JES_O_BINARY, 0644);
}
else
{
fd = ::open(filename, O_RDONLY | JES_O_BINARY);
}
if (fd < 0)
{
return false;
}
m_fd = fd;
return true;
}
/**
* Close the file.
*/
void File::close()
{
if (m_fd >= 0)
{
::close(m_fd);
m_fd = -1;
}
}
/**
* Get the file's size.
*
* This function will reset the file position to the beginning of the file.
*
* @return The file's size, or -1 on error.
*/
size_t File::get_size()
{
if (m_fd >= 0)
{
off_t o = lseek(m_fd, 0, SEEK_END);
lseek(m_fd, 0, SEEK_SET);
return o;
}
return -1;
}
/**
* Read from the file.
*
* @param[in] buf Memory buffer to read into. Must be large enough to hold
* size bytes.
* @param[in] size Number of bytes to read.
*
* @retval false The file failed to read.
* @retval true The file was read successfully.
*/
bool File::read(uint8_t * buf, size_t size)
{
size_t n_bytes_read = 0u;
for (;;)
{
off_t rd_size = ::read(m_fd, &buf[n_bytes_read], size - n_bytes_read);
if (rd_size <= 0)
break;
n_bytes_read += (size_t)rd_size;
if (n_bytes_read >= size)
break;
}
if (n_bytes_read != size)
{
return false;
}
return true;
}
bool File::write(const uint8_t * buf, size_t size)
{
if (size <= 0)
return true;
size_t n_bytes_written = 0u;
for (;;)
{
off_t write_size = ::write(m_fd, &buf[n_bytes_written], size - n_bytes_written);
if (write_size <= 0)
break;
n_bytes_written += (size_t)write_size;
if (n_bytes_written >= size)
break;
}
if (n_bytes_written != size)
{
return false;
}
return true;
}

View File

@ -1,27 +0,0 @@
#ifndef FILE_H
#define FILE_H
#include <stdint.h>
#include <stdlib.h>
#include "Span.h"
class File
{
public:
File() : m_fd(-1) {}
~File() { close(); }
bool open(const char * filename, bool writing = false);
void close();
size_t get_size();
bool read(uint8_t * buf, size_t size);
bool write(const uint8_t * buf, size_t size);
bool write(const Span & span)
{
return write(span.start, span.length);
}
protected:
int m_fd;
};
#endif

View File

@ -1,129 +0,0 @@
#include "GapBuffer.h"
#include "System.h"
#include <string.h>
GapBuffer::GapBuffer()
{
int num_pages = (64u * 1024u + System::page_size - 1u) >> System::page_size_log;
m_buffer = (uint8_t *)System::alloc_pages(num_pages);
m_buffer_size = System::page_size;
m_size = 0u;
m_gap_position = 0u;
}
GapBuffer::GapBuffer(uint8_t * buffer, size_t buffer_size, size_t size)
{
m_buffer = buffer;
m_buffer_size = buffer_size;
m_size = size;
m_gap_position = size;
}
GapBuffer::~GapBuffer()
{
System::free_pages(m_buffer, m_buffer_size >> System::page_size_log);
}
void GapBuffer::insert(size_t position, const uint8_t * data, size_t length)
{
ensure_free(length);
move_gap(position);
memcpy(&m_buffer[m_gap_position], data, length);
m_gap_position += length;
m_size += length;
}
void GapBuffer::erase(size_t position, size_t length)
{
if ((position < m_size) && ((position + length) <= m_size))
{
if ((position + length) == m_gap_position)
{
m_gap_position -= length;
m_size -= length;
}
else
{
move_gap(position);
m_size -= length;
}
}
}
void GapBuffer::clear()
{
m_size = 0u;
m_gap_position = 0u;
}
#ifdef ENABLE_TESTING
std::string GapBuffer::get_string()
{
compact();
return std::string((char *)m_buffer, m_size);
}
void GapBuffer::set_string(const std::string & s)
{
clear();
insert(0u, (const uint8_t *)s.c_str(), s.size());
}
#endif
void GapBuffer::ensure_free(size_t length)
{
if (gap_size() < length)
{
/* We're out of space. Allocate more and move data. */
size_t new_size = (m_buffer_size + (128u * 1024u)) & System::page_base_mask;
size_t new_num_pages = new_size >> System::page_size_log;
uint8_t * new_buffer = (uint8_t *)System::alloc_pages(new_num_pages);
memcpy(new_buffer, m_buffer, m_gap_position);
memcpy(&new_buffer[m_gap_position], &m_buffer[m_gap_position + gap_size()], m_size - m_gap_position);
/* Free the old buffer */
System::free_pages(m_buffer, m_buffer_size >> System::page_size_log);
m_buffer = new_buffer;
m_buffer_size = new_size;
m_gap_position = m_size;
}
}
void GapBuffer::move_gap(size_t position)
{
if (position != m_gap_position)
{
if (position < m_gap_position)
{
memmove(&m_buffer[position + gap_size()], &m_buffer[position], m_gap_position - position);
}
else
{
memmove(&m_buffer[m_gap_position], &m_buffer[m_gap_position + gap_size()], position - m_gap_position);
}
m_gap_position = position;
}
}
void GapBuffer::copy_to(size_t source_position, size_t length, GapBuffer & other, size_t destination_position)
{
if ((source_position < m_size) &&
(length > 0u) &&
((source_position + length) <= m_size))
{
if ((m_gap_position <= source_position) ||
(m_gap_position >= (source_position + length)))
{
/* The gap is before or after the range of data to copy. */
other.insert(destination_position, address(source_position), length);
}
else
{
/* The gap is in the middle of the range of data to copy. */
size_t pre_gap_size = m_gap_position - source_position;
other.insert(destination_position, address(source_position), pre_gap_size);
other.insert(destination_position + pre_gap_size, address(source_position + pre_gap_size), length - pre_gap_size);
}
}
}

View File

@ -1,169 +0,0 @@
#ifndef GAPBUFFER_H
#define GAPBUFFER_H
#include <stdint.h>
#include <stdlib.h>
#ifdef ENABLE_TESTING
#include <string>
#endif
class GapBuffer
{
public:
/**
* Create a new GapBuffer object.
*
* The GapBuffer will allocate its own memory.
*/
GapBuffer();
/**
* Create a GapBuffer object that points to preallocated memory.
*
* The GapBuffer object will take over ownership of the allocated memory.
*
* @param buffer
* Pointer to buffer allocated by System::alloc_pages().
* @param buffer_size
* Size of the buffer allocated by System::alloc_pages().
* @param size
* Size of the data in the buffer.
*/
GapBuffer(uint8_t * buffer, size_t buffer_size, size_t size);
/**
* Destroy the GapBuffer object and free its memory.
*/
~GapBuffer();
/**
* Insert data into the gap buffer at the given position.
*
* @param position
* Position in the gap buffer to insert the code point at.
* @param data
* Pointer to the data to insert.
* @param length
* Length of the data to insert.
*/
void insert(size_t position, const uint8_t * data, size_t length);
/**
* Erase data from the gap buffer.
*
* @param position
* Position in the gap buffer to erase data from.
* @param length
* Length of the data to erase.
*/
void erase(size_t position, size_t length);
/**
* Erase all data from the gap buffer.
*/
void clear();
/**
* Get the size of the data stored within the buffer.
*/
size_t size() const
{
return m_size;
}
/**
* Get the memory address of a buffer location.
*
* @param offset
* The index into the gap buffer data.
*/
uint8_t * address(size_t offset) const
{
if (offset < m_gap_position)
{
return &m_buffer[offset];
}
else
{
return &m_buffer[offset + gap_size()];
}
}
/**
* Move buffer gap to the end of the buffer so the contents are congruent
* at the beginning of the buffer.
*/
void compact()
{
move_gap(m_size);
}
/**
* Copy a range of data from this GapBuffer to another.
*
* @param source_position
* Position in the gap buffer containing the data to copy.
* @param length
* Length of the data to copy.
* @param other
* Destination GapBuffer to copy the data into.
* @param destination_position
* Position in the destination GapBuffer to copy the data to.
*/
void copy_to(size_t source_position, size_t length, GapBuffer & other, size_t destination_position);
#ifdef ENABLE_TESTING
/**
* Get the contents of the gap buffer as a string.
*
* This makes a copy of the entire buffer in a std::string. This method is
* intended for use by unit tests.
*/
std::string get_string();
/**
* Replace the buffer contents with a string.
*/
void set_string(const std::string & s);
#endif
protected:
/** Address of the allocated memory buffer. */
uint8_t * m_buffer;
/** Size of the allocated memory buffer. */
size_t m_buffer_size;
/** Size of the data stored in the buffer. */
size_t m_size;
/** Index of the gap in the memory buffer. */
size_t m_gap_position;
/**
* Get the gap size.
*/
size_t gap_size() const
{
return m_buffer_size - m_size;
}
/**
* Verify that there is enough free space in the gap, and if not, grow the
* gap and move data.
*
* @param length
* The number of bytes that must be free.
*/
void ensure_free(size_t length);
/**
* Move the gap buffer gap position to the requested position.
*
* @param position
* New gap position. Must be <= m_size.
*/
void move_gap(size_t position);
};
#endif

View File

@ -1,11 +0,0 @@
#include "LineEndings.h"
static uint8_t lf[] = "\n";
static uint8_t crlf[] = "\r\n";
static uint8_t cr[] = "\r";
const Span LineEndings::spans[] = {
Span(lf, 1u),
Span(crlf, 2u),
Span(cr, 1u),
};

View File

@ -1,20 +0,0 @@
#ifndef LINEENDINGS_H
#define LINEENDINGS_H
#include "Span.h"
class LineEndings
{
public:
enum Type
{
LF,
CRLF,
CR,
COUNT,
};
static const Span spans[COUNT];
};
#endif