Exemple #1
0
static Int read_stream_to_terms(USES_REGS1) {
  int sno = Yap_CheckStream(ARG1, Input_Stream_f, "read_line_to_codes/2");
  Term t, hd;
  yhandle_t tails, news;

  if (sno < 0)
    return FALSE;

  t = AbsPair(HR);
  RESET_VARIABLE(HR);
  Yap_InitSlot((CELL)(HR));
  tails = Yap_InitSlot((CELL)(HR));
  news = Yap_InitSlot((CELL)(HR));
  HR++;

  while (!(GLOBAL_Stream[sno].status & Eof_Stream_f)) {
    RESET_VARIABLE(HR);
    RESET_VARIABLE(HR + 1);
    hd = (CELL)HR;
    Yap_PutInSlot(news, (CELL)(HR + 1));
    HR += 2;
    while ((hd = Yap_read_term(sno, TermNil, 2)) == 0L)
      ;
    // just ignore failure
    CELL *pt = VarOfTerm(Yap_GetFromSlot(tails));
    if (Deref(hd) == TermEOfCode) {
      *pt = Deref(ARG3);
      break;
    } else {
      CELL *newpt = (CELL *)Yap_GetFromSlot(news);
      *pt = AbsPair(newpt - 1);
      Yap_PutInSlot(tails, (CELL)newpt);
    }
  }
  UNLOCK(GLOBAL_Stream[sno].streamlock);
  return Yap_unify(t, ARG2);
}
Exemple #2
0
/* Return a list of files for a directory */
static Int list_directory(USES_REGS1) {
  Term tf = MkAtomTerm(Yap_LookupAtom("[]"));
  yhandle_t sl = Yap_InitSlot(tf);
VFS_t *vfsp;
  char *buf = (char *)AtomName(AtomOfTerm(ARG1));
    if ((vfsp = vfs_owner(buf))) {
        void *de;
      const char *dp;

    if ((de = vfsp->opendir(vfsp, buf)) == NULL) {
      PlIOError(PERMISSION_ERROR_INPUT_STREAM, ARG1, "%s in list_directory",
		strerror(errno));
    }
    while ((dp = vfsp->nextdir( de))) {
      YAP_Term ti = MkAtomTerm(Yap_LookupAtom(dp));
      Yap_PutInHandle(sl, MkPairTerm(ti, Yap_GetFromHandle(sl)));
    }
    vfsp->closedir( de);
 } else {
#if defined(__MINGW32__) || _MSC_VER
        struct _finddata_t c_file;
        char bs[BUF_SIZE];
        long hFile;

        bs[0] = '\0';
#if HAVE_STRNCPY
        strncpy(bs, buf, BUF_SIZE);
#else
        strcpy(bs, buf);
#endif
#if HAVE_STRNCAT
        strncat(bs, "/*", BUF_SIZE);
#else
        strcat(bs, "/*");
#endif
        if ((hFile = _findfirst(bs, &c_file)) == -1L) {
          return (Yap_unify(ARG2, tf));
        }
        Yap_PutInSlot(sl, MkPairTerm(MkAtomTerm(Yap_LookupAtom(c_file.name)),
                                         Yap_GetFromSlot(sl)));
        while (_findnext(hFile, &c_file) == 0) {
          Term ti = MkAtomTerm(Yap_LookupAtom(c_file.name));
          Yap_PutInSlot(sl, MkPairTerm(ti, Yap_GetFromSlot(sl)));
        }
        _findclose(hFile);
#elif HAVE_OPENDIR
        {
            DIR *de;
            struct dirent *dp;

            if ((de = opendir(buf)) == NULL) {
                PlIOError(PERMISSION_ERROR_INPUT_STREAM, ARG1, "%s in list_directory",
                          strerror(errno));

                return false;
            }
            while ((dp = readdir(de))) {
                Term ti = MkAtomTerm(Yap_LookupAtom(dp->d_name));
                Yap_PutInSlot(sl, MkPairTerm(ti, Yap_GetFromSlot(sl)));
            }
            closedir(de);
        }
#endif /* HAVE_OPENDIR */
    }
  tf = Yap_GetFromSlot(sl); 
  return Yap_unify(ARG2, tf);
}