stashing kte files

This commit is contained in:
Kyle Isom 2025-05-01 15:35:19 -07:00
parent 59dddd91da
commit 0f8b26ad64
5 changed files with 175 additions and 0 deletions

View File

@ -6,3 +6,4 @@ srm/srm
libdirutils/dirutils-test libdirutils/dirutils-test
Makefile Makefile
libiniparser/iniparser-test libiniparser/iniparser-test
/build/

58
kte/AppendBuffer.cc Normal file
View File

@ -0,0 +1,58 @@
#include <cassert>
#include <iostream>
#include "AppendBuffer.h"
namespace kte {
void
AppendBuffer::Append(const char *s, int slen)
{
char *nc = nullptr;
int sz = this->len + slen;
assert(this->len + slen > this->len);
if (sz >= this->cap) {
std::cerr << "AppendBuffer: resizing from " << this->cap << "b to ";
while (sz > this->cap) {
if (this->cap == 0) {
this->cap = sizeof(uintptr_t);
} else {
this->cap <<= 1;
}
}
std::cerr << this->cap << "bytes.\n";
nc = new char[this->cap];
std::copy_n(this->b, this->len, nc);
delete[] this->b;
this->b = nc;
}
std::copy_n(s, slen, this->b+len);
this->len += slen;
}
void
AppendBuffer::Append(const std::string& s)
{
return this->Append(s.c_str(), static_cast<int>(s.length()));
}
void
AppendBuffer::Append(const char ch)
{
return this->Append(&ch, 1);
}
std::ostream& operator<<(std::ostream &stream, const AppendBuffer buf) {
return stream << "AppendBuffer<" << buf.cap << ", " << buf.len << "> " << buf.b;
}
} // namespace kte

30
kte/AppendBuffer.h Normal file
View File

@ -0,0 +1,30 @@
/*
* abuf.cc
* 2024-05-30
* kyle@imap.cc
*
* append buffers
*/
#pragma once
#include <string>
namespace kte {
class AppendBuffer {
public:
AppendBuffer() : b(nullptr), len(0), cap(0) {};
void Append(const char *, int);
void Append(const std::string& s);
void Append(const char ch);
friend std::ostream &operator<<(std::ostream& os, AppendBuffer buf);
private:
char *b;
int len;
int cap;
};
} // namespace kte

61
kte/Makefile.in Normal file
View File

@ -0,0 +1,61 @@
VERSION := 0.0.1
CXX ?= clang++
TARGET := kte
OBJS := main.o AppendBuffer.o
LIBS :=
PREFIX ?= $PREFIX
MANDIR ?= $MANDIR
CXXFLAGS += -Wall -Wextra -pedantic -Wshadow -Werror -std=c++17 -g
CXXFLAGS += -Wno-unused-function
CXXFLAGS += -DKE_VERSION="\"$(TARGET) version $(VERSION)\""
CXXFLAGS += OS_CFLAGS
all: $(TARGET)
./$(TARGET)
clean:
-rm -f .?*.* *.core *.o *.html tags $(TARGET) $(OBJS)
-rm -rf security
-rm -rf $(TARGET)-$(VERSION)
-rm -f $(TARGET)-$(VERSION).tgz
$(TARGET): $(OBJS)
${CXX} -o $(TARGET) ${CXXFLAGS} ${LDFLAGS} $(LIBS) $(OBJS)
install: $(TARGET)
install -m 0755 $(TARGET) $(PREFIX)/bin/$(TARGET)
install -m 0755 -d $(MANDIR)/man1
install -m 0444 $(TARGET).1 $(MANDIR)/man1/$(TARGET).1
uninstall:
-rm -f $(PREFIX)/bin/$(TARGET)
-rm -f $(MANDIR)/man1/$(TARGET).1
lint:
make clean && scan-build make
# -mkdir security
# -rats -w 3 $(TARGET).[ch] > security/rats.out
# -lint -fhrs $(TARGET).c > security/lint.out
# -splint +posixlib $(TARGET).[ch] > security/splint.out
dist: clean
-mkdir $(TARGET)-$(VERSION)
-cp * $(TARGET)-$(VERSION)
-cd $(TARGET)-$(VERSION) && make distclean && cd ..
-tar czf $(TARGET)-$(VERSION).tgz $(TARGET)-$(VERSION)
distclean: clean
-rm -f Makefile
htmldoc:
-mandoc -Thtml $(TARGET).1 > $(TARGET).1.html
tags:
ctags *.cc *.h
.cc.o:
$(CXX) -c ${CXXFLAGS} $?
.PHONY: clean all install lint uninstall dist distclean htmldoc tags

25
kte/main.cc Normal file
View File

@ -0,0 +1,25 @@
#include <iostream>
#include "AppendBuffer.h"
using namespace std;
static const string TestString2 = " This is a test of the self-destruct mechanism.";
int
main()
{
kte::AppendBuffer buf;
buf.Append('*');
cout << "append buffer\n";
buf.Append("Hello, world.");
cout << "append buffer 2\n";
buf.Append(TestString2);
buf.Append('*');
cout << buf << "\n";
}