#include "config.h"
#include <stdlib.h>
#include <string.h>
#include "libgfortran.h"
#include "io.h"
#define CACHE_SIZE 3
static gfc_unit internal_unit, *unit_cache[CACHE_SIZE];
static int
pseudo_random (void)
{
static int x0 = 5341;
x0 = (22611 * x0 + 10) % 44071;
return x0;
}
static gfc_unit *
rotate_left (gfc_unit * t)
{
gfc_unit *temp;
temp = t->right;
t->right = t->right->left;
temp->left = t;
return temp;
}
static gfc_unit *
rotate_right (gfc_unit * t)
{
gfc_unit *temp;
temp = t->left;
t->left = t->left->right;
temp->right = t;
return temp;
}
static int
compare (int a, int b)
{
if (a < b)
return -1;
if (a > b)
return 1;
return 0;
}
static gfc_unit *
insert (gfc_unit * new, gfc_unit * t)
{
int c;
if (t == NULL)
return new;
c = compare (new->unit_number, t->unit_number);
if (c < 0)
{
t->left = insert (new, t->left);
if (t->priority < t->left->priority)
t = rotate_right (t);
}
if (c > 0)
{
t->right = insert (new, t->right);
if (t->priority < t->right->priority)
t = rotate_left (t);
}
if (c == 0)
internal_error ("insert(): Duplicate key found!");
return t;
}
void
insert_unit (gfc_unit * new)
{
new->priority = pseudo_random ();
g.unit_root = insert (new, g.unit_root);
}
static gfc_unit *
delete_root (gfc_unit * t)
{
gfc_unit *temp;
if (t->left == NULL)
return t->right;
if (t->right == NULL)
return t->left;
if (t->left->priority > t->right->priority)
{
temp = rotate_right (t);
temp->right = delete_root (t);
}
else
{
temp = rotate_left (t);
temp->left = delete_root (t);
}
return temp;
}
static gfc_unit *
delete_treap (gfc_unit * old, gfc_unit * t)
{
int c;
if (t == NULL)
return NULL;
c = compare (old->unit_number, t->unit_number);
if (c < 0)
t->left = delete_treap (old, t->left);
if (c > 0)
t->right = delete_treap (old, t->right);
if (c == 0)
t = delete_root (t);
return t;
}
static void
delete_unit (gfc_unit * old)
{
g.unit_root = delete_treap (old, g.unit_root);
}
gfc_unit *
find_unit (int n)
{
gfc_unit *p;
int c;
for (c = 0; c < CACHE_SIZE; c++)
if (unit_cache[c] != NULL && unit_cache[c]->unit_number == n)
{
p = unit_cache[c];
return p;
}
p = g.unit_root;
while (p != NULL)
{
c = compare (n, p->unit_number);
if (c < 0)
p = p->left;
if (c > 0)
p = p->right;
if (c == 0)
break;
}
if (p != NULL)
{
for (c = 0; c < CACHE_SIZE - 1; c++)
unit_cache[c] = unit_cache[c + 1];
unit_cache[CACHE_SIZE - 1] = p;
}
return p;
}
gfc_unit *
get_unit (int read_flag)
{
if (ioparm.internal_unit != NULL)
{
internal_unit.s =
open_internal (ioparm.internal_unit, ioparm.internal_unit_len);
internal_unit.flags.access = ACCESS_SEQUENTIAL;
internal_unit.flags.action = ACTION_READWRITE;
internal_unit.flags.form = FORM_FORMATTED;
internal_unit.flags.delim = DELIM_NONE;
return &internal_unit;
}
return find_unit (ioparm.unit);
}
int
is_internal_unit ()
{
return current_unit == &internal_unit;
}
void
init_units (void)
{
gfc_offset m, n;
gfc_unit *u;
int i;
if (options.stdin_unit >= 0)
{
u = get_mem (sizeof (gfc_unit));
memset (u, '\0', sizeof (gfc_unit));
u->unit_number = options.stdin_unit;
u->s = input_stream ();
u->flags.action = ACTION_READ;
u->flags.access = ACCESS_SEQUENTIAL;
u->flags.form = FORM_FORMATTED;
u->flags.status = STATUS_OLD;
u->flags.blank = BLANK_ZERO;
u->flags.position = POSITION_ASIS;
u->recl = options.default_recl;
u->endfile = NO_ENDFILE;
insert_unit (u);
}
if (options.stdout_unit >= 0)
{
u = get_mem (sizeof (gfc_unit));
memset (u, '\0', sizeof (gfc_unit));
u->unit_number = options.stdout_unit;
u->s = output_stream ();
u->flags.action = ACTION_WRITE;
u->flags.access = ACCESS_SEQUENTIAL;
u->flags.form = FORM_FORMATTED;
u->flags.status = STATUS_OLD;
u->flags.blank = BLANK_ZERO;
u->flags.position = POSITION_ASIS;
u->recl = options.default_recl;
u->endfile = AT_ENDFILE;
insert_unit (u);
}
if (options.stderr_unit >= 0)
{
u = get_mem (sizeof (gfc_unit));
memset (u, '\0', sizeof (gfc_unit));
u->unit_number = options.stderr_unit;
u->s = error_stream ();
u->flags.action = ACTION_WRITE;
u->flags.access = ACCESS_SEQUENTIAL;
u->flags.form = FORM_FORMATTED;
u->flags.status = STATUS_OLD;
u->flags.blank = BLANK_ZERO;
u->flags.position = POSITION_ASIS;
u->recl = options.default_recl;
u->endfile = AT_ENDFILE;
insert_unit (u);
}
g.max_offset = 0;
for (i=0; i < sizeof(g.max_offset) * 8 - 1; i++)
g.max_offset = g.max_offset + ((gfc_offset) 1 << i);
}
int
close_unit (gfc_unit * u)
{
int i, rc;
for (i = 0; i < CACHE_SIZE; i++)
if (unit_cache[i] == u)
unit_cache[i] = NULL;
rc = (u->s == NULL) ? 0 : sclose (u->s) == FAILURE;
delete_unit (u);
free_mem (u);
return rc;
}
void
close_units (void)
{
while (g.unit_root != NULL)
close_unit (g.unit_root);
}