コード例 #1
0
ファイル: uliolib51.c プロジェクト: Frankie-666/farmanager
static int g_read(lua_State *L, FILE *f, int first)
{
    int nargs = lua_gettop(L) - 1;
    int success;
    int n;
    clearerr(f);

    if(nargs == 0)     /* no arguments? */
    {
        success = read_line(L, f);
        n = first+1;  /* to return 1 result */
    }
    else    /* ensure stack space for all results and for auxlib's buffer */
    {
        luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
        success = 1;

        for(n = first; nargs-- && success; n++)
        {
            if(lua_type(L, n) == LUA_TNUMBER)
            {
                size_t l = (size_t)lua_tointeger(L, n);
                success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
            }
            else
            {
                const char *p = lua_tostring(L, n);
                luaL_argcheck(L, p && p[0] == '*', n, "invalid option");

                switch(p[1])
                {
                case 'n':  /* number */
                    success = read_number(L, f);
                    break;
                case 'l':  /* line */
                    success = read_line(L, f);
                    break;
                case 'a':  /* file */
                    read_chars(L, f, ~((size_t)0));  /* read MAX_SIZE_T chars */
                    success = 1; /* always success */
                    break;
                default:
                    return luaL_argerror(L, n, "invalid format");
                }
            }
        }
    }

    if(ferror(f))
        return pushresult(L, 0, NULL);

    if(!success)
    {
        lua_pop(L, 1);  /* remove last result */
        lua_pushnil(L);  /* push nil instead */
    }

    return n - first;
}
コード例 #2
0
ファイル: io.c プロジェクト: Stefan-Schmidt/efl
static int
elua_read(lua_State *L)
{
   FILE *f   = tofile(L);
   int nargs = lua_gettop(L) - 1;
   int first = 2;
   int success, n;
   clearerr(f);
   if (!nargs)
     {
        success = read_line(L, f);
        n = first + 1;
     }
   else
     {
        luaL_checkstack(L, nargs + LUA_MINSTACK, "too many arguments");
        success = 1;
        for (n = first; nargs-- && success; ++n)
          {
             if (lua_type(L, n) == LUA_TNUMBER)
               {
                  size_t l = (size_t)lua_tointeger(L, n);
                  success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
               }
             else
               {
                  const char *p = lua_tostring(L, n);
                  luaL_argcheck(L, p && p[0] == '*', n, "invalid option");
                  switch (p[1])
                    {
                       case 'n':
                          success = read_number(L, f);
                          break;
                       case 'l':
                          success = read_line(L, f);
                          break;
                       case 'a':
                          read_chars(L, f, ~((size_t)0));
                          success = 1;
                          break;
                       default:
                          return luaL_argerror(L, n, "invalid format");
                    }
               }
          }
     }
   if (ferror(f))
      return push_ret(L, 0, NULL);
   if (!success)
     {
        lua_pop(L, 1);
        lua_pushnil(L);
     }
   return n - first;
}
コード例 #3
0
ファイル: fdisksgilabel.c プロジェクト: dtrebbien/util-linux
static int sgi_set_parttype(struct fdisk_context *cxt, int i,
			    struct fdisk_parttype *t)
{
	if (i >= partitions || !t || t->type > UINT32_MAX)
		return -EINVAL;

	if (sgi_get_num_sectors(cxt, i) == 0)	/* caught already before, ... */ {
		printf(_("Sorry, only for non-empty partitions you can change the tag.\n"));
		return -EINVAL;
	}

	if ((i == 10 && t->type != ENTIRE_DISK) || (i == 8 && t->type != 0))
		printf(_("Consider leaving partition 9 as volume header (0), "
			 "and partition 11 as entire volume (6), as IRIX "
			 "expects it.\n\n"));

	if (((t->type != ENTIRE_DISK) && (t->type != SGI_VOLHDR))
	    && (sgi_get_start_sector(cxt, i) < 1)) {
		read_chars(
			_("It is highly recommended that the partition at offset 0\n"
			  "is of type \"SGI volhdr\", the IRIX system will rely on it to\n"
			  "retrieve from its directory standalone tools like sash and fx.\n"
			  "Only the \"SGI volume\" entire disk section may violate this.\n"
			  "Type YES if you are sure about tagging this partition differently.\n"));
		if (strcmp (line_ptr, _("YES\n")))
			return 1;
	}
	sgilabel->partitions[i].id = SSWAP32(t->type);
	return 0;
}
コード例 #4
0
ファイル: memoryfile.c プロジェクト: brkpt/luaplus51-all
static int
memfile_read (lua_State *L) {
    MemoryFile *f = luaL_checkudata(L, 1, MEMORYFILE_MT_NAME);
    int nargs = lua_gettop(L) - 1;
    int success;
    int n;

    if (nargs == 0) {   /* no arguments? */
        success = read_line(f, L);
        n = 3;          /* to return 1 result */
    }
    else {  /* ensure stack space for all results and for auxlib's buffer */
        luaL_checkstack(L, nargs + LUA_MINSTACK, "too many arguments");
        success = 1;
        for (n = 2; nargs-- && success; n++) {
            if (lua_type(L, n) == LUA_TNUMBER) {
                size_t l = (size_t) lua_tointeger(L, n);
                success = (l == 0) ? test_eof(f, L) : read_chars(f, L, l);
            }
            else {
                const char *p = lua_tostring(L, n);
                luaL_argcheck(L, p && p[0] == '*', n, "invalid option");
                switch (p[1]) {
                    case 'n':  /* number */
                        success = read_number(f, L);
                        break;
                    case 'l':  /* line */
                        success = read_line(f, L);
                        break;
                    case 'a':  /* all the rest of the file */
                        read_chars(f, L, ~((size_t)0)); /* MAX_SIZE_T bytes */
                        success = 1; /* always success */
                        break;
                    default:
                        return luaL_argerror(L, n, "invalid format");
                }
            }
        }
    }

    if (!success) {
        lua_pop(L, 1);      /* remove last result */
        lua_pushnil(L);     /* push nil instead */
    }

    return n - 2;
}
コード例 #5
0
ファイル: thinnx.c プロジェクト: BackupTheBerlios/freenx-svn
void
drop_chars (gint fd, gint count)
{
  gchar *buffer;

  buffer = read_chars (fd, count);
  flush_buffer (buffer);
}
コード例 #6
0
/*
 * Ask the user for new partition type information (logical, extended).
 * This function calls the actual partition adding logic - dos_add_partition.
 */
void dos_new_partition(struct fdisk_context *cxt)
{
	int i, free_primary = 0;

	for (i = 0; i < 4; i++)
		free_primary += !ptes[i].part_table->sys_ind;

	if (!free_primary && partitions >= MAXIMUM_PARTS) {
		printf(_("The maximum number of partitions has been created\n"));
		return;
	}

	if (!free_primary) {
		if (extended_offset) {
			printf(_("All primary partitions are in use\n"));
			add_logical(cxt);
		} else
			printf(_("If you want to create more than four partitions, you must replace a\n"
				 "primary partition with an extended partition first.\n"));
	} else if (partitions >= MAXIMUM_PARTS) {
		printf(_("All logical partitions are in use\n"));
		printf(_("Adding a primary partition\n"));
		dos_add_partition(cxt, get_partition(cxt, 0, 4), LINUX_NATIVE);
	} else {
		char c, dflt, line[LINE_LENGTH];

		dflt = (free_primary == 1 && !extended_offset) ? 'e' : 'p';
		snprintf(line, sizeof(line),
			 _("Partition type:\n"
			   "   p   primary (%d primary, %d extended, %d free)\n"
			   "%s\n"
			   "Select (default %c): "),
			 4 - (extended_offset ? 1 : 0) - free_primary, extended_offset ? 1 : 0, free_primary,
			 extended_offset ? _("   l   logical (numbered from 5)") : _("   e   extended"),
			 dflt);

		c = tolower(read_chars(line));
		if (c == '\n') {
			c = dflt;
			printf(_("Using default response %c\n"), c);
		}
		if (c == 'p') {
			int i = get_nonexisting_partition(cxt, 0, 4);
			if (i >= 0)
				dos_add_partition(cxt, i, LINUX_NATIVE);
			return;
		} else if (c == 'l' && extended_offset) {
			add_logical(cxt);
			return;
		} else if (c == 'e' && !extended_offset) {
			int i = get_nonexisting_partition(cxt, 0, 4);
			if (i >= 0)
				dos_add_partition(cxt, i, EXTENDED);
			return;
		} else
			printf(_("Invalid partition type `%c'\n"), c);
	}
}
コード例 #7
0
ファイル: ascii1.c プロジェクト: FuzzyHobbit/mordor
int read_object_basic(FILE *fp, int *index, object *obj)
{
	int	i;
	char	str[10];
	
/* begin of object data */
	read_int(fp, index);  /* object # */
	
	read_chars(fp, obj->name, sizeof(obj->name));
	read_chars(fp, obj->description, sizeof(obj->description));
	for (i = 0; i != 3; i++)
		read_chars(fp, obj->key[i], sizeof(obj->key[i]));
	read_chars(fp, obj->use_output, sizeof(obj->use_output));
	read_long(fp, &obj->value);
	read_short(fp, &obj->weight);
	
	read_char(fp, &obj->type);
	read_char(fp, &obj->adjustment);
	
	read_short(fp, &obj->shotsmax);
	read_short(fp, &obj->shotscur);
	read_short(fp, &obj->ndice);
	read_short(fp, &obj->sdice);
	read_short(fp, &obj->pdice);
	
	read_char(fp, &obj->armor);
	read_char(fp, &obj->wearflag);
	read_char(fp, &obj->magicpower);
	read_char(fp, &obj->magicrealm);
	read_short(fp, &obj->special);
	read_chars(fp, obj->flags, sizeof(obj->flags));
	read_char(fp, &obj->questnum);
	read_char(fp,&obj->strength);
	read_char(fp,&obj->dexterity);
	read_char(fp,&obj->constitution);
	read_char(fp,&obj->intelligence);
	read_char(fp,&obj->piety);
	for (i = 0; i != 16; i++)
		read_short(fp, &obj->sets_flag[i]);
	read_short(fp,&obj->special1);
	read_long(fp,&obj->special2);
	
/* end of object data */
	return(0);
}
コード例 #8
0
// grabs the 32 bytes for the file at (directory_clus, entry_num) and place it
// where ptr points to
int get_directory_entry(union directory_entry *ptr, unsigned int directory_clus, unsigned int entry_num) {
	unsigned int first_dir_clus, entry_first_byte_offset;
	first_dir_clus = get_first_sector_of_cluster(directory_clus);
	if ((entry_first_byte_offset = 32*entry_num) >= img_info.bytes_per_sec*img_info.sec_per_clus) {
		// bad offset
		return 0;
	}
	read_chars(ptr, entry_first_byte_offset + first_dir_clus*img_info.bytes_per_sec, sizeof(union directory_entry));
	return 1;
}
コード例 #9
0
ファイル: icu.ufile.c プロジェクト: Chingliu/icu4lua
static int icu_ufile_read(lua_State *L) {
	int nargs = lua_gettop(L) - 1;
	int success;
	int n;
	UFILE* ufile = icu4lua_checkufile(L,1,UFILE_UV_META);
	if (nargs == 0) { // no arguments?
		success = read_line(L, ufile);
		n = 3; // to return 1 result
	}
	else {
		luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
		success = 1;
		for (n = 2; nargs-- && success; n++) {
			if (lua_type(L,n) == LUA_TNUMBER) {
				int32_t l = (int32_t)lua_tointeger(L, n);
				success = (l == 0) ? test_eof(L, ufile) : read_chars(L, ufile, l);
			}
			else {
				const char* p = lua_tostring(L, n);
				luaL_argcheck(L, p && p[0] == '*', n, "invalid option");
				switch(p[1]) {
					case 'n': // number
						success = read_number(L, ufile);
						break;
					case 'l': // line
						success = read_line(L, ufile);
						break;
					case 'a': // file
						read_chars(L, ufile, INT32_MAX);
						success = 1;
						break;
					default:
						return luaL_argerror(L, n, "invalid format");
				}
			}
		}
	}
	if (!success) {
		lua_pop(L,1); // remove last result
		lua_pushnil(L); // push nil instead
	}
	return n - 2;
}
コード例 #10
0
int read_file(union directory_entry *file, unsigned int position, unsigned int size) {
	unsigned int offset, bytes_left, cur_clus, bytes_per_clus, byte_position, nmemb;
	char *buffer;
	bytes_per_clus = img_info.bytes_per_sec*img_info.sec_per_clus;
	buffer = malloc(sizeof(char)*(bytes_per_clus));
	offset = position;
	bytes_left = size;
	cur_clus = get_file_cluster(file);
	while (offset > bytes_per_clus) {
		cur_clus = get_next_cluster_in_fat(cur_clus);
		if (end_of_chain(cur_clus)) {
			free(buffer);
			return 0;
		}
		offset -= bytes_per_clus;
	}
	byte_position = img_info.bytes_per_sec*get_first_sector_of_cluster(cur_clus) + offset;
	if (bytes_left > bytes_per_clus - offset) {
		nmemb = bytes_per_clus - offset;
	} else {
		nmemb = bytes_left;
	}
	read_chars(buffer, byte_position, nmemb);
	fwrite(buffer, sizeof(char), nmemb, stdout);
	bytes_left -= nmemb;
	while (bytes_left > 0) {
		cur_clus = get_next_cluster_in_fat(cur_clus);
		if (end_of_chain(cur_clus)) {
			free(buffer);
			return 0;
		}
		byte_position = img_info.bytes_per_sec*get_first_sector_of_cluster(cur_clus);
		if (bytes_left < bytes_per_clus) {
			nmemb = bytes_left;
		} else {
			nmemb = bytes_per_clus;
		}
		read_chars(buffer, byte_position, nmemb);
		fwrite(buffer, sizeof(char), nmemb, stdout);
		bytes_left -= nmemb;
	}
	return 1;
}
コード例 #11
0
ファイル: lua_iolib.cpp プロジェクト: astrellon/loss
    // File Read Operatings {{{
    int LuaIOLib::g_read (lua_State *lua, FileHandle *file, int first) 
    {
        auto process = proc(lua);
        auto &vfs = process->vfs();

        int nargs = lua_gettop(lua) - 1;
        int success;
        int n;
        //clearerr(file);
        if (nargs == 0) {  /* no arguments? */
            success = read_line(lua, file, 1);
            n = first+1;  /* to return 1 result */
        }
        else {  /* ensure stack space for all results and for auxlib's buffer */
            luaL_checkstack(lua, nargs+LUA_MINSTACK, "too many arguments");
            success = 1;
            for (n = first; nargs-- && success; n++) {
                if (lua_type(lua, n) == LUA_TNUMBER) {
                    size_t l = (size_t)lua_tointeger(lua, n);
                    success = (l == 0) ? file->at_end_of_stream() : read_chars(lua, file, l);
                }
                else {
                    const char *p = lua_tostring(lua, n);
                    luaL_argcheck(lua, p && p[0] == '*', n, "invalid option");
                    switch (p[1]) {
                        case 'n':  /* number */
                            success = read_number(lua, file);
                            break;
                        case 'l':  /* line */
                            success = read_line(lua, file, 1);
                            break;
                        case 'L':  /* line with end-of-line */
                            success = read_line(lua, file, 0);
                            break;
                        case 'a':  /* file */
                            read_all(lua, file);  /* read entire file */
                            success = 1; /* always success */
                            break;
                        default:
                            return luaL_argerror(lua, n, "invalid format");
                    }
                }
            }
        }
        /*
        if (ferror(file))
            return luaL_fileresult(lua, 0, NULL);
            */
        if (!success) {
            lua_pop(lua, 1);  /* remove last result */
            lua_pushnil(lua);  /* push nil instead */
        }
        return n - first;
    }
コード例 #12
0
int get_next_directory_entry(union directory_entry *ptr, unsigned int directory_clus, unsigned int entry_num) {
	unsigned int first_dir_clus, entry_first_byte_offset, next_num;
	next_num = entry_num + 1;
	if ((entry_first_byte_offset = 32*next_num) >= img_info.bytes_per_sec*img_info.sec_per_clus) {
		first_dir_clus = get_first_sector_of_cluster(get_next_cluster_in_fat(directory_clus));
		entry_first_byte_offset -= img_info.bytes_per_sec*img_info.sec_per_clus;
	} else {
		first_dir_clus = get_first_sector_of_cluster(directory_clus);
	}
	read_chars(ptr, entry_first_byte_offset + first_dir_clus*img_info.bytes_per_sec, sizeof(union directory_entry));
	return 1;
}
コード例 #13
0
ファイル: liolib.cpp プロジェクト: philm001/Omni-FEM
static int io_read (lua_State *L) {
  IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
  int lastarg = lua_gettop(L) - 1;
  int firstarg = 1;
  FILE *f = gethandle(L, ctrl, firstarg);
  int n;
  if (f) firstarg++;
  else f = getfilebyref(L, ctrl, INFILE);  /* get _INPUT */
  lua_pop(L, 1);
  if (firstarg > lastarg) {  /* no arguments? */
    lua_settop(L, 0);  /* erase upvalue and other eventual garbage */
    firstarg = lastarg = 1;  /* correct indices */
    lua_pushstring(L, "*l");  /* push default argument */
  }
  else  /* ensure stack space for all results and for auxlib's buffer */
    luaL_checkstack(L, lastarg-firstarg+1+LUA_MINSTACK, "too many arguments");
  for (n = firstarg; n<=lastarg; n++) {
    int success;
    if (lua_isnumber(L, n))
      success = read_chars(L, f, (size_t)Re(lua_tonumber(L, n)));
    else {
      const char *p = luaL_check_string(L, n);
      if (p[0] != '*')
        success = read_pattern(L, f, p);  /* deprecated! */
      else {
        switch (p[1]) {
          case 'n':  /* number */
            if (!read_number(L, f)) goto endloop;  /* read fails */
            continue;  /* number is already pushed; avoid the "pushstring" */
          case 'l':  /* line */
            success = read_line(L, f);
            break;
          case 'a':  /* file */
            read_file(L, f);
            success = 1; /* always success */
            break;
          case 'w':  /* word */
            success = read_word(L, f);
            break;
          default:
            luaL_argerror(L, n, "invalid format");
            success = 0;  /* to avoid warnings */
        }
      }
    }
    if (!success) {
      lua_pop(L, 1);  /* remove last result */
      break;  /* read fails */
    }
  } endloop:
  return n - firstarg;
}
コード例 #14
0
static int req_read (lua_State *L)
{
  int nargs = lua_gettop(L) ;
  int success;
  int n = 1;

  request_rec *r = CHECK_REQUEST_OBJECT(1);
#if 0
  if (r->remaining == 0 )
  {
    lua_pushnil(L);  /* push nil instead */
    return 1;
  }
#endif
  if (nargs == 1)    /* no arguments? FIXIT */
  {
    return success = read_chars(L, r, (size_t)~0);
  }

  luaL_checkstack(L, nargs + LUA_MINSTACK, "too many arguments");
  success = 1;

  for (n = 2; n <= nargs && success; n++)
  {
    if (lua_type(L, n) == LUA_TNUMBER)
    {
      size_t l = (size_t)lua_tonumber(L, n);
      success = read_chars(L, r, l);
    }
    else
    {
      const char *p = luaL_checkstring(L, n);
      luaL_argcheck(L, p && p[0] == '*' && p[1] == 'a', n, "invalid option");
      success = read_chars(L, r, (size_t)~0); /* read MAX_SIZE_T chars */
    }
  }

  return n - 2;
}
コード例 #15
0
ファイル: database.cpp プロジェクト: cloverhap/mapplus
void read_db() {
#ifdef DEBUG_VALUE
    //char cur_dir[256];
    //GetCurrentDirectory(256, cur_dir);
    //printf("in read_db(): %s\n", cur_dir);
#endif
    GLint charsRead = read_chars();
    GLint mapsRead = read_maps();
    GLint itemsRead = read_items();

    if (charsRead || mapsRead || itemsRead) {
        exit_glut("Error reading database!");
    }
}
コード例 #16
0
boolean put_handler(MiniWebServer& web_server) {
    web_server.send_error_code(200);
    web_server.end_headers();

    const char* length_str = web_server.get_header_value("Content-Length");
    long length = atol(length_str);
    uint32_t start_time = 0;
    boolean watchdog_start = false;

    EthernetClient client = web_server.get_client();

    if (put_handler_fn) {
        (*put_handler_fn)(web_server, START, NULL, length);
    }

    uint32_t i;
    for (i = 0; i < length && client.connected();) {
        int16_t size = read_chars(web_server, client, (uint8_t*)buffer, 64);
        if (!size) {
            if (watchdog_start) {
                if (millis() - start_time > 30000) {
                    // Exit if there has been zero data from connected client
                    // for more than 30 seconds.
#if DEBUG
                    Serial << F("TWS:There has been no data for >30 Sec.\n");
#endif
                    break;
                }
            } else {
                // We have hit an empty buffer, start the watchdog.
                start_time = millis();
                watchdog_start = true;
            }
            continue;
        }
        i += size;
        // Ensure we re-start the watchdog if we get ANY data input.
        watchdog_start = false;

        if (put_handler_fn) {
            (*put_handler_fn)(web_server, WRITE, buffer, size);
        }
    }
    if (put_handler_fn) {
        (*put_handler_fn)(web_server, END, NULL, 0);
    }

    return true;
}
コード例 #17
0
ファイル: liolib.c プロジェクト: 825126369/2018_Server
static int g_read (lua_State *L, FILE *f, int first) {
  int nargs = lua_gettop(L) - 1;
  int success;
  int n;
  clearerr(f);
  if (nargs == 0) {  /* no arguments? */
    success = read_line(L, f, 1);
    n = first+1;  /* to return 1 result */
  }
  else {  /* ensure stack space for all results and for auxlib's buffer */
    luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
    success = 1;
    for (n = first; nargs-- && success; n++) {
      if (lua_type(L, n) == LUA_TNUMBER) {
        size_t l = (size_t)luaL_checkinteger(L, n);
        success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
      }
      else {
        const char *p = luaL_checkstring(L, n);
        if (*p == '*') p++;  /* skip optional '*' (for compatibility) */
        switch (*p) {
          case 'n':  /* number */
            success = read_number(L, f);
            break;
          case 'l':  /* line */
            success = read_line(L, f, 1);
            break;
          case 'L':  /* line with end-of-line */
            success = read_line(L, f, 0);
            break;
          case 'a':  /* file */
            read_all(L, f);  /* read entire file */
            success = 1; /* always success */
            break;
          default:
            return luaL_argerror(L, n, "invalid format");
        }
      }
    }
  }
  if (ferror(f))
    return luaL_fileresult(L, 0, NULL);
  if (!success) {
    lua_pop(L, 1);  /* remove last result */
    lua_pushnil(L);  /* push nil instead */
  }
  return n - first;
}
コード例 #18
0
void dos_set_mbr_id(struct fdisk_context *cxt)
{
	unsigned long new_id;
	char *ep;
	char ps[64];

	snprintf(ps, sizeof ps, _("New disk identifier (current 0x%08x): "),
		 dos_read_mbr_id(cxt->mbr));

	if (read_chars(ps) == '\n')
		return;

	new_id = strtoul(line_ptr, &ep, 0);
	if (*ep != '\n')
		return;

	dos_write_mbr_id(cxt->mbr, new_id);
	MBRbuffer_changed = 1;
	dos_print_mbr_id(cxt);
}
コード例 #19
0
ファイル: fdisksgilabel.c プロジェクト: SpoilForHawkeye/mount
int
sgi_change_sysid(int i, int sys)
{
	if (sgi_get_num_sectors(i) == 0) /* caught already before, ... */ {
		printf(_("Sorry, only for non-empty partitions you can change the tag.\n"));
		return 0;
	}
	if (((sys != ENTIRE_DISK) && (sys != SGI_VOLHDR))
	    && (sgi_get_start_sector(i)<1)) {
		read_chars(
			_("It is highly recommended that the partition at offset 0\n"
			  "is of type \"SGI volhdr\", the IRIX system will rely on it to\n"
			  "retrieve from its directory standalone tools like sash and fx.\n"
			  "Only the \"SGI volume\" entire disk section may violate this.\n"
			  "Type YES if you are sure about tagging this partition differently.\n"));
		if (strcmp (line_ptr, _("YES\n")))
			return 0;
	}
	sgilabel->partitions[i].id = SSWAP32(sys);
	return 1;
}
コード例 #20
0
ファイル: main.c プロジェクト: ShadowWarden/ShadowRPG
int main(int argc, char ** argv){
	// Declerations
	Map M;
	Char C;
	FILE *fin;
	char input;	

	// File pointer init
	fin = fopen("./resources/maps/test.map","r");
	map_read(fin,&M);
	read_chars(&C);
//	printf("%d\n",ret);
	place_on_map(C,&M);

//	print_deb(M);	
	initscr();
	curs_set(0);
	header();
	keypad(stdscr,TRUE);
	cbreak();
	getch();
	clear();
	print_map(M);
	// Main game loop
	do{
		input = getch();	
		clear();
		int ret = action(&C,&M,input);
		if(ret){
			mvprintw(GRID_Y,0,"Can't do that!");
		}
		print_map(M);
		refresh();
		fflush(stdin);
	}while(input != 'q');
	endwin();
	
	fclose(fin);
	return 0;
}
コード例 #21
0
ファイル: liolib.c プロジェクト: luigiScarso/luatexjit
static int read_all(lua_State * L, FILE * f)
{
    /* attempt to speed up reading whole files */
    long rlen;                  /* how much to read */
    size_t nr;                  /* number of chars actually read */
    long old;                   /* old file location */
    char *p;
    old = ftell(f);
    if (old > 0 && (fseek(f, 0, SEEK_END) >= 0) && ((rlen = ftell(f)) > 0) && rlen < 1000 * 1000 * 100) {       /* 100 MB */
        fseek(f, old, SEEK_SET);
        p = malloc((size_t) rlen);
        if (p != NULL) {
            nr = fread(p, sizeof(char), (size_t) rlen, f);
            lua_pushlstring(L, p, nr);
            free(p);
            return 1;
        }
    }
    if (old > 0)
        fseek(f, old, SEEK_SET);
    return read_chars(L, f, ~((size_t) 0));
}
コード例 #22
0
ファイル: fdisksgilabel.c プロジェクト: dtrebbien/util-linux
void
sgi_set_bootfile(struct fdisk_context *cxt)
{
	printf(_("\nThe current boot file is: %s\n"), sgilabel->boot_file);
	if (read_chars(_("Please enter the name of the new boot file: ")) == '\n') {
		printf(_("Boot file unchanged\n"));
		return;
	}

	if (sgi_check_bootfile(cxt, line_ptr)) {
		size_t i = 0;
		while (i < 16) {
			if ((line_ptr[i] != '\n')	/* in principle caught again by next line */
			    &&  (strlen(line_ptr) > i))
				sgilabel->boot_file[i] = line_ptr[i];
			else
				sgilabel->boot_file[i] = 0;
			i++;
		}
		printf(_("\n\tBootfile is changed to \"%s\".\n"),
		       sgilabel->boot_file);
	}
}
コード例 #23
0
ファイル: luiolib.c プロジェクト: gitrider/wxsj2
static int g_read (lua_State *L, FILE *f, int first) {
  int nargs = lua_gettop(L) - 1;
  int success;
  int n;
  if (nargs == 0) {  /* no arguments? */
    success = read_line(L, f);
    n = first+1;  /* to return 1 result */
  }
  else {  /* ensure stack space for all results and for auxlib's buffer */
    luaL_check_stack(L, nargs+LUA_MINSTACK, "too many arguments");
    success = 1;
    for (n = first; nargs-- && success; n++) {
      if (lua_type(L, n) == LUA_TNUMBER) {
        size_t l = (size_t)lua_tonumber(L, n);
        success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
      }
      else if (lua_type(L, n) == LUA_TSTRING) {
        const char *p = lua_tostring(L, n);
        if (!p || p[0] != '*')
          return luaL_verror(L, "invalid `read' option");
        switch (p[1]) {
          case 'n':  /* number */
            success = read_number(L, f);
            break;
          case 'l':  /* line */
            success = read_line(L, f);
            break;
          case 'a':  /* file */
            read_chars(L, f, ~((size_t)0));  /* read MAX_SIZE_T chars */
            success = 1; /* always success */
            break;
          case 'w':  /* word */
            return luaL_verror(L, "obsolete option `*w'");
            break;
          default:
            return luaL_argerror(L, n, "invalid format");
        }
      }
      else if (lua_type(L, n) == LUA_TUSTRING) {
        const wchar_t *p = lua_toustring(L, n);
        if (!p || p[0] != '*')
          lua_error(L, "invalid `read' option");
        switch (p[1]) {
          case 'n':  /* number */
            success = uread_number(L, f);
            break;
          case 'l':  /* line */
            success = uread_until(L, f, L"\n", 1);  /* read until \n */
            break;
          case 'a':  /* file */
            uread_chars(L, f, ~((size_t)0));  /* read MAX_SIZE_T chars */
            success = 1; /* always success */
            break;
          case 'w':  /* word */
            lua_error(L, "option `*w' is deprecated");
            break;
          case 'u': {  /* read until */
            size_t pl = lua_strlen(L, n) - 2;
            luaL_arg_check(L, 0 < pl && pl <= LUA_MAXUNTIL, n,
                              "invalid read-until length");
            success = uread_until(L, f, p+2, (int)pl);
            break;
          }
          default:
            luaL_argerror(L, n, "invalid format");
            success = 0;  /* to avoid warnings */
        }
      }
    }
  }
  if (!success) {
    lua_pop(L, 1);  /* remove last result */
    lua_pushnil(L);  /* push nil instead */
  }
  return n - first;
}
コード例 #24
0
ファイル: opus_header.c プロジェクト: 0xheart0/vlc
int opus_header_parse(const unsigned char *packet, int len, OpusHeader *h)
{
    char str[9];
    ROPacket p;
    unsigned char ch;
    uint16_t shortval;

    p.data = packet;
    p.maxlen = len;
    p.pos = 0;
    str[8] = 0;
    if (len<19)return 0;
    read_chars(&p, (unsigned char*)str, 8);
    if (memcmp(str, "OpusHead", 8)!=0)
        return 0;

    if (!read_chars(&p, &ch, 1))
        return 0;
    h->version = ch;
    if((h->version&240) != 0) /* Only major version 0 supported. */
        return 0;

    if (!read_chars(&p, &ch, 1))
        return 0;
    h->channels = ch;
    if (h->channels == 0)
        return 0;

    if (!read_uint16(&p, &shortval))
        return 0;
    h->preskip = shortval;

    if (!read_uint32(&p, &h->input_sample_rate))
        return 0;

    if (!read_uint16(&p, &shortval))
        return 0;
    h->gain = (short)shortval;

    if (!read_chars(&p, &ch, 1))
        return 0;
    h->channel_mapping = ch;

    if (h->channel_mapping != 0)
    {
        if (!read_chars(&p, &ch, 1))
            return 0;

        if (ch<1)
            return 0;
        h->nb_streams = ch;

        if (!read_chars(&p, &ch, 1))
            return 0;

        if (ch>h->nb_streams || (ch+h->nb_streams)>255)
            return 0;
        h->nb_coupled = ch;

        /* Multi-stream support */
        for (int i=0;i<h->channels;i++)
        {
            if (!read_chars(&p, &h->stream_map[i], 1))
                return 0;
            if (h->stream_map[i]>(h->nb_streams+h->nb_coupled) && h->stream_map[i]!=255)
                return 0;
        }
    } else {
        if(h->channels>2)
            return 0;
        h->nb_streams = 1;
        h->nb_coupled = h->channels>1;
        h->stream_map[0]=0;
        h->stream_map[1]=1;
    }
    /*For version 0/1 we know there won't be any more data
      so reject any that have data past the end.*/
    if ((h->version==0 || h->version==1) && p.pos != len)
        return 0;
    return 1;
}
コード例 #25
0
ファイル: ascii1.c プロジェクト: FuzzyHobbit/mordor
int read_room(FILE *fp, room *rom)
{
    int             i;
	int		cnt;
	xtag            *xp;
	xtag            **xprev;
	exit_           *ext;
	ctag            *cp;
	ctag            **cprev;
	creature        *crt;
	otag            *op;
	otag            **oprev;
	object          *obj;
	char            *sh, *lo, *ob;
	char		str[10];

/* begin of basic structure */
	fscanf(fp, "%s", str); 
	if (strcmp(str, "#begrom"))
		return(1);
	
	read_short(fp, &rom->rom_num);
	read_chars(fp, rom->name, sizeof(rom->name));
	read_char(fp, &rom->lolevel);
	read_char(fp, &rom->hilevel);
	read_short(fp, &rom->special);
	read_char(fp, &rom->trap);
	read_short(fp, &rom->trapexit);
	read_chars(fp, rom->track, sizeof(rom->track));
	read_chars(fp, rom->flags, sizeof(rom->flags));
	
	for (i = 0; i != 10; i++)
		read_short(fp, &rom->random[i]);
	
	read_char(fp, &rom->traffic);
	
	for (i = 0; i != 10; i++)
		read_lasttime(fp, &rom->perm_mon[i]);
	for (i = 0; i != 10; i++)
		read_lasttime(fp, &rom->perm_obj[i]);
	
	read_long(fp, &rom->beenhere);
	read_long(fp, &rom->established);
	read_short(fp, &rom->death_rom);
	read_char(fp, &rom->zone_type);
	read_char(fp, &rom->zone_subtype);
	read_char(fp, &rom->env_type);
	read_short(fp,&rom->special1);
	read_long(fp,&rom->special2);

/* end basic structure */

    rom->short_desc = 0;
    rom->long_desc  = 0;
	rom->obj_desc = 0;
    rom->first_ext = 0;
    rom->first_obj = 0;
    rom->first_mon = 0;
    rom->first_ply = 0;

/* short description */
	read_int(fp, &cnt);

    if(cnt) {
		sh = (char *)malloc(cnt);
		if(sh) {
			read_chars(fp, sh, cnt);
			rom->short_desc = sh;
		}
		else
			merror("read_rom", FATAL);
        }

/* long description */
	read_int(fp, &cnt);

    if(cnt) {
        lo = (char *)malloc(cnt);
        if(lo) {
			read_chars(fp, lo, cnt);
			rom->long_desc = lo;
		}
		else
            merror("read_rom", FATAL);
		}

/* object description */
	read_int(fp, &cnt);

	if(cnt) {
		ob = (char *)malloc(cnt);
		if(ob) {
			read_chars(fp, ob, cnt);
			rom->obj_desc = ob;
		}
		else
			merror("read_rom", FATAL);
	}

/* exits */
	read_int(fp, &cnt);

    xprev = &rom->first_ext;
    while(cnt > 0) {
		cnt--;
		xp = (xtag *)malloc(sizeof(xtag));
		if(xp) {
				ext = (exit_ *)malloc(sizeof(exit_));
				if(ext) {
				/* begin exit structure */
					read_chars(fp, ext->name, sizeof(ext->name));
					read_short(fp, &ext->room);
					read_chars(fp, ext->flags, sizeof(ext->flags));
					read_lasttime(fp, &ext->ltime);
					read_lasttime(fp, &ext->random);
					read_chars(fp,ext->rand_mesg[0],sizeof(ext->rand_mesg[0]));
					read_chars(fp,ext->rand_mesg[1],sizeof(ext->rand_mesg[1]));
					read_char(fp, &ext->key);
					/* end exit structure */
                    xp->ext = ext;
                    xp->next_tag = 0;
                    *xprev = xp;
                    xprev = &xp->next_tag;
                }
                else
					merror("read_rom", FATAL);
        }
        else
			merror("read_rom", FATAL);
    }

/* monsters/creatures */
	read_int(fp, &cnt);

        cprev = &rom->first_mon;
        while(cnt > 0) {
                cnt--;
                cp = (ctag *)malloc(sizeof(ctag));
                if(cp) {
                        crt = (creature *)malloc(sizeof(creature));
                        if(crt) {
                                read_creature(fp, crt);
                                crt->parent_rom = rom;
                                cp->crt = crt;
                                cp->next_tag = 0;
                                *cprev = cp;
                                cprev = &cp->next_tag;
                        }
                        else
                                merror("read_rom", FATAL);
                }
                else
                        merror("read_rom", FATAL);
        }

/* items/objects */
	read_int(fp, &cnt);

        oprev = &rom->first_obj;
        while(cnt > 0) {
                cnt--;
                op = (otag *)malloc(sizeof(otag));
                if(op) {
                        obj = (object *)malloc(sizeof(object));
                        if(obj) {
                                read_object(fp, obj);
                                obj->parent_rom = rom;
                                op->obj = obj;
                                op->next_tag = 0;
                                *oprev = op;
                                oprev = &op->next_tag;
                        }
                        else
                                merror("read_rom", FATAL);
                }
                else
                        merror("read_rom", FATAL);
        }

	fscanf(fp, "%s", str); 
	if (strcmp(str, "#endrom"))
		return(1);

        if(error)
                return(1);
        else
                return(0);
}
コード例 #26
0
ファイル: liolib.c プロジェクト: RonDePrez/opentx
static int io_read (lua_State *L) {
  LStream *p = tolstream(L);
  size_t l = (size_t)lua_tointeger(L, 2);
  read_chars(L, &p->f, l);
  return 1;
}
コード例 #27
0
int main(int argc, char **argv)
{
    int rc;

    /* Initialize libusb
     */
    rc = libusb_init(NULL);
    if (rc < 0) {
        fprintf(stderr, "Error initializing libusb: %s\n", libusb_error_name(rc));
        exit(1);
    }

    /* Set debugging output to max level.
     */
    libusb_set_debug(NULL, 3);

    /* Look for a specific device and open it.
     */
    devh = libusb_open_device_with_vid_pid(NULL, VENDOR_ID, PRODUCT_ID);
    if (!devh) {
        fprintf(stderr, "Error finding USB device\n");
        goto out;
    }

    /* As we are dealing with a CDC-ACM device, it's highly probable that
     * Linux already attached the cdc-acm driver to this device.
     * We need to detach the drivers from all the USB interfaces. The CDC-ACM
     * Class defines two interfaces: the Control interface and the
     * Data interface.
     */
    for (int if_num = 0; if_num < 2; if_num++) {
        if (libusb_kernel_driver_active(devh, if_num)) {
            libusb_detach_kernel_driver(devh, if_num);
        }
        rc = libusb_claim_interface(devh, if_num);
        if (rc < 0) {
            fprintf(stderr, "Error claiming interface: %s\n",
                    libusb_error_name(rc));
            goto out;
        }
    }

    /* Start configuring the device:
     * - set line state
     */
    rc = libusb_control_transfer(devh, 0x21, 0x22, ACM_CTRL_DTR | ACM_CTRL_RTS,
                                0, NULL, 0, 0);
    if (rc < 0) {
        fprintf(stderr, "Error during control transfer: %s\n",
                libusb_error_name(rc));
    }

    /* - set line encoding: here 9600 8N1
     * 9600 = 0x2580 ~> 0x80, 0x25 in little endian
     */
    unsigned char encoding[] = { 0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08 };
    rc = libusb_control_transfer(devh, 0x21, 0x20, 0, 0, encoding,
                                sizeof(encoding), 0);
    if (rc < 0) {
        fprintf(stderr, "Error during control transfer: %s\n",
                libusb_error_name(rc));
    }

    /* We can now start sending or receiving data to the device
     */
    unsigned char buf[65];
    int len;
    
    while(1) {
        write_char('t');
        len = read_chars(buf, 64);
        buf[len] = 0;
        fprintf(stdout, "Received: \"%s\"\n", buf);
        sleep(1);
    }

    libusb_release_interface(devh, 0);

out:
    if (devh)
            libusb_close(devh);
    libusb_exit(NULL);
    return rc;
}
コード例 #28
0
ファイル: opus_header.c プロジェクト: rillian/krad_ebml
int opus_header_parse(const unsigned char *packet, int len, OpusHeader *h)
{
   int i;
   char str[9];
   ROPacket p;
   unsigned char ch;
   opus_uint16 shortval;
   
   p.data = packet;
   p.maxlen = len;
   p.pos = 0;
   str[8] = 0;
   read_chars(&p, (unsigned char*)str, 8);
   if (strcmp(str, "OpusHead")!=0)
      return 0;
   
   if (!read_chars(&p, &ch, 1))
      return 0;
   h->version = ch;
   
   if (!read_chars(&p, &ch, 1))
      return 0;
   h->channels = ch;
   if (h->channels == 0)
      return 0;

   if (!read_uint16(&p, &shortval))
      return 0;
   h->preskip = shortval;

   if (!read_uint32(&p, &h->input_sample_rate))
      return 0;

   if (!read_uint16(&p, &shortval))
      return 0;
   h->gain = (short)shortval;

   if (!read_chars(&p, &ch, 1))
      return 0;
   h->channel_mapping = ch;
   
   if (h->channel_mapping != 0)
   {
      if (!read_chars(&p, &ch, 1))
         return 0;
      h->nb_streams = ch;
      
      if (!read_chars(&p, &ch, 1))
         return 0;
      h->nb_coupled = ch;
      
      /* Multi-stream support */
      for (i=0;i<h->channels;i++)
      {
         if (!read_chars(&p, &h->stream_map[i], 1))
            return 0;
      }
   }
   if (h->version==0 && p.pos != len)
      return 0;
   return 1;
}
コード例 #29
0
ファイル: ascii1.c プロジェクト: FuzzyHobbit/mordor
int read_creature_basic(FILE *fp, int *index, creature *crt)
{
	int	i;
	char	str[10];
	
/* begin of creature data */

	read_int(fp, index);  /* creature # */
	
	read_chars(fp, crt->name, sizeof(crt->name));
	read_chars(fp, crt->description, sizeof(crt->description));
	read_chars(fp, crt->talk, sizeof(crt->talk));
	read_chars(fp, crt->password, sizeof(crt->password));
	for (i = 0; i != 3; i++)
		read_chars(fp, crt->key[i], sizeof(crt->key[i]));
	
	read_short(fp, &crt->fd);
	
	read_char(fp, &crt->level);
	read_char(fp, &crt->type);
	read_char(fp, &crt->class);
	read_char(fp, &crt->race);
	read_char(fp, &crt->numwander);
	
	read_short(fp, &crt->alignment);
	
	read_char(fp, &crt->strength);
	read_char(fp, &crt->dexterity);
	read_char(fp, &crt->constitution);
	read_char(fp, &crt->intelligence);
	read_char(fp, &crt->piety);
	
	read_short(fp, &crt->hpmax);
	read_short(fp, &crt->hpcur);
	read_short(fp, &crt->mpmax);
	read_short(fp, &crt->mpcur);
	
	read_char(fp, &crt->armor);
	read_char(fp, &crt->thaco);
	
	read_long(fp, &crt->experience);
	read_long(fp, &crt->gold);
	
	read_short(fp, &crt->ndice);
	read_short(fp, &crt->sdice);
	read_short(fp, &crt->pdice);

	read_short(fp, &crt->special);
	
	for (i=0; i != 6; i++)
		read_long(fp, &crt->proficiency[i]);
	for (i=0; i != 8; i++)
		read_long(fp, &crt->realm[i]);

	read_chars(fp, crt->spells, sizeof(crt->spells));
	read_chars(fp, crt->flags, sizeof(crt->flags));
	read_chars(fp, crt->quests, sizeof(crt->quests));
	read_char(fp, &crt->questnum);
	
	for (i=0; i != 10; i++)
		read_short(fp, &crt->carry[i]);
	read_short(fp, &crt->rom_num);
	
	for (i=0; i != 10; i++) {
		read_char(fp, &crt->daily[i].max);
		read_char(fp, &crt->daily[i].cur);
		read_long(fp, &crt->daily[i].ltime);
	}

	for (i=0; i < 45; i++)
		read_lasttime(fp, &crt->lasttime[i]);
	read_long(fp, &crt->bank_balance);
	read_chars(fp,crt->title, sizeof(crt->title));
	for(i=0;i!=5;i++)
		read_short(fp, &crt->misc_stats[i]);
	read_short(fp, &crt->clanindex);
	read_long(fp, &crt->clanexp);
	read_char(fp, &crt->guildtype);
	read_long(fp, &crt->guildexp);
	read_short(fp, &crt->special1);
	read_long(fp, &crt->special2);
	
/* end of creature data */
	return(0);
}