Skip to content

Commit

Permalink
-Added Print.saveAndClearBuf that temporarily clears the buffer but k…
Browse files Browse the repository at this point in the history
…eeps a handle to the content. This can be restored with a call to Print.restoreBuf. Useful when the print buffer is temporarily needed while already in use.

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@15084 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Peter Aronsson committed Feb 7, 2013
1 parent ecd9bc4 commit 10cbbda
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Compiler/Util/Print.mo
Expand Up @@ -41,6 +41,16 @@ encapsulated package Print
some other place. It also contains print functions for error messages, to be
used in interactive mode."

public function saveAndClearBuf "saves and clears content of buffer and return a handle to the saved buffer so it can be restored by restorBuf later on"
output Integer handle;
external "C";
end saveAndClearBuf;

public function restoreBuf
input Integer handle;
external "C";
end restoreBuf;

public function setBufSize
input Integer newSize;

Expand Down
13 changes: 13 additions & 0 deletions Compiler/runtime/Print_omc.cpp
Expand Up @@ -35,6 +35,19 @@ extern "C" {

#include "printimpl.c"

extern int Print_saveAndClearBuf()
{
// TODO: copy impl. from Print_rml.c
MMC_THROW();
return 0;
}

extern int Print_restoreBuf(int handle)
{
// TODO: copy impl. from Print_rml.c
MMC_THROW();
}

extern void Print_printErrorBuf(const char* str)
{
if (PrintImpl__printErrorBuf(str))
Expand Down
74 changes: 74 additions & 0 deletions Compiler/runtime/Print_rml.c
Expand Up @@ -46,6 +46,80 @@ void Print_5finit(void)
int errorCursize=0;
}

RML_BEGIN_LABEL(Print__saveAndClearBuf)
{
long freeHandle,foundHandle=0;

if (! savedBuffers) {
savedBuffers = (char**)malloc(MAXSAVEDBUFFERS*sizeof(char*));
if (!savedBuffers) {
fprintf(stderr, "Internal error allocating savedBuffers in Print.saveAndClearBuf\n");
RML_TAILCALLK(rmlFC);
}
memset(savedBuffers,0,MAXSAVEDBUFFERS);
}
if (! savedCurSize) {
savedCurSize = (long*)malloc(MAXSAVEDBUFFERS*sizeof(long*));
if (!savedCurSize) {
fprintf(stderr, "Internal error allocating savedCurSize in Print.saveAndClearBuf\n");
RML_TAILCALLK(rmlFC);
}
memset(savedCurSize,0,MAXSAVEDBUFFERS);
}
if (! savedNfilled) {
savedNfilled = (long*)malloc(MAXSAVEDBUFFERS*sizeof(long*));
if (!savedNfilled) {
fprintf(stderr, "Internal error allocating savedNfilled in Print.saveAndClearBuf\n");
RML_TAILCALLK(rmlFC);
}
memset(savedNfilled,0,MAXSAVEDBUFFERS);
}
for (freeHandle=0; freeHandle< MAXSAVEDBUFFERS; freeHandle++) {
if (savedBuffers[freeHandle]==0)
{
foundHandle = 1;
break;
}
}
if (!foundHandle) {
fprintf(stderr,"Internal error, can not save more than %d buffers, increase MAXSAVEDBUFFERS in printimpl.c\n",MAXSAVEDBUFFERS);
RML_TAILCALLK(rmlFC);
}
savedBuffers[freeHandle] = buf;
savedCurSize[freeHandle] = cursize;
savedNfilled[freeHandle] = nfilled;
buf = (char*)malloc(INITIAL_BUFSIZE*sizeof(char));
nfilled=0;
cursize=0;
rmlA0 = mk_icon(freeHandle);
RML_TAILCALLK(rmlSC);
}
RML_END_LABEL

RML_BEGIN_LABEL(Print__restoreBuf)
{
long handle = (long)RML_UNTAGFIXNUM(rmlA0);

if (handle < 0 || handle > MAXSAVEDBUFFERS-1) {
fprintf(stderr,"Internal error, hanlde %d out of range. Should be in [%d,&d]\n",handle,0,MAXSAVEDBUFFERS-1);
RML_TAILCALLK(rmlFC);
} else {
if (buf) { free(buf);}
buf = savedBuffers[handle];
cursize = savedCurSize[handle];
nfilled = savedNfilled[handle];
savedBuffers[handle] = 0;
savedCurSize[handle] = 0;
savedNfilled[handle] = 0;
if (buf == 0) {
fprintf(stderr,"Internal error, handle %d does not contain a valid buffer pointer\n",handle);
RML_TAILCALLK(rmlFC);
}
RML_TAILCALLK(rmlSC);
}
}
RML_END_LABEL

RML_BEGIN_LABEL(Print__setBufSize)
{
long newSize = (long)RML_UNTAGFIXNUM(rmlA0); // adrpo: do not use RML_IMMEDIATE as is just a cast to void! IS NOT NEEDED!
Expand Down
4 changes: 4 additions & 0 deletions Compiler/runtime/printimpl.c
Expand Up @@ -57,6 +57,10 @@ int cursize=0;
int errorNfilled=0;
int errorCursize=0;

char** savedBuffers=0;
long* savedCurSize;
long* savedNfilled;

static int increase_buffer(void)
{

Expand Down

0 comments on commit 10cbbda

Please sign in to comment.