From 629688d345cd377559e164a5217fe030cbd84d95 Mon Sep 17 00:00:00 2001 From: arphaman Date: Sun, 14 Jul 2013 14:07:24 +0100 Subject: added functions for unformatted write --- include/IO/Write.h | 31 ++++++++++++ lib/IO/CMakeLists.txt | 3 +- lib/IO/Write.cpp | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 include/IO/Write.h create mode 100644 lib/IO/Write.cpp diff --git a/include/IO/Write.h b/include/IO/Write.h new file mode 100644 index 0000000..f489a61 --- /dev/null +++ b/include/IO/Write.h @@ -0,0 +1,31 @@ +//===--- Write.h - Write implementation -------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBFLANG_IO_WRITE_H +#define LLVM_LIBFLANG_IO_WRITE_H + +#include +#include "Libflang.h" + +struct WriteController { + const char *FormatPtr; + size_t FormatLength; + int32_t unit; + uint32_t flags; +}; + +LIBFLANG_ABI void libflang_write_start(WriteController *Controller); +LIBFLANG_ABI void libflang_write_integer(WriteController *Controller, const void *Ptr, int32_t Size); +LIBFLANG_ABI void libflang_write_real(WriteController *Controller, const void *Ptr, int32_t Size); +LIBFLANG_ABI void libflang_write_complex(WriteController *Controller, const void *Ptr, int32_t Size); +LIBFLANG_ABI void libflang_write_logical(WriteController *Controller, const void *Ptr, int32_t Size); +LIBFLANG_ABI void libflang_write_character(WriteController *Controller, const char *Ptr, size_t Length); +LIBFLANG_ABI void libflang_write_end(WriteController *Controller); + +#endif diff --git a/lib/IO/CMakeLists.txt b/lib/IO/CMakeLists.txt index 8b13789..c1fa3a5 100644 --- a/lib/IO/CMakeLists.txt +++ b/lib/IO/CMakeLists.txt @@ -1 +1,2 @@ - +add_libflang_library(libflangIO + Write.cpp) diff --git a/lib/IO/Write.cpp b/lib/IO/Write.cpp new file mode 100644 index 0000000..5d1b11e --- /dev/null +++ b/lib/IO/Write.cpp @@ -0,0 +1,127 @@ +#include +#include +#include "IO/Write.h" + +#ifdef _MSC_VER +#define snprintf _snprintf +#endif + +static void InvalidSizeError(WriteController *Controller, int32_t Size) { + // FIXME +} + +template +static T getValue(const void *Ptr) { + return *(reinterpret_cast(Ptr)); +} + +template +static T getValue(const void *Ptr,size_t I) { + return (reinterpret_cast(Ptr))[I]; +} + +static int getIntValue(WriteController *Controller, const void *Ptr, int32_t Size) { + switch(Size) { + case 1: + return getValue(Ptr); + case 2: + return getValue(Ptr); + case 4: + return getValue(Ptr); + case 8: + return int(getValue(Ptr)); + default: + InvalidSizeError(Controller, Size); + break; + } + return 0; +} + +static FILE *getFilePtr(WriteController *Controller) { + return stdin; +} + +class Buffer { + enum { + kStorageSize = 128 + }; + size_t Offset; + char Storage[kStorageSize]; + +public: + Buffer() : Offset(0) {} + + template + void Print(const char *FMT, T Value) { + auto Remaining = size_t(kStorageSize)-Offset; + if(Remaining) + Offset += snprintf(Storage + Offset, Remaining, FMT, Value); + } + + void Write(WriteController *Controller) { + fwrite(Storage, 1, Offset, getFilePtr(Controller)); + } +}; + +template +static inline void SmallPrintf(WriteController *Controller, const char *FMT, T Value) { + Buffer Buf; + Buf.Print(FMT, Value); + Buf.Write(Controller); +} + +template +static inline void SmallPrintf(WriteController *Controller, const char *FMT1, T1 Value1, const char *FMT2, T2 Value2) { + Buffer Buf; + Buf.Print(FMT1, Value1); + Buf.Print(FMT2, Value2); + Buf.Write(Controller); +} + +LIBFLANG_ABI void libflang_write_start(WriteController *Controller) { + +} + +LIBFLANG_ABI void libflang_write_integer(WriteController *Controller, const void *Ptr, int32_t Size) { + SmallPrintf(Controller, "%i", getIntValue(Controller, Ptr, Size)); +} + +LIBFLANG_ABI void libflang_write_real(WriteController *Controller, const void *Ptr, int32_t Size) { + switch(Size) { + case 4: + SmallPrintf(Controller, "%g", getValue(Ptr)); + break; + case 8: + SmallPrintf(Controller, "%g", getValue(Ptr)); + break; + default: + InvalidSizeError(Controller, Size); + break; + } +} + +LIBFLANG_ABI void libflang_write_complex(WriteController *Controller, const void *Ptr, int32_t Size) { + switch(Size) { + case 4: + SmallPrintf(Controller, "(%g, ", getValue(Ptr), "%g)", getValue(Ptr,1)); + break; + case 8: + SmallPrintf(Controller, "(%g, ", getValue(Ptr), "%g)", getValue(Ptr,1)); + break; + default: + InvalidSizeError(Controller, Size); + break; + } +} + +LIBFLANG_ABI void libflang_write_logical(WriteController *Controller, const void *Ptr, int32_t Size) { + SmallPrintf(Controller, "%s", getIntValue(Controller, Ptr, Size) != 0? "true" : "false"); +} + +LIBFLANG_ABI void libflang_write_character(WriteController *Controller, const char *Ptr, size_t Length) { + fwrite(Ptr, 1, Length, getFilePtr(Controller)); +} + +LIBFLANG_ABI void libflang_write_end(WriteController *Controller) { + fwrite("\n", 1, 1, getFilePtr(Controller)); +} -- cgit v1.2.1