From 0f8b26ad644bfe4d9b32e81414e0ea7ece286518 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Thu, 1 May 2025 15:35:19 -0700 Subject: [PATCH] stashing kte files --- .hgignore => .gitignore | 1 + kte/AppendBuffer.cc | 58 +++++++++++++++++++++++++++++++++++++++ kte/AppendBuffer.h | 30 ++++++++++++++++++++ kte/Makefile.in | 61 +++++++++++++++++++++++++++++++++++++++++ kte/main.cc | 25 +++++++++++++++++ 5 files changed, 175 insertions(+) rename .hgignore => .gitignore (92%) create mode 100644 kte/AppendBuffer.cc create mode 100644 kte/AppendBuffer.h create mode 100644 kte/Makefile.in create mode 100644 kte/main.cc diff --git a/.hgignore b/.gitignore similarity index 92% rename from .hgignore rename to .gitignore index 8ad9223..34b90fe 100644 --- a/.hgignore +++ b/.gitignore @@ -6,3 +6,4 @@ srm/srm libdirutils/dirutils-test Makefile libiniparser/iniparser-test +/build/ diff --git a/kte/AppendBuffer.cc b/kte/AppendBuffer.cc new file mode 100644 index 0000000..8db1de2 --- /dev/null +++ b/kte/AppendBuffer.cc @@ -0,0 +1,58 @@ +#include +#include + +#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(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 diff --git a/kte/AppendBuffer.h b/kte/AppendBuffer.h new file mode 100644 index 0000000..0cc5110 --- /dev/null +++ b/kte/AppendBuffer.h @@ -0,0 +1,30 @@ +/* + * abuf.cc + * 2024-05-30 + * kyle@imap.cc + * + * append buffers + */ + +#pragma once + +#include + +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 diff --git a/kte/Makefile.in b/kte/Makefile.in new file mode 100644 index 0000000..1118d87 --- /dev/null +++ b/kte/Makefile.in @@ -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 diff --git a/kte/main.cc b/kte/main.cc new file mode 100644 index 0000000..fe38d1d --- /dev/null +++ b/kte/main.cc @@ -0,0 +1,25 @@ +#include + +#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"; +} +