Subversion Repositories shark

Rev

Blame | Last modification | View Log | RSS feed

/*
 * Project: S.Ha.R.K.
 *
 * Coordinators:
 *   Giorgio Buttazzo    <giorgio@sssup.it>
 *   Paolo Gai           <pj@gandalf.sssup.it>
 *
 * Authors     :
 *   Paolo Gai           <pj@gandalf.sssup.it>
 *   (see the web pages for full authors list)
 *
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
 *
 * http://www.sssup.it
 * http://retis.sssup.it
 * http://shark.sssup.it
 */


/**
 ------------
 CVS :        $Id: dosfs.c,v 1.1 2004-07-05 14:17:17 pj Exp $

 File:        $File$
 Revision:    $Revision: 1.1 $
 Last update: $Date: 2004-07-05 14:17:17 $
 ------------
**/


/*
 * Copyright (C) 2001 Paolo Gai
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */


#include <kernel/kern.h>
#include <ll/i386/x-dos.h>
#include <ctype.h>
#include "crunch.h"


#define FILEBUF_DIM 1000000

/*
 *
 * Read functions
 *
 */


/* This is the buffer used by read_myfile */
char myfilebuf[FILEBUF_DIM];

/* This is the number of bytes read by read_myfile */
int myfilebuf_length;

/* This function read myfile.txt (up to 1000 chars) */
void read_myfile(char *name)
{
  /* DOS file descriptor */
  DOS_FILE *f;

  /* Error code */
  int err;

  /* open the DOS file for reading  (you can specify only "r" or "w") */
  f = DOS_fopen(name,"r");

  /* check for open errors */
  if (!f) {
    /* error!! */
    err = DOS_error();

    /* note that if you call DOS_error() here, it return 0!!! */
    cprintf("Error %d opening %s...\n", err, name);
    myfilebuf_length = 0;
    return;
  }

  /* read up to 1000 chars */
  myfilebuf_length = DOS_fread(&myfilebuf,1,FILEBUF_DIM,f);

  /* check for errors */
  err = DOS_error();

  cprintf("Read %d bytes from %s...\n", myfilebuf_length, name);

  if (err) {
    cprintf("Error %d reading %s...\n", err, name);
    myfilebuf_length = 0;
   /* there is not return because I want to close the file! */
  }

  /* Close the file */
  DOS_fclose(f);
}

/*
 *
 * Parsing functions
 *
 */



int geti(char *b, int *pos, int maxpos);

/*
 * This function crunches the buffer that contains the configuration file.
 * m is the maximum size of the t array...
 */

void crunch_file(struct task_struct *t, int *curr_table_dim, int m)
{
  int curr_filepos = 0;
  int max_task_pos;
  int i = 0;

  // read the number of tasks into the data file
  max_task_pos = geti(myfilebuf, &curr_filepos, myfilebuf_length);
  if (max_task_pos == -1) {
    cprintf("Error parsing configuration file...\n");
    *curr_table_dim = 0;
  }

  for (i=0; i<max_task_pos; i++) {
      // read the seven datas
      t[i].index      = i;
      t[i].time       = geti(myfilebuf, &curr_filepos, myfilebuf_length);
      t[i].type       = geti(myfilebuf, &curr_filepos, myfilebuf_length);
      t[i].reldline   = geti(myfilebuf, &curr_filepos, myfilebuf_length);
      t[i].wcet       = geti(myfilebuf, &curr_filepos, myfilebuf_length);
      t[i].iterations = geti(myfilebuf, &curr_filepos, myfilebuf_length);
      t[i].value      = geti(myfilebuf, &curr_filepos, myfilebuf_length);
      t[i].penalty    = geti(myfilebuf, &curr_filepos, myfilebuf_length);

      // check the last one for an error
      if (t[i].penalty == -1) {
        cprintf("Error parsing task %d...\n",i);
        break;
      }
  }

  *curr_table_dim = i;
}

/* the buffer b is scannedc to search for numbers
   at the first non-number the function stops */

int geti(char *b, int *pos, int maxpos)
{
  int res = 0;

  // skip first chars
  while (!isdigit(b[*pos])) {
    (*pos)++;
    if (*pos == maxpos) return -1;  // Error!!!
  }


  // read the numbers
  do {
    res = (res * 10) + b[*pos] - '0';
    (*pos)++;
  } while (isdigit(b[*pos]));

  return res;
}