Пример #1
0
static int r3d_read_header(AVFormatContext *s)
{
    R3DContext *r3d = s->priv_data;
    Atom atom;
    int ret;

    if (read_atom(s, &atom) < 0) {
        av_log(s, AV_LOG_ERROR, "error reading atom\n");
        return -1;
    }
    if (atom.tag == MKTAG('R','E','D','1')) {
        if ((ret = r3d_read_red1(s)) < 0) {
            av_log(s, AV_LOG_ERROR, "error parsing 'red1' atom\n");
            return ret;
        }
    } else {
        av_log(s, AV_LOG_ERROR, "could not find 'red1' atom\n");
        return -1;
    }

    /* we cannot create the audio stream now because we do not know the
     * sample rate */
    if (r3d->audio_channels)
        s->ctx_flags |= AVFMTCTX_NOHEADER;

    s->internal->data_offset = avio_tell(s->pb);
    av_log(s, AV_LOG_TRACE, "data offset %#"PRIx64"\n", s->internal->data_offset);
    if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL))
        return 0;
    // find REOB/REOF/REOS to load index
    avio_seek(s->pb, avio_size(s->pb)-48-8, SEEK_SET);
    if (read_atom(s, &atom) < 0)
        av_log(s, AV_LOG_ERROR, "error reading end atom\n");

    if (atom.tag != MKTAG('R','E','O','B') &&
        atom.tag != MKTAG('R','E','O','F') &&
        atom.tag != MKTAG('R','E','O','S'))
        goto out;

    r3d_read_reos(s);

    if (r3d->rdvo_offset) {
        avio_seek(s->pb, r3d->rdvo_offset, SEEK_SET);
        if (read_atom(s, &atom) < 0)
            av_log(s, AV_LOG_ERROR, "error reading 'rdvo' atom\n");
        if (atom.tag == MKTAG('R','D','V','O')) {
            if (r3d_read_rdvo(s, &atom) < 0)
                av_log(s, AV_LOG_ERROR, "error parsing 'rdvo' atom\n");
        }
    }

 out:
    avio_seek(s->pb, s->internal->data_offset, SEEK_SET);
    return 0;
}
Пример #2
0
Файл: r3d.c Проект: andoma/libav
static int r3d_read_header(AVFormatContext *s, AVFormatParameters *ap)
{
    R3DContext *r3d = s->priv_data;
    Atom atom;
    int ret;

    if (read_atom(s, &atom) < 0) {
        av_log(s, AV_LOG_ERROR, "error reading atom\n");
        return -1;
    }
    if (atom.tag == MKTAG('R','E','D','1')) {
        if ((ret = r3d_read_red1(s)) < 0) {
            av_log(s, AV_LOG_ERROR, "error parsing 'red1' atom\n");
            return ret;
        }
    } else {
        av_log(s, AV_LOG_ERROR, "could not find 'red1' atom\n");
        return -1;
    }

    s->data_offset = avio_tell(s->pb);
    av_dlog(s, "data offset %#llx\n", s->data_offset);
    if (!s->pb->seekable)
        return 0;
    // find REOB/REOF/REOS to load index
    avio_seek(s->pb, avio_size(s->pb)-48-8, SEEK_SET);
    if (read_atom(s, &atom) < 0)
        av_log(s, AV_LOG_ERROR, "error reading end atom\n");

    if (atom.tag != MKTAG('R','E','O','B') &&
        atom.tag != MKTAG('R','E','O','F') &&
        atom.tag != MKTAG('R','E','O','S'))
        goto out;

    r3d_read_reos(s);

    if (r3d->rdvo_offset) {
        avio_seek(s->pb, r3d->rdvo_offset, SEEK_SET);
        if (read_atom(s, &atom) < 0)
            av_log(s, AV_LOG_ERROR, "error reading 'rdvo' atom\n");
        if (atom.tag == MKTAG('R','D','V','O')) {
            if (r3d_read_rdvo(s, &atom) < 0)
                av_log(s, AV_LOG_ERROR, "error parsing 'rdvo' atom\n");
        }
    }

 out:
    avio_seek(s->pb, s->data_offset, SEEK_SET);
    return 0;
}
Пример #3
0
atom_t* read_atom(scanner_t *scan){
	slice_t slice;
	int c = scan_while_func(scan, NULL, isspace);
	
	while (c == ';'){
		scan_until(scan, NULL, '\n');
		c = scan_peek(scan);
	}
	
	if (c == EOF) {
		return nil_atom();
	} else if (c == '(') {
		return read_list(scan);
	} else if (c == '\'') {
		// Quote
		scan_one_of(scan, '\'');
		atom_t *content = read_atom(scan);
		return pair_atom_alloc(sym_atom_alloc("quote"), pair_atom_alloc(content, nil_atom()));
	} else if (c == '"') {
		// String
		scan_one_of(scan, '"');
		scan_until(scan, &slice, '"');
		return str_atom_alloc(slice.ptr);
	} else if ( isdigit(c) ) {
		// Number
		scan_while_func(scan, &slice, isdigit);
		int64_t value = strtoll(slice.ptr, NULL, 10);
		free(slice.ptr);
		return num_atom_alloc(value);
	} else {
		return read_sym(scan);
	}
}
Пример #4
0
static int r3d_read_packet(AVFormatContext *s, AVPacket *pkt)
{
    R3DContext *r3d = s->priv_data;
    Atom atom;
    int err = 0;

    while (!err) {
        if (read_atom(s, &atom) < 0) {
            err = -1;
            break;
        }
        switch (atom.tag) {
        case MKTAG('R','E','D','V'):
            if (s->streams[0]->discard == AVDISCARD_ALL)
                goto skip;
            if (!(err = r3d_read_redv(s, pkt, &atom)))
                return 0;
            break;
        case MKTAG('R','E','D','A'):
            if (!r3d->audio_channels)
                return -1;
            if (s->nb_streams >= 2 && s->streams[1]->discard == AVDISCARD_ALL)
                goto skip;
            if (!(err = r3d_read_reda(s, pkt, &atom)))
                return 0;
            break;
        default:
        skip:
            avio_skip(s->pb, atom.size-8);
        }
    }
    return err;
}
Пример #5
0
atom_t* read_test_code(char *code){
	atom_t *atom;
	
	scanner_t scan = scan_open_string(code);
	atom = read_atom(&scan);
	scan_close(&scan);
	
	return atom;
}
Пример #6
0
bool MDTRA_Compact_PDB_File :: load( int threadnum, unsigned int format, const char *filename, int streamFlags )
{
	FILE *fp = NULL;
	char linebuf[82];
	int line_c = 0;
	int residue_c = 0;

	if (fopen_s( &fp, filename, "r" )) {
		return false;
	}

	reset();

	int fIgnoreHetatm = streamFlags & STREAM_FLAG_IGNORE_HETATM;
	int fIgnoreSolvent = streamFlags & STREAM_FLAG_IGNORE_SOLVENT;

	//count ATOM lines
	while ( !feof(fp) ) {
		linebuf[0] = 0;
		if (!fgets( linebuf, 82, fp )) break;
		if (!_strnicmp( linebuf, "ATOM  ", 6 ) || ( !fIgnoreHetatm && !_strnicmp( linebuf, "HETATM", 6 ) )) line_c++;
	}

	//allocate atoms
	m_pAtoms = (MDTRA_Compact_PDB_Atom*)UTIL_AlignedMalloc(line_c * sizeof(MDTRA_Compact_PDB_Atom));
	if (!m_pAtoms) {
		fclose( fp );
		reset();
		return false;
	}
	memset( m_pAtoms, 0, line_c * sizeof(MDTRA_Compact_PDB_Atom) );

	//load ATOM lines
	rewind(fp);
	while ( !feof(fp) ) {
		linebuf[0] = 0;
		if (!fgets( linebuf, 82, fp )) break;
		if (!_strnicmp( linebuf, "ATOM  ", 6 ) || 
			( !fIgnoreHetatm && !_strnicmp( linebuf, "HETATM", 6 ) )) {
			//parse atom
			if (!read_atom( threadnum, format, linebuf, m_pAtoms + m_iNumAtoms, residue_c )) {
				fclose( fp );
				reset();
				return false;
			}
			if ( fIgnoreSolvent && (m_pAtoms[m_iNumAtoms].atomFlags & (PDB_FLAG_WATER|PDB_FLAG_ION) ))
				continue;
			m_iNumAtoms++;
		}
	}
	fclose( fp );

	set_flags();

	return true;
}
Пример #7
0
checker_sexpr_t
checker_read_sexpr(int ind)
{
  int c;
  checker_sexpr_t cur = 0, *plast = &cur, p, q;

  c = getc_unlocked(f_arr[ind]);
  while (c != EOF && isspace(c)) c = getc_unlocked(f_arr[ind]);
  if (c == EOF && ferror(f_arr[ind])) {
    fatal_CF(_("%s: input error"), gettext(f_arr_names[ind]));
  }
  if (c != EOF && c < ' ') {
    fatal_read(ind, _("Invalid control character with code %d"), c);
  }

  if (c == '(') {
    while (1) {
      c = getc_unlocked(f_arr[ind]);
      while (c != EOF && isspace(c)) c = getc_unlocked(f_arr[ind]);
      if (c == EOF && ferror(f_arr[ind])) {
        fatal_CF("%s: input error", gettext(f_arr_names[ind]));
      }
      if (c != EOF && c < ' ') {
        fatal_read(ind, _("Invalid control character with code %d"), c);
      }
      if (c == EOF) {
        fatal_read(ind, _("Unexpected EOF"));
      }
      if (c == ')') break;
      ungetc(c, f_arr[ind]);
      q = checker_read_sexpr(ind);
      p = xcalloc(1, sizeof(*p));
      p->p.kind = CHECKER_SEXPR_PAIR;
      p->p.head = q;
      *plast = p;
      plast = &p->p.tail;
    }
    return cur;
  }

  if (c == EOF && ferror(f_arr[ind])) {
    fatal_CF(_("%s: input error"), gettext(f_arr_names[ind]));
  }
  if (c == EOF) {
    fatal_read(ind, _("Unexpected EOF"));
  }
  ungetc(c, f_arr[ind]);
  p = xcalloc(1, sizeof(*p));
  p->a.kind = CHECKER_SEXPR_ATOM;
  p->a.value = read_atom(ind);
  return p;
}
Пример #8
0
VALUE read_pid(unsigned char **pData) {
  if(read_1(pData) != ERL_PID) {
    rb_raise(rb_eStandardError, "Invalid Type, not a pid");
  }
  
  VALUE node = read_atom(pData);
  VALUE id = INT2NUM(read_4(pData));
  VALUE serial = INT2NUM(read_4(pData));
  VALUE creation = INT2FIX(read_1(pData));
  
  VALUE pid_class = rb_const_get(mErlectricity, rb_intern("Pid"));
  return rb_funcall(pid_class, rb_intern("new"), 4, node, id, serial, creation);
}
Пример #9
0
const Sexp::Cell *Sexp::read(char **begin, const char *end) {
  comment(begin, end);
  if (*begin >= end) {
    return 0;
  }
  const int r = next_token(begin, end, '(');
  if (r == 1) {
    return read_car(begin, end);
  } else if (r == 0) {
    return read_atom(begin, end);
  }
  return 0;
}
Пример #10
0
atom_t* read_list(scanner_t *scan){
	int c;
	atom_t* list_start_atom = pair_atom_alloc( nil_atom(), nil_atom() );
	atom_t* current_atom = list_start_atom;
	
	scan_one_of(scan, '(');
	// Check if we got an empty list
	c = scan_while_func(scan, NULL, isspace);
	if (c == ')'){
		scan_one_of(scan, ')');
		return nil_atom();
	}
	
	while (true) {
		current_atom->first = read_atom(scan);
		
		c = scan_while_func(scan, NULL, isspace);
		if (c == ')') {
			scan_one_of(scan, ')');
			current_atom->rest = nil_atom();
			break;
		} else if (c == '.') {
			scan_one_of(scan, '.');
			current_atom->rest = read_atom(scan);
			scan_while_func(scan, NULL, isspace);
			scan_one_of(scan, ')');
			break;
		} else if (c == EOF) {
			return nil_atom();
		}
		
		current_atom->rest = pair_atom_alloc( nil_atom(), nil_atom() );
		current_atom = current_atom->rest;
	}
	
	return list_start_atom;
}
Пример #11
0
VALUE read_any_raw(unsigned char **pData) {
  switch(peek_1(pData)) {
    case ERL_SMALL_INT:
      return read_small_int(pData);
      break;
    case ERL_INT:
      return read_int(pData);
      break;
    case ERL_FLOAT:
      return read_float(pData);
      break;
    case ERL_ATOM:
      return read_atom(pData);
      break;
    case ERL_PID:
      return read_pid(pData);
      break;
    case ERL_SMALL_TUPLE:
      return read_small_tuple(pData);
      break;
    case ERL_LARGE_TUPLE:
      return read_large_tuple(pData);
      break;
    case ERL_NIL:
      return read_nil(pData);
      break;
    case ERL_STRING:
      return read_string(pData);
      break;
    case ERL_LIST:
      return read_list(pData);
      break;
    case ERL_BIN:
      return read_bin(pData);
      break;
    case ERL_SMALL_BIGNUM:
      return read_small_bignum(pData);
      break;
    case ERL_LARGE_BIGNUM:
      return read_large_bignum(pData);
      break;
    case ERL_NEW_REF:
      return read_new_reference(pData);
      break;
  }
  return Qnil;
}
Пример #12
0
VALUE read_new_reference(unsigned char **pData) {
  if(read_1(pData) != ERL_NEW_REF) {
    rb_raise(rb_eStandardError, "Invalid Type, not a new-style reference");
  }
  
  int size = read_2(pData);
  VALUE node = read_atom(pData);
  VALUE creation = INT2FIX(read_1(pData));
  
  VALUE id = rb_ary_new2(size);
  int i;
  for(i = 0; i < size; ++i) {
    rb_ary_store(id, i, INT2NUM(read_4(pData)));
  }
  
  VALUE newref_class = rb_const_get(mErlectricity, rb_intern("NewReference"));
  return rb_funcall(newref_class, rb_intern("new"), 3, node, creation, id);
}
Пример #13
0
VALUE read_any_raw(unsigned char **pData, VALUE forceToEncoding) {
  switch(peek_1(pData)) {
    case ERL_SMALL_INT:
      return read_small_int(pData);
      break;
    case ERL_INT:
      return read_int(pData);
      break;
    case ERL_FLOAT:
      return read_float(pData);
      break;
    case ERL_ATOM:
      return read_atom(pData);
      break;
    case ERL_SMALL_TUPLE:
      return read_small_tuple(pData, forceToEncoding);
      break;
    case ERL_LARGE_TUPLE:
      return read_large_tuple(pData, forceToEncoding);
      break;
    case ERL_NIL:
      return read_nil(pData);
      break;
    case ERL_STRING:
      return read_string(pData, forceToEncoding);
      break;
    case ERL_LIST:
      return read_list(pData, forceToEncoding);
      break;
    case ERL_BIN:
      return read_bin(pData, forceToEncoding);
      break;
    case ERL_SMALL_BIGNUM:
      return read_small_bignum(pData);
      break;
    case ERL_LARGE_BIGNUM:
      return read_large_bignum(pData);
      break;
  }
  return Qnil;
}
Пример #14
0
static void read_ter_db_file(const char *fn,
                             int *ntbptr, t_hackblock **tbptr,
                             gpp_atomtype_t atype)
{
    char         filebase[STRLEN], *ptr;
    FILE        *in;
    char         header[STRLEN], buf[STRLEN], line[STRLEN];
    t_hackblock *tb;
    int          i, j, n, ni, kwnr, nb, maxnb, nh;

    fflib_filename_base(fn, filebase, STRLEN);
    /* Remove the C/N termini extension */
    ptr = strrchr(filebase, '.');
    if (ptr != nullptr)
    {
        ptr[0] = '\0';
    }

    in = fflib_open(fn);

    tb    = *tbptr;
    nb    = *ntbptr - 1;
    maxnb = 0;
    kwnr  = NOTSET;
    get_a_line(in, line, STRLEN);
    while (!feof(in))
    {
        if (get_header(line, header))
        {
            /* this is a new block, or a new keyword */
            kwnr = find_kw(header);

            if (kwnr == NOTSET)
            {
                nb++;
                /* here starts a new block */
                if (nb >= maxnb)
                {
                    maxnb = nb + 100;
                    srenew(tb, maxnb);
                }
                clear_t_hackblock(&tb[nb]);
                tb[nb].name     = gmx_strdup(header);
                tb[nb].filebase = gmx_strdup(filebase);
            }
        }
        else
        {
            if (nb < 0)
            {
                gmx_fatal(FARGS, "reading termini database: "
                          "directive expected before line:\n%s\n"
                          "This might be a file in an old format.", line);
            }
            /* this is not a header, so it must be data */
            if (kwnr >= ebtsNR)
            {
                /* this is a hack: add/rename/delete atoms */
                /* make space for hacks */
                if (tb[nb].nhack >= tb[nb].maxhack)
                {
                    tb[nb].maxhack += 10;
                    srenew(tb[nb].hack, tb[nb].maxhack);
                }
                nh = tb[nb].nhack;
                clear_t_hack(&(tb[nb].hack[nh]));
                for (i = 0; i < 4; i++)
                {
                    tb[nb].hack[nh].a[i] = nullptr;
                }
                tb[nb].nhack++;

                /* get data */
                n = 0;
                if (kwnr == ekwRepl || kwnr == ekwDel)
                {
                    if (sscanf(line, "%s%n", buf, &n) != 1)
                    {
                        gmx_fatal(FARGS, "Reading Termini Database '%s': "
                                  "expected atom name on line\n%s", fn, line);
                    }
                    tb[nb].hack[nh].oname = gmx_strdup(buf);
                    /* we only replace or delete one atom at a time */
                    tb[nb].hack[nh].nr = 1;
                }
                else if (kwnr == ekwAdd)
                {
                    read_ab(line, fn, &(tb[nb].hack[nh]));
                    get_a_line(in, line, STRLEN);
                }
                else
                {
                    gmx_fatal(FARGS, "unimplemented keyword number %d (%s:%d)",
                              kwnr, __FILE__, __LINE__);
                }
                if (kwnr == ekwRepl || kwnr == ekwAdd)
                {
                    snew(tb[nb].hack[nh].atom, 1);
                    read_atom(line+n, kwnr == ekwAdd,
                              &tb[nb].hack[nh].nname, tb[nb].hack[nh].atom, atype,
                              &tb[nb].hack[nh].cgnr);
                    if (tb[nb].hack[nh].nname == nullptr)
                    {
                        if (tb[nb].hack[nh].oname != nullptr)
                        {
                            tb[nb].hack[nh].nname = gmx_strdup(tb[nb].hack[nh].oname);
                        }
                        else
                        {
                            gmx_fatal(FARGS, "Reading Termini Database '%s': don't know which name the new atom should have on line\n%s", fn, line);
                        }
                    }
                }
            }
            else if (kwnr >= 0 && kwnr < ebtsNR)
            {
                /* this is bonded data: bonds, angles, dihedrals or impropers */
                srenew(tb[nb].rb[kwnr].b, tb[nb].rb[kwnr].nb+1);
                n = 0;
                for (j = 0; j < btsNiatoms[kwnr]; j++)
                {
                    if (sscanf(line+n, "%s%n", buf, &ni) == 1)
                    {
                        tb[nb].rb[kwnr].b[tb[nb].rb[kwnr].nb].a[j] = gmx_strdup(buf);
                    }
                    else
                    {
                        gmx_fatal(FARGS, "Reading Termini Database '%s': expected %d atom names (found %d) on line\n%s", fn, btsNiatoms[kwnr], j-1, line);
                    }
                    n += ni;
                }
                for (; j < MAXATOMLIST; j++)
                {
                    tb[nb].rb[kwnr].b[tb[nb].rb[kwnr].nb].a[j] = nullptr;
                }
                strcpy(buf, "");
                sscanf(line+n, "%s", buf);
                tb[nb].rb[kwnr].b[tb[nb].rb[kwnr].nb].s = gmx_strdup(buf);
                tb[nb].rb[kwnr].nb++;
            }
            else
            {
                gmx_fatal(FARGS, "Reading Termini Database: Expecting a header at line\n"
                          "%s", line);
            }
        }
        get_a_line(in, line, STRLEN);
    }
    nb++;
    srenew(tb, nb);

    gmx_ffclose(in);

    *ntbptr = nb;
    *tbptr  = tb;
}
Пример #15
0
void Linker::read_linked_segs(FILE * f, aUINT16 atom_table_length, 
                              FILE * pFtarg, int filei)
{                             //  reads the linked segments for one PROCEDURE 
  aINT32      proc_code_length;
  aUINT16      clause_code_length;
  CODE       buf[6];
  aUINT16     last_clause, first_clause;
  aINT32      start_of_proc, end_of_proc;  // changed from long
  aINT16     zero = 0;
  char       op;   
  
  start_of_proc = ftell(pFtarg);
  
  /* build the procedure prefix segment
     
     proc_code_length
     name
     arity

     */
  read_l_atoms(f, atom_table_length, filei);    // Read the local atom table 
  
  if (!read_uint16(&clause_code_length, f))      // code length 
    aborteof(aS("linked segment length"));
#ifdef BUG_LINK
  fprintf(lout, "clause_length = %u, local_atom_table_length = %u\n", clause_code_length, atom_table_length);
#endif
  //  ucFWRITE(&zero, sizeof(aINT16), 1, pFtarg);      // reserve this 
  // ucFWRITE(&zero, sizeof(intC), 1, pFtarg);      // reserve this  ray
  ucFWRITE(&zero, sizeof(aINT32), 1, pFtarg);      // no ray, should be int32, intC might be 64
  
  if (!read_uint16(&last_clause, f))             // last clause marker 
    aborteof(aS("linked segment clause marker"));
  
  read_atom(f, pFtarg);                         // functor 
  
  if (0 == fread(buf, sizeof(aINT16), 1, f))     // arity 
    aborteof(aS("linked segment arity"));
  ucFWRITE(buf, 2, 1, pFtarg);
  
  proc_code_length = clause_code_length - 2;     // size of first clause 
  /* 
     There is no last clause marker in the created prefix segment 
     so subtract this off
     */
   first_clause = 1;
   while (1)
   {                                            // We are now at the code

#ifdef BUG_LINK
      fprintf(lout, "\nNext clause");
#endif
      if (first_clause)
      {
         first_clause = 0;
         fread(&op, sizeof(aBYTE), 1, f);       // see if  switch is active
         if (op == Ono_switch)
         {
#ifdef xBUG_LINK
            fprintf(lout, "\nno switch");
#endif
            fseek(f, 6L, SEEK_CUR);            // skip the remaining labels
            clause_code_length -= 7;
            proc_code_length -= 7;
              
            fread(&op, sizeof(aBYTE), 1, f);   // now look at try_me_else
            if (op == Ono_try)
            {
#ifdef xBUG_LINK
               fprintf(lout, "\nno try_me_else");
#endif
               clause_code_length -= 5;
               proc_code_length -= 5;
               fseek(f, 4L, SEEK_CUR);         // skip label and NTV
            }
            else
               fseek(f, -1L, SEEK_CUR);          // get back to the try_me
         }
         else
            fseek(f, -1L, SEEK_CUR);              // get back to first op code
      }
      
      read_code(f, pFtarg, PLM_CLHEAD_L, clause_code_length, (aBYTE*) buf);
      if (last_clause)                            // was last clause 
         break;
      
      if (!read_uint16(&atom_table_length, f))
         aborteof(aS("linked atom table length"));
      read_l_atoms(f, atom_table_length, filei);
      
      if (!read_uint16(&clause_code_length, f))    
         aborteof(aS("linked clause length"));
#ifdef BUG_LINK
  fprintf(lout, "clause_length = %d\n", clause_code_length);
#endif

      /* We don't include junk in prefix as part of length 
         after the first clause (we only have ONE name, arity etc) */
      
      proc_code_length += clause_code_length - PLM_CLHEAD_L;
      
      if (!read_uint16(&last_clause, f))
        aborteof(aS("linked last clause"));
      
      fseek(f, 4L, SEEK_CUR);                 // skip Name, ARITY 
    }
#ifdef BUG_LINK
  fprintf(lout, "\n  ----- end of predicate ----- \n");
#endif
                         // seek back to fill in proc_size & proc length 
  end_of_proc = ftell(pFtarg);
  fseek(pFtarg, start_of_proc, SEEK_SET);
#ifdef BUG_LINK
  fprintf(lout, "proc_length = %d\n", proc_code_length);
#endif
  write_int32(proc_code_length, pFtarg);
  fseek(pFtarg, end_of_proc, SEEK_SET);

}
Пример #16
0
int read_pdbfile(FILE *in,char *title,int *model_nr,
		 t_atoms *atoms,rvec x[],int *ePBC,matrix box,bool bChange,
		 gmx_conect conect)
{
  gmx_conect_t *gc = (gmx_conect_t *)conect;
  static t_symtab symtab;
  static bool bFirst=TRUE;
  bool bCOMPND;
  bool bConnWarn = FALSE;
  char line[STRLEN+1];
  int  line_type;
  char *c,*d;
  int  natom;
  bool bStop=FALSE;

  if (ePBC) {
    /* Only assume pbc when there is a CRYST1 entry */
    *ePBC = epbcNONE;
  }
  if (box != NULL) 
    clear_mat(box);

  if (bFirst) {
    open_symtab(&symtab);
    bFirst=FALSE;
  }

  bCOMPND=FALSE;
  title[0]='\0';
  natom=0;
  while (!bStop && (fgets2(line,STRLEN,in) != NULL)) {
    line_type = line2type(line);
    
    switch(line_type) {
    case epdbATOM:
    case epdbHETATM:
      natom = read_atom(&symtab,line,line_type,natom,atoms,x,bChange);
      break;
      
    case epdbANISOU:
      if (atoms->pdbinfo)
	read_anisou(line,natom,atoms);
      break;

    case epdbCRYST1:
      read_cryst1(line,ePBC,box);
      break;

    case epdbTITLE:
    case epdbHEADER:
      if (strlen(line) > 6) {
	c=line+6;
	/* skip HEADER or TITLE and spaces */
	while (c && (c[0]!=' ')) c++;
	while (c && (c[0]==' ')) c++;
	/* truncate after title */
	d=strstr(c,"      ");
	if (d) {
	  d[0]='\0';
	}
	if (strlen(c)>0)
	  strcpy(title,c);
      }
      break;
      
    case epdbCOMPND:
      if ((!strstr(line,": ")) || (strstr(line+6,"MOLECULE:"))) {
	if ( !(c=strstr(line+6,"MOLECULE:")) )
	  c=line;
	/* skip 'MOLECULE:' and spaces */
	while (c && (c[0]!=' ')) c++;
	while (c && (c[0]==' ')) c++;
	/* truncate after title */
	d=strstr(c,"   ");
	if (d) {
	  while ( (d[-1]==';') && d>c)  d--;
	  d[0]='\0';
	}
	if (strlen(c) > 0) {
	  if (bCOMPND) {
	    strcat(title,"; ");
	    strcat(title,c);
	  } else
	    strcpy(title,c);
	}
	bCOMPND=TRUE;
      } 
      break;
      
    case epdbTER:
      if (bTER)
	bStop=TRUE;
      break;
    case epdbMODEL:
      if(model_nr)
	sscanf(line,"%*s%d",model_nr);
      break;
    case epdbENDMDL:
      bStop=TRUE;
      break;
    case epdbCONECT:
      if (gc) 
	add_conection(gc,line);
      else if (!bConnWarn) {
	fprintf(stderr,"WARNING: all CONECT records are ignored\n");
	bConnWarn = TRUE;
      }
      break;
    default:
      break;
    }
  }
  
  return natom;
}
Пример #17
0
int read_ter_db(char *FF,char ter,t_hackblock **tbptr,gpp_atomtype_t atype)
{
    FILE       *in;
    char       inf[STRLEN],header[STRLEN],buf[STRLEN],line[STRLEN];
    t_hackblock *tb;
    int        i,j,n,ni,kwnr,nb,maxnb,nh;

    sprintf(inf,"%s-%c.tdb",FF,ter);
    in=libopen(inf);
    if (debug)
        fprintf(debug,"Opened %s\n",inf);

    tb=NULL;
    nb=-1;
    maxnb=0;
    kwnr=NOTSET;
    get_a_line(in,line,STRLEN);
    while (!feof(in)) {
        if (get_header(line,header)) {
            /* this is a new block, or a new keyword */
            kwnr=find_kw(header);

            if (kwnr == NOTSET) {
                nb++;
                /* here starts a new block */
                if ( nb >= maxnb ) {
                    maxnb+=100;
                    srenew(tb,maxnb);
                }
                clear_t_hackblock(&tb[nb]);
                tb[nb].name=strdup(header);
            }
        } else {
            if (nb < 0)
                gmx_fatal(FARGS,"reading termini database: "
                          "directive expected before line:\n%s\n"
                          "This might be a file in an old format.",line);
            /* this is not a header, so it must be data */
            if (kwnr >= ebtsNR) {
                /* this is a hack: add/rename/delete atoms */
                /* make space for hacks */
                if (tb[nb].nhack >= tb[nb].maxhack) {
                    tb[nb].maxhack+=10;
                    srenew(tb[nb].hack, tb[nb].maxhack);
                }
                nh=tb[nb].nhack;
                clear_t_hack(&(tb[nb].hack[nh]));
                for(i=0; i<4; i++)
                    tb[nb].hack[nh].a[i]=NULL;
                tb[nb].nhack++;

                /* get data */
                n=0;
                if ( kwnr==ekwRepl || kwnr==ekwDel ) {
                    if (sscanf(line, "%s%n", buf, &n) != 1)
                        gmx_fatal(FARGS,"Reading Termini Database: "
                                  "expected atom name on line\n%s",line);
                    tb[nb].hack[nh].oname = strdup(buf);
                    /* we only replace or delete one atom at a time */
                    tb[nb].hack[nh].nr = 1;
                } else if ( kwnr==ekwAdd ) {
                    read_ab(line, inf, &(tb[nb].hack[nh]));
                    get_a_line(in, line, STRLEN);
                } else
                    gmx_fatal(FARGS,"unimplemented keyword number %d (%s:%d)",
                              kwnr,__FILE__,__LINE__);
                if ( kwnr==ekwRepl || kwnr==ekwAdd ) {
                    snew(tb[nb].hack[nh].atom, 1);
                    read_atom(line+n, tb[nb].hack[nh].atom, atype,
                              &tb[nb].hack[nh].cgnr);
                    if (!tb[nb].hack[nh].nname) {
                        if (tb[nb].hack[nh].oname)
                            tb[nb].hack[nh].nname = strdup(tb[nb].hack[nh].oname);
                        else
                            gmx_fatal(FARGS,"Don't know which name the new atom should have");
                    }
                }
            } else if (kwnr >= 0 && kwnr < ebtsNR) {
                /* this is bonded data: bonds, angles, dihedrals or impropers */
                srenew(tb[nb].rb[kwnr].b,tb[nb].rb[kwnr].nb+1);
                n=0;
                for(j=0; j<btsNiatoms[kwnr]; j++) {
                    if ( sscanf(line+n, "%s%n", buf, &ni) == 1 )
                        tb[nb].rb[kwnr].b[tb[nb].rb[kwnr].nb].a[j] = strdup(buf);
                    else
                        gmx_fatal(FARGS,"Reading Termini Database: expected %d atom names (found %d) on line\n%s", btsNiatoms[kwnr], j-1, line);
                    n+=ni;
                }
                for(   ; j<MAXATOMLIST; j++)
                    tb[nb].rb[kwnr].b[tb[nb].rb[kwnr].nb].a[j] = NULL;
                strcpy(buf, "");
                sscanf(line+n, "%s", buf);
                tb[nb].rb[kwnr].b[tb[nb].rb[kwnr].nb].s = strdup(buf);
                tb[nb].rb[kwnr].nb++;
            } else
                gmx_fatal(FARGS,"Reading Termini Database: Expecting a header at line\n"
                          "%s",line);
        }
        get_a_line(in,line,STRLEN);
    }
    nb++;
    srenew(tb,nb);

    fclose(in);

    if (debug)
        print_ter_db(FF,ter,nb,tb,atype);

    *tbptr=tb;
    return nb;
}
Пример #18
0
int Linker::read_code(FILE* f, FILE* pFtarg, long init_pos, int length, 
							 aBYTE* buf)
{
  aBYTE op;
  int   x;
#ifdef BUG_LINK
  int   i;
#endif
  
  while( init_pos < length)
    {
#ifdef BUG_LINK
      fprintf(lout, "\nstart length %d init_pos %d ", length, init_pos);
#endif
      if (0 == fread(buf, CODESIZE, 1, f))
        aborteof(aS("code"));
      ucFWRITE(buf, CODESIZE, 1, pFtarg);
      init_pos += CODESIZE;
      op = *buf;
#ifdef BUG_LINK
      fprintf(lout, "\nop %d, %s: ", (int) op, lops[op]);
#endif
      switch(op)
        {    
        case Ono_op:                           // no args 
        case Ofail:
        case Oproceed:
        case Odealloc:
        case Ocut:
        case Ocut64:
        case Otrust_me_else:
        case Ou_var_getlist:
        case Ounify_nil:
          break;
          
        case Ounify_y_var:                     // Xi or Yi 
        case Ounify_y_val:
        case Ounify_unsafe:
        case Oget_nil:
        case Oget_list:
        case Oput_nil:
        case Oput_list:
        case Ounify_x_var:
        case Ounify_x_val:
          if (0 == fread(buf, 2, 1, f))
            aborteof(aS("unify x val"));
          ucFWRITE(buf, 2, 1, pFtarg);
          init_pos += 2;
          break;
          
        case Oget_y_var:                       // Xi and Yi or Xj 
        case Oget_y_val:
        case Oput_y_var:
        case Oput_y_val:
        case Oput_unsafe:
        case Oget_x_var:
        case Oget_x_val:
        case Oput_x_var:
        case Oput_x_val:
          if (0 == fread(buf, 2, 2, f))
            aborteof(aS("put x val"));
          ucFWRITE(buf, 2, 2, pFtarg);
          init_pos += 4;
          break;
          
        case Ounify_void:                      // short int 
        case Oalloc:
        case Ocutd:
        case Oretry_me_else:
        case Oretry:
        case Otrust:
        case Ogoto:
        case Otrust_me_2_else:
        case Olabel:
          if (0 == fread(buf, 2, 1, f))
            aborteof(aS("label"));
          ucFWRITE(buf, 2, 1, pFtarg);
          init_pos += 2;
#ifdef BUG_LINK
          fprintf(lout, "%d", (int) getint16(buf));
#endif
          break;
          
        case Otry_me_else:
        case Otry_me_or_else:
        case Otry:                             // 2 short ints 
          if (0 == fread(buf, 2, 2, f))
            aborteof(aS("try"));
          ucFWRITE(buf, 2, 2, pFtarg);
          init_pos += 4;
#ifdef BUG_LINK
          fprintf(lout, "%d, %d", 
                  (int) getint16(buf), (int) getint16(buf+2));
#endif
          break;
          
        case Oget_con:                         // Constant, Xi 
        case Oput_con:
          read_const(f, pFtarg, &init_pos);
          if (0 == fread(buf, 2, 1, f))
            aborteof(aS("put con"));
          ucFWRITE(buf, 2, 1, pFtarg);
          init_pos += 2;
          break;
          
        case Oexec:
        case Oescape:
        case Ocall:                          
        case Omod_call:
        case Omod_exec:
        case Oget_struc:              // functor, arity, (Xi or short or null)
        case Oput_struc:
          read_atom(f, pFtarg);                // functor 
          init_pos += 2;
          if (op == Ocall || op == Oexec || op == Omod_call || op == Omod_exec)      // new style call/exec 
            {
              //if (0 == fread(buf, 2, 1, f)) 
              //  aborteof(aS("put struc"));
              //ucFWRITE(buf, 2, 1, pFtarg);
             // shouldn't this be a read atom, not just a buf??
             read_atom(f, pFtarg);
              init_pos += 2;
            }       
          
          if (0 == fread(buf, 2, 1, f))
            aborteof(aS("put struc 2"));
          ucFWRITE(buf, 2, 1, pFtarg);
          init_pos += 2;
#ifdef BUG_LINK
          fprintf(lout, "  arity = %d", (int) getint16(buf));
#endif
                                             // now figure third (optional arg 
          if (op == Oget_struc || 
              op == Oput_struc)                // Xi 
            {
              if (0 == fread(buf, 2, 1, f))
                aborteof(aS("put struc 3"));
              ucFWRITE(buf, 2, 1, pFtarg);
              init_pos += 2;
#ifdef BUG_LINK
          fprintf(lout, "  xi(get/put_struc) = %d", (int) getint16(buf));
#endif
            }
          else if (op == Ocall || op == Omod_call)                // short 
            {
              if (0 == fread(buf, 2, 1, f))
                aborteof(aS("put struc 4"));
              ucFWRITE(buf, 2, 1, pFtarg);
              init_pos += 2;
#ifdef BUG_LINK
          fprintf(lout, "  short(call/mod_call) = %d", (int) getint16(buf));
#endif
            }
                                              // else no 3rd arg 
          break;
          
        case Ounify_con:
          read_const(f, pFtarg, &init_pos);
          break;
          
          
        case Oswitch_on_term:
          if (0 == fread(buf, 2, 3, f))        // lab1, lab2, lab3 
            aborteof(aS("switch on term"));
          init_pos += 6;
          ucFWRITE(buf, 2, 3, pFtarg);
#ifdef BUG_LINK
          for (i=0; i<3; i++) 
            fprintf(lout, " %d ",(int) getint16(buf + 2 * i));
#endif
          break;
          
        case Oswitch_on_cons:           // short size, (size x |CELL|LABEL|) 
          if (0 == fread(buf, 2, 1, f))
            aborteof(aS("switch on cons"));
          ucFWRITE(buf, 2, 1, pFtarg);
          x = getint16(buf);
          init_pos += 2;
          while(x--)
            {       
              read_const(f, pFtarg, &init_pos); // get const 
              if (0 == fread(buf, 2, 1, f))     // branch label 
                aborteof(aS("switch on cons 2"));

#ifdef BUG_LINK

              fprintf(lout, " branch[%d] ", (int) getint16(buf));
#endif
              ucFWRITE(buf, 2, 1, pFtarg);
              init_pos += 2;
            }
          break;
          
        case Oswitch_on_struc:    // short size, (size x |NAME|ARITY|LABEL|) 
          if (0 == fread(buf, 2, 1, f))
            aborteof(aS("switch on struc"));
          ucFWRITE(buf, 2, 1, pFtarg);
          x = getint16(buf);
          init_pos += 2;
          while(x--)
            {       
              read_atom(f, pFtarg);
              if (0 == fread(buf, 2, 1, f)) // arity 
                aborteof(aS("switch on struc 2"));
              ucFWRITE(buf, 2, 1, pFtarg);
              
              if (0 == fread(buf, 2, 1, f))    // label 
                aborteof(aS("switch on struc 2"));

#ifdef BUG_LINK
              fprintf(lout, " branch[%d] ", (int) getint16(buf));

#endif
              ucFWRITE(buf, 2, 1, pFtarg);
              init_pos += 2 + 2 + 2;
            }
          break;
          
        default:
          abort_linker(aS("Error: Bad opcode in CodeStream %d"), op);
        }
#ifdef BUG_LINK
      fprintf(lout, "\nend length %d init_pos %d ", length, init_pos);
#endif
    }
  return(1);
}
Пример #19
0
MalVal *read_form(Reader *reader) {
    char *token;
    MalVal *form = NULL, *tmp;

//    while(token = reader_next(reader)) {
//        printf("token: %s\n", token);
//    }
//    return NULL;

    token = reader_peek(reader);

    if (!token) { return NULL; }
    //g_print("read_form token: %s\n", token);

    switch (token[0]) {
    case ';':
        abort("comments not yet implemented");
        break;
    case '\'':
        reader_next(reader);
        form = _listX(2, malval_new_symbol("quote"),
                         read_form(reader));
        break;
    case '`':
        reader_next(reader);
        form = _listX(2, malval_new_symbol("quasiquote"),
                         read_form(reader));
        break;
    case '~':
        reader_next(reader);
        if (token[1] == '@') {
            form = _listX(2, malval_new_symbol("splice-unquote"),
                             read_form(reader));
        } else {
            form = _listX(2, malval_new_symbol("unquote"),
                             read_form(reader));
        };
        break;
    case '^':
        reader_next(reader);
        MalVal *meta = read_form(reader);
        form = _listX(3, malval_new_symbol("with-meta"),
                         read_form(reader), meta);
        break;
    case '@':
        reader_next(reader);
        form = _listX(2, malval_new_symbol("deref"),
                         read_form(reader));
        break;


    // list
    case ')':
        abort("unexpected ')'");
        break;
    case '(':
        form = read_list(reader, MAL_LIST, '(', ')');
        break;

    // vector
    case ']':
        abort("unexpected ']'");
        break;
    case '[':
        form = read_list(reader, MAL_VECTOR, '[', ']');
        break;

    // hash-map
    case '}':
        abort("unexpected '}'");
        break;
    case '{':
        form = read_hash_map(reader);
        break;

    default:
        form = read_atom(reader);
        break;
    }
    return form;

}
Пример #20
0
void Linker::read_const(FILE *f, FILE *pFtarg, long *pi)
{
   /* a constant is  a tag followed by constant data 
   tag = 1 -> constant is short atom
   tag = 2 -> constant is short int
   tag = 3 -> constant is arb string terminated by EOS
   tag = 4 -> constant is 4 byte float
   tag = 5 -> constant is 4 byte long
   tag = 6 -> constant is 4 byte long wide char
   tag = 7 -> constant is 8 byte double
   tag = 8 -> constant is real
    */
   aBYTE buf[8];
   int   ccount;
   aBYTE ibyte;
   aINT16 temp, length;
	aINT32 temp32;
   aCHAR ichar;

   if (0 == fread(buf, 1, 1, f))
      aborteof(aS("read const"));
   ucFWRITE(buf, 1, 1, pFtarg);
   (*pi) += 1;
   switch(*buf)
     {
     case 1:
       read_atom(f, pFtarg);
       (*pi) += 2;
       break;
       
     case 2:
       if (0 == fread(buf, 2, 1, f))
         aborteof(aS("read const 2"));
       ucFWRITE(buf, 2, 1, pFtarg);
       (*pi) += 2;     
       break;
       
     case 3:                                      // single byte string 
       ccount = 1;
       if (0 == fread(&ibyte, 1, 1, f))
         aborteof(aS("read const 3"));
       ichar = (aCHAR)ibyte;
       while(ichar)
         {
           ++ccount;
           ucFWRITE(&ichar, 1, 1, pFtarg);
           if (0 == fread(&ibyte, 1, 1, f))
             aborteof(aS("read const 3b"));
           ichar = (aCHAR)ibyte;
         }
       ucFWRITE(&ichar, 1, 1, pFtarg);
       (*pi) += ccount*sizeof(char);
       break;
       
     case 4:                                      // float
       if (0 == fread(buf, 4, 1, f))
         aborteof(aS("read const 4"));
       ucFWRITE(buf, 4, 1, pFtarg);
       (*pi) += 4;     
       break;
       
     case 5:                                      // long
       if (0 == fread(buf, 4, 1, f))
         aborteof(aS("read const 5"));
       ucFWRITE(buf, 4, 1, pFtarg);
       (*pi) += 4;     
       break;
       
     case 6:                                      // wide character string 
       ccount = 1;
       //    if (0 == fread(&ichar, sizeof(aCHAR), 1, f))
       if (!read_int16(&temp, f))
         aborteof(aS("read const 3"));
       while(temp)
         {
           ++ccount;
           //       ucFWRITE(&ichar, sizeof(aCHAR), 1, pFtarg);
           write_int16(temp, pFtarg);
           //       if (0 == fread(&ichar, sizeof(aCHAR), 1, f))
           if (!read_int16(&temp, f))
             aborteof(aS("read const 3b"));
         }
       //    fwrite(&ichar, sizeof(aCHAR), 1, pFtarg);
       write_int16(temp, pFtarg);
       (*pi) += ccount*2;
       break;
       
	  case 9:                                   // fixed
     case 7:                                   // C double
       if (0 == fread(buf, 8, 1, f))
         aborteof(aS("read const 7"));
       ucFWRITE(buf, 8, 1, pFtarg);
       (*pi) += 8;     
       break;

	  case 8:
		 for(ccount = 0; ccount < 6; ccount += 2)
			{                                    // copy the real descr
			  if (!read_int16(&temp, f))
				 aborteof(aS("read const 3"));
			  if(ccount == 0) 
				 length = temp;                   // length
           write_int16(temp, pFtarg);
			}
		 while(length)
			{                                    // copy the gigits
			  if (!read_int32(&temp32, f))
				 aborteof(aS("read const 3b"));
           write_int32(temp32, pFtarg);
			  ccount+= 4;
			  length--;
			}
       (*pi) += ccount;
     }                                        // end switch
}