static void *
parse_input_open(int *size)
{
   char path[4096];
   char key[4096];
   char line[4096];
   int path_len, key_len;
   Msg_Open msg;
   char *buf;
   int file_id;

   _read_line(line, sizeof(line));
   path_len = _read_line(path, sizeof(path));
   key_len = _read_line(key, sizeof(key));

   sscanf(line, "%d", &file_id);

   buf = malloc(sizeof(msg) + path_len + key_len);

   msg.base.rid = _rid_count++;
   msg.base.type = CSERVE2_OPEN;
   msg.file_id = file_id;
   msg.path_offset = 0;
   msg.key_offset = path_len;

   memcpy(buf, &msg, sizeof(msg));
   memcpy(buf + sizeof(msg), path, path_len);
   memcpy(buf + sizeof(msg) + path_len, key, key_len);

   *size = sizeof(msg) + path_len + key_len;

   return buf;
}
示例#2
0
文件: liolib.c 项目: richq/rockbox
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, 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)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, 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 (!success) {
    lua_pop(L, 1);  /* remove last result */
    lua_pushnil(L);  /* push nil instead */
  }
  return n - first;
}
static void *
parse_input_setopts(int *size)
{
   Msg_Setopts *msg;
   char line[4096];
   int file_id, image_id;
   double dpi;
   int w, h;
   int scale;
   int rx, ry, rw, rh;
   int orientation;

   // reading file_id, image_id
   _read_line(line, sizeof(line));
   sscanf(line, "%d %d", &file_id, &image_id);

   // reading load dpi
   _read_line(line, sizeof(line));
   dpi = atof(line);

   // reading load size
   _read_line(line, sizeof(line));
   sscanf(line, "%d %d", &w, &h);

   // reading load scale down
   _read_line(line, sizeof(line));
   sscanf(line, "%d", &scale);

   // reading load region
   _read_line(line, sizeof(line));
   sscanf(line, "%d %d %d %d", &rx, &ry, &rw, &rh);

   // reading orientation
   _read_line(line, sizeof(line));
   sscanf(line, "%d", &orientation);


   msg = calloc(1, sizeof(*msg));

   msg->base.rid = _rid_count++;
   msg->base.type = CSERVE2_SETOPTS;
   msg->file_id = file_id;
   msg->image_id = image_id;
   msg->opts.dpi = dpi;
   msg->opts.w = w;
   msg->opts.h = h;
   msg->opts.scale_down = scale;
   msg->opts.rx = rx;
   msg->opts.ry = ry;
   msg->opts.rw = rw;
   msg->opts.rh = rh;
   msg->opts.orientation = !!orientation;

   *size = sizeof(*msg);

   return msg;
}
static void *
parse_input_close(int *size)
{
   Msg_Close *msg;
   char line[4096];
   int file_id;

   // read file_id
   _read_line(line, sizeof(line));
   sscanf(line, "%d", &file_id);

   msg = calloc(1, sizeof(*msg));

   msg->base.rid = _rid_count++;
   msg->base.type = CSERVE2_CLOSE;
   msg->file_id = file_id;

   *size = sizeof(*msg);

   return msg;
}
static void *
parse_input_unload(int *size)
{
   Msg_Unload *msg;
   char line[4096];
   int image_id;

   // read image_id
   _read_line(line, sizeof(line));
   sscanf(line, "%d", &image_id);

   msg = calloc(1, sizeof(*msg));

   msg->base.rid = _rid_count++;
   msg->base.type = CSERVE2_UNLOAD;
   msg->image_id = image_id;

   *size = sizeof(*msg);

   return msg;
}
示例#6
0
static void *
parse_input_open(int *size)
{
   char path[4096];
   char key[4096];
   char line[4096];
   int path_len, key_len;
   Msg_Open msg;
   char *buf;
   int file_id;

   // TODO: Add load opts

   _read_line(line, sizeof(line));
   path_len = _read_line(path, sizeof(path));
   key_len = _read_line(key, sizeof(key));

   sscanf(line, "%d", &file_id);

   buf = malloc(sizeof(msg) + path_len + key_len);

   msg.base.rid = _rid_count++;
   msg.base.type = CSERVE2_OPEN;
   msg.file_id = file_id;
   msg.path_offset = 0;
   msg.key_offset = path_len;
   msg.has_load_opts = EINA_FALSE;
   msg.image_id = 0;

   memcpy(buf, &msg, sizeof(msg));
   memcpy(buf + sizeof(msg), path, path_len);
   memcpy(buf + sizeof(msg) + path_len, key, key_len);

   *size = sizeof(msg) + path_len + key_len;

   return buf;

#if 0
   // TODO: Adapt the following code
   Msg_Setopts *msg;
   char line[4096];
   int file_id, image_id;
   double dpi;
   int w, h;
   int scale;
   int rx, ry, rw, rh;
   int scale_src_x, scale_src_y, scale_src_w, scale_src_h;
   int scale_dst_w, scale_dst_h;
   int scale_smooth;
   int scale_hint;
   int degree;
   int orientation;

   // reading file_id, image_id
   _read_line(line, sizeof(line));
   sscanf(line, "%d %d", &file_id, &image_id);

   // reading load dpi
   _read_line(line, sizeof(line));
   dpi = atof(line);

   // reading load size
   _read_line(line, sizeof(line));
   sscanf(line, "%d %d", &w, &h);

   // reading load scale down
   _read_line(line, sizeof(line));
   sscanf(line, "%d", &scale);

   // reading load region
   _read_line(line, sizeof(line));
   sscanf(line, "%d %d %d %d", &rx, &ry, &rw, &rh);

   // reading original image's source coord
   _read_line(line, sizeof(line));
   sscanf(line, "%d %d", &scale_src_x, &scale_src_y);

   // reading original size
   _read_line(line, sizeof(line));
   sscanf(line, "%d %d", &scale_src_w, &scale_src_h);

   // reading scale size
   _read_line(line, sizeof(line));
   sscanf(line, "%d %d", &scale_dst_w, &scale_dst_h);

   // reading scale smooth
   _read_line(line, sizeof(line));
   sscanf(line, "%d", &scale_smooth);

   // reading scale hint
   _read_line(line, sizeof(line));
   sscanf(line, "%d", &scale_hint);

   // reading degree
   _read_line(line, sizeof(line));
   sscanf(line, "%d", &degree);

   // reading orientation
   _read_line(line, sizeof(line));
   sscanf(line, "%d", &orientation);

   msg = calloc(1, sizeof(*msg));

   msg->base.rid = _rid_count++;
   msg->base.type = CSERVE2_SETOPTS;
   msg->file_id = file_id;
   msg->image_id = image_id;
   msg->opts.dpi = dpi;
   msg->opts.w = w;
   msg->opts.h = h;
   msg->opts.scale_down_by = scale;
   msg->opts.region.x = rx;
   msg->opts.region.y = ry;
   msg->opts.region.w = rw;
   msg->opts.region.h = rh;
   msg->opts.scale_load.src_x = scale_src_x;
   msg->opts.scale_load.src_y = scale_src_y;
   msg->opts.scale_load.src_w = scale_src_w;
   msg->opts.scale_load.src_h = scale_src_h;
   msg->opts.scale_load.dst_w = scale_dst_w;
   msg->opts.scale_load.dst_h = scale_dst_h;
   msg->opts.scale_load.smooth = scale_smooth;
   msg->opts.scale_load.scale_hint = scale_hint;
   msg->opts.degree = degree;
   msg->opts.orientation = !!orientation;

   *size = sizeof(*msg);

   return msg;
#endif
}
/*
 * Read sensors values stored in data file
 */
static bool
_read_state_cb(struct sml_object *sml, void *data)
{
    uint16_t i, ind, input_len, output_len, tokens_len;
    float value;
    char line[LINE_LEN];
    char **tokens;
    Context *ctx = data;
    struct sml_variables_list *input_list, *output_list;
    struct sml_variable *sml_variable;
    char var_name[SML_VARIABLE_NAME_MAX_LEN + 1];

    input_list = sml_get_input_list(sml);
    output_list = sml_get_output_list(sml);
    input_len = sml_variables_list_get_length(sml, output_list);
    output_len = sml_variables_list_get_length(sml, output_list);

    if (!_read_line(ctx->f, line, LINE_LEN))
        return false;

    tokens = g_strsplit(line, " ", 0);
    if (!tokens)
        return false;

    tokens_len = g_strv_length(tokens);
    if (tokens_len < input_len + output_len) {
        fprintf(stderr, "Line in file has not enought data\n");
        g_strfreev(tokens);
        return false;
    }

    printf("Reading sensors: Inputs {");
    for (i = 0; i < input_len; i++) {
        sml_variable = sml_variables_list_index(sml, input_list, i);
        value = atof(tokens[i]);
        sml_variable_set_value(sml, sml_variable, value);
        if (i != 0)
            printf(", ");
        if (!sml_variable_get_name(sml, sml_variable, var_name,
            sizeof(var_name)))
            printf("%s: %f", var_name, value);
    }
    printf("}, Outputs {");

    for (i = 0; i < output_len; i++) {
        sml_variable = sml_variables_list_index(sml, output_list, i);
        value = atof(tokens[input_len + i]);
        ctx->outputs[i] = value;
        sml_variable_set_value(sml, sml_variable, value);
        if (i != 0)
            printf(", ");
        if (!sml_variable_get_name(sml, sml_variable, var_name,
            sizeof(var_name)))
            printf("%s: %f", var_name, value);
    }
    printf("}, Expected {");

    for (i = 0; i < output_len; i++) {
        ind = input_len + output_len + i;
        sml_variable = sml_variables_list_index(sml, output_list, i);
        if (i != 0)
            printf(", ");
        if (!sml_variable_get_name(sml, sml_variable, var_name,
            sizeof(var_name)))
            printf("%s: ", var_name);
        if (ind < tokens_len && tokens[ind][0] != 0 && tokens[ind][0] != '?') {
            ctx->expected_outputs[i] = atof(tokens[ind]);
            printf("%f", ctx->expected_outputs[i]);
        } else {
            printf("?");
            ctx->expected_outputs[i] = NAN;
        }
    }
    printf("}\n");
    g_strfreev(tokens);

    for (i = 0; i < output_len; i++)
        ctx->output_state_changed_outputs[i] = NAN;

    ctx->read_count++;
    return true;
}