Skip to content

Commit

Permalink
Add iostream to CTL
Browse files Browse the repository at this point in the history
  • Loading branch information
jart committed Jun 29, 2024
1 parent 617ddfe commit 98e6846
Show file tree
Hide file tree
Showing 20 changed files with 1,531 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@ include third_party/nsync/mem/BUILD.mk # │ You can now use stdio
include libc/proc/BUILD.mk # │ You can now use threads
include libc/dlopen/BUILD.mk # │ You can now use processes
include libc/thread/BUILD.mk # │ You can finally call malloc()
include ctl/BUILD.mk #
include third_party/zlib/BUILD.mk #
include libc/stdio/BUILD.mk #
include tool/hello/BUILD.mk #
Expand Down Expand Up @@ -285,6 +284,7 @@ include third_party/ncurses/BUILD.mk # │
include third_party/readline/BUILD.mk #
include third_party/libunwind/BUILD.mk # |
include third_party/libcxxabi/BUILD.mk # |
include ctl/BUILD.mk #
include third_party/libcxx/BUILD.mk #
include third_party/openmp/BUILD.mk #
include third_party/double-conversion/BUILD.mk #
Expand Down
4 changes: 4 additions & 0 deletions ctl/BUILD.mk
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ CTL_A_CHECKS = \
CTL_A_DIRECTDEPS = \
LIBC_INTRIN \
LIBC_MEM \
LIBC_STDIO \
LIBC_STR \
THIRD_PARTY_GDTOA \
THIRD_PARTY_LIBCXXABI \
THIRD_PARTY_LIBUNWIND \

CTL_A_DEPS := $(call uniq,$(foreach x,$(CTL_A_DIRECTDEPS),$($(x))))

Expand Down
39 changes: 39 additions & 0 deletions ctl/ios.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// -*- mode:c++; indent-tabs-mode:nil; c-basic-offset:4; coding:utf-8 -*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
//
// Copyright 2024 Justine Alexandra Roberts Tunney
//
// Permission to use, copy, modify, and/or distribute this software for
// any purpose with or without fee is hereby granted, provided that the
// above copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
// PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

#include "ios.h"

namespace ctl {

ios::ios(FILE* file) : file_(file)
{
}

ios::~ios() = default;

ios&
ios::operator=(ios&& other) noexcept
{
if (this != &other) {
file_ = other.file_;
other.file_ = nullptr;
}
return *this;
}

} // namespace ctl
27 changes: 27 additions & 0 deletions ctl/ios.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// -*-mode:c++;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8-*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
#ifndef CTL_IOS_H_
#define CTL_IOS_H_
#include "ios_base.h"

struct FILE;

namespace ctl {

class ios : public ios_base
{
public:
explicit ios(FILE*);
virtual ~ios();

protected:
ios& operator=(ios&&) noexcept;
FILE* file_;

private:
ios(const ios&) = delete;
};

} // namespace ctl

#endif // CTL_IOS_H_
118 changes: 118 additions & 0 deletions ctl/ios_base.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// -*- mode:c++; indent-tabs-mode:nil; c-basic-offset:4; coding:utf-8 -*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
//
// Copyright 2024 Justine Alexandra Roberts Tunney
//
// Permission to use, copy, modify, and/or distribute this software for
// any purpose with or without fee is hereby granted, provided that the
// above copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
// PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

#include "ios_base.h"

namespace ctl {

ios_base::ios_base() : state_(goodbit), flags_(skipws | dec)
{
}

ios_base::~ios_base() = default;

ios_base::fmtflags
ios_base::flags() const
{
return static_cast<ios_base::fmtflags>(flags_);
}

ios_base::fmtflags
ios_base::flags(fmtflags fmtfl)
{
int old = flags_;
flags_ = fmtfl;
return static_cast<ios_base::fmtflags>(old);
}

ios_base::fmtflags
ios_base::setf(fmtflags fmtfl)
{
int old = flags_;
flags_ |= fmtfl;
return static_cast<ios_base::fmtflags>(old);
}

ios_base::fmtflags
ios_base::setf(fmtflags fmtfl, fmtflags mask)
{
int old = flags_;
flags_ = (flags_ & ~mask) | (fmtfl & mask);
return static_cast<ios_base::fmtflags>(old);
}

void
ios_base::unsetf(fmtflags mask)
{
flags_ &= ~mask;
}

ios_base::iostate
ios_base::rdstate() const
{
return static_cast<ios_base::iostate>(state_);
}

void
ios_base::clear(int state)
{
state_ = state;
}

void
ios_base::setstate(int state)
{
state_ |= state;
}

bool
ios_base::good() const
{
return state_ == goodbit;
}

bool
ios_base::eof() const
{
return (state_ & eofbit) != 0;
}

bool
ios_base::fail() const
{
return (state_ & (failbit | badbit)) != 0;
}

bool
ios_base::bad() const
{
return (state_ & badbit) != 0;
}

bool
ios_base::operator!() const
{
return fail();
}

ios_base::operator bool() const
{
return !fail();
}

} // namespace ctl
86 changes: 86 additions & 0 deletions ctl/ios_base.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// -*-mode:c++;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8-*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
#ifndef CTL_IOS_BASE_H_
#define CTL_IOS_BASE_H_

namespace ctl {

class ios_base
{
public:
typedef size_t streamsize;

enum iostate
{
goodbit = 0,
badbit = 1,
failbit = 2,
eofbit = 4
};

enum fmtflags
{
boolalpha = 1 << 0,
dec = 1 << 1,
fixed = 1 << 2,
hex = 1 << 3,
internal = 1 << 4,
left = 1 << 5,
oct = 1 << 6,
right = 1 << 7,
scientific = 1 << 8,
showbase = 1 << 9,
showpoint = 1 << 10,
showpos = 1 << 11,
skipws = 1 << 12,
unitbuf = 1 << 13,
uppercase = 1 << 14,
adjustfield = left | right | internal,
basefield = dec | oct | hex,
floatfield = scientific | fixed
};

enum openmode
{
app = 1 << 0,
binary = 1 << 1,
in = 1 << 2,
out = 1 << 3,
trunc = 1 << 4,
ate = 1 << 5
};

protected:
ios_base();
virtual ~ios_base();

int state_;
int flags_;

public:
fmtflags flags() const;
fmtflags flags(fmtflags);
fmtflags setf(fmtflags);
fmtflags setf(fmtflags, fmtflags);
void unsetf(fmtflags);

iostate rdstate() const;
void clear(int = goodbit);
void setstate(int);

bool good() const;
bool eof() const;
bool fail() const;
bool bad() const;

bool operator!() const;
explicit operator bool() const;

private:
ios_base(const ios_base&) = delete;
ios_base& operator=(const ios_base&) = delete;
};

} // namespace ctl

#endif // CTL_IOS_BASE_H_
1 change: 0 additions & 1 deletion ctl/is_convertible.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
#ifndef CTL_IS_CONVERTIBLE_H_
#define CTL_IS_CONVERTIBLE_H_

#include "ctl/integral_constant.h"
#include "ctl/void_t.h"

Expand Down
Loading

0 comments on commit 98e6846

Please sign in to comment.