Пример #1
0
static void json_object_fini(void) {
  struct lh_entry *ent;
  if(MC_GET_DEBUG()) {
    if (json_object_table->count) {
      MC_DEBUG("json_object_fini: %d referenced objects at exit\n",
  	       json_object_table->count);
      lh_foreach(json_object_table, ent) {
        struct json_object* obj = (struct json_object*)ent->v;
        MC_DEBUG("\t%s:%p\n", json_type_to_name(obj->o_type), obj);
      }
    }
  }
Пример #2
0
static int build_token_into(struct access_token *token, struct json_object *obj)
{
	int err;

	verbose(FIREHOSE, "%s(): Building token from %p (type '%s')\n",
		__func__, obj, json_type_to_name(json_object_get_type(obj)));

	err = copy_value_to_token_string(&token->access_token, obj, "access_token");
	if (err != 0) {
		verbose(ERROR, "%s(): No 'access_token' member in token json (%s)\n",
			__func__, strerror(err));
		return err;
	}

	err = copy_value_to_token_int(&token->expires_in, obj, "expires_in");
	if (err != 0) {
		verbose(ERROR, "%s(): No 'expires_in' member in token json (%s)\n",
			__func__, strerror(err));
	}

	return err;
}
void
TrexRpcCommand::check_field_type_common(const Json::Value &field, const std::string &name, field_type_e type, Json::Value &result) {
    std::stringstream ss;

    /* first check if field exists */
    if (field == Json::Value::null) {
        ss << "field '" << name << "' is missing";
        generate_parse_err(result, ss.str());
    }

    bool rc = true;

    switch (type) {
    case FIELD_TYPE_BYTE:
        if ( (!field.isUInt()) || (field.asInt() > 0xFF)) {
            rc = false;
        }
        break;

    case FIELD_TYPE_UINT16:
        if ( (!field.isUInt()) || (field.asInt() > 0xFFFF)) {
            rc = false;
        }
        break;

    case FIELD_TYPE_UINT32:
        if ( (!field.isUInt()) || (field.asUInt() > 0xFFFFFFFF)) {
            rc = false;
        }
        break;

    case FIELD_TYPE_UINT64:
        if (!field.isUInt64()) {
            rc = false;
        }
        break;

    case FIELD_TYPE_BOOL:
        if (!field.isBool()) {
            rc = false;
        }
        break;

    case FIELD_TYPE_INT:
        if (!field.isInt()) {
            rc = false;
        }
        break;

    case FIELD_TYPE_DOUBLE:
        if (!field.isDouble()) {
            rc = false;
        }
        break;

    case FIELD_TYPE_OBJ:
        if (!field.isObject()) {
            rc = false;
        }
        break;

    case FIELD_TYPE_STR:
        if (!field.isString()) {
            rc = false;
        }
        break;

    case FIELD_TYPE_ARRAY:
        if (!field.isArray()) {
            rc = false;
        }
        break;

    default:
        throw TrexRpcException("unhandled type");
        break;

    }
    if (!rc) {
        ss << "error at offset: " << field.getOffsetStart() << " - '" << name << "' is '" << json_type_to_name(field) << "', expecting '" << type_to_str(type) << "'";
        generate_parse_err(result, ss.str());
    }

}
Пример #4
0
bool settings_read(void)
{
  bool rv;
  bool bind_defaults;
  bool nonexistent_ok;

  rv             = true;
  bind_defaults  = true;
  nonexistent_ok = false;
  
  /* Read and delete the old versions first if it exists. */
  settings_read_v1();

  if(settings_read_v2())
    {
      bind_defaults = false;
      nonexistent_ok = true; // If we managed to read the old .lua file, then failing to read the new .json file should not cause any error messages.
    }


  /* Read the new version if it exists. */
  char * json;
  int    json_size;

  snprintf(filename, sizeof filename, "%s", get_save_filename("settings.json"));
  if(read_file(filename, &json, &json_size) == true)
    {
      enum json_tokener_error e;
      struct json_object * obj;

      obj = json_tokener_parse_verbose(json, &e);
      if(obj != NULL)
        {
          bool ok;
          struct json_object_iterator it;
          struct json_object_iterator itEnd;
          int active_questline;

          ok    = true;
          it    = json_object_iter_begin(obj);
          itEnd = json_object_iter_end(obj);

          active_questline = globals.active_questline;

          while(ok == true && !json_object_iter_equal(&it, &itEnd))
            {
              const char *         name;
              struct json_object * value;
              enum json_type       type;
              bool                 dummy_boolean;
              
              name  = json_object_iter_peek_name(&it);
              value = json_object_iter_peek_value(&it);
              type = json_object_get_type(value);

              struct
              {
                const char *   name;
                enum json_type type;
                bool (*callback)(struct json_object * value, void * value_ptr);
                void *         value_ptr;
              } stuff[] =
                  {
                    { "Fullscreen",                 json_type_boolean, cb_bool,                 &globals.fullscreen                   },
                    { "OpenGL",                     json_type_boolean, cb_bool,                 &globals.opengl                       },
                    { "OpenGL_POT",                 json_type_boolean, cb_opengl_pot,           NULL                                  },               
                    { "OpenGL_compressed_textures", json_type_boolean, cb_bool,                 &globals.opengl_compressed_textures   },
                    { "SFX",                        json_type_boolean, cb_bool,                 &globals.use_sfx                      },
                    { "Music",                      json_type_boolean, cb_bool,                 &globals.use_music                    },
                    { "SFX_max_channels",           json_type_int,     cb_int,                  &globals.max_channels                 },
                    { "Sound_volume",               json_type_int,     cb_sound_volume,         NULL                                  },
                    { "Joysticks",                  json_type_boolean, cb_bool,                 &globals.use_joysticks                },
                    { "CaveSelection",              json_type_string,  cb_cave_selection,       NULL                                  },
                    { "CaveSelectionLevel",         json_type_int,     cb_cave_selection_level, NULL                                  },
                    { "ShowFPS",                    json_type_boolean, cb_bool,                 &globals.fps_counter_active           },
                    { "GameMode",                   json_type_string,  cb_game_mode,            NULL                                  },
                    { "ActiveQuestline",            json_type_int,     cb_int,                  &active_questline                     },
                    { "TitleMidarea",               json_type_string,  cb_title_midarea,        NULL                                  },
                    { "IronGirlMode",               json_type_boolean, cb_bool,                 &globals.iron_girl_mode               },
                    { "Locale",                     json_type_string,  cb_locale,               NULL                                  },
                    { "MapTilting",                 json_type_boolean, cb_bool,                 &globals.map_tilting                  },
                    { "SmoothClassicMode",          json_type_boolean, cb_bool,                 &globals.smooth_classic_mode          },
                    { "Bindings",                   json_type_array,   cb_bindings,             NULL                                  },
                    { "Themes",                     json_type_array,   cb_themes,               NULL                                  },
                    { "WriteSettings",              json_type_boolean, cb_bool,                 &globals.write_settings               },
                    { "TestsCompleted",             json_type_boolean, cb_bool,                 &dummy_boolean                        }, // Ignored.
                    { NULL,                         json_type_int,     NULL,                    NULL                                  }
                  };
              
              int ind;

              ind = -1;
              for(int i = 0; ind == -1 && stuff[i].name != NULL; i++)
                if(!strcmp(stuff[i].name, name))
                  ind = i;

              if(ind >= 0)
                {
                  if(stuff[ind].type == type)
                    {
                      ok = stuff[ind].callback(value, stuff[ind].value_ptr);

                      /* Don't bind the default keybindings if we were able to read them from the settings.json. */
                      if(ok == true && !strcmp(name, "Bindings"))
                        bind_defaults = false;
                    }
                  else
                    {
                      fprintf(stderr, "%s: Incorrect value type %s for '%s', expected %s.\n", filename, json_type_to_name(type), name, json_type_to_name(stuff[ind].type));
                      ok = false;
                    }
                }
              else
                {
                  fprintf(stderr, "%s: Failed to parse: unknown variable '%s'.\n", filename, name);
                  ok = false;
                }

              json_object_iter_next(&it);
            }

          if(ok == true)
            globals.active_questline = active_questline;
          else
            rv = false;
        }
      else
        {
          fprintf(stderr, "%s: Failed to parse: %d: %s\n", filename, (int) e, json_tokener_error_desc(e));
        }
      free(json);
    }
  else
    {
      if(nonexistent_ok == false)
        fprintf(stderr, "%s: Failed to read '%s'.\n", __FUNCTION__, filename);
    }


  if(bind_defaults)
    ui_bindings_default(true);

  if(globals.iron_girl_mode)
    theme_set(THEME_TRAIT, "iron-girl");
  
  return rv;
}
Пример #5
0
static void test_incremental_parse()
{
	json_object *new_obj;
	enum json_tokener_error jerr;
	json_tokener *tok;
	const char *string_to_parse;
	int ii;
	int num_ok, num_error;

	num_ok = 0;
	num_error = 0;

	printf("Starting incremental tests.\n");
	printf("Note: quotes and backslashes seen in the output here are literal values passed\n");
	printf("     to the parse functions.  e.g. this is 4 characters: \"\\f\"\n");

	string_to_parse = "{ \"foo"; /* } */
	printf("json_tokener_parse(%s) ... ", string_to_parse);
	new_obj = json_tokener_parse(string_to_parse);
	if (new_obj == NULL) printf("got error as expected\n");

	/* test incremental parsing in various forms */
	tok = json_tokener_new();
	for (ii = 0; incremental_steps[ii].string_to_parse != NULL; ii++)
	{
		int this_step_ok = 0;
		struct incremental_step *step = &incremental_steps[ii];
		int length = step->length;
		int expected_char_offset = step->char_offset;

		if (step->reset_tokener & 2)
			json_tokener_set_flags(tok, JSON_TOKENER_STRICT);
		else
			json_tokener_set_flags(tok, 0);

		if (length == -1)
			length = strlen(step->string_to_parse);
		if (expected_char_offset == -1)
			expected_char_offset = length;

		printf("json_tokener_parse_ex(tok, %-12s, %3d) ... ",
			step->string_to_parse, length);
		new_obj = json_tokener_parse_ex(tok, step->string_to_parse, length);

		jerr = json_tokener_get_error(tok);
		if (step->expected_error != json_tokener_success)
		{
			if (new_obj != NULL)
				printf("ERROR: invalid object returned: %s\n",
					json_object_to_json_string(new_obj));
			else if (jerr != step->expected_error)
				printf("ERROR: got wrong error: %s\n",
					json_tokener_error_desc(jerr));
			else if (tok->char_offset != expected_char_offset)
				printf("ERROR: wrong char_offset %d != expected %d\n",
					tok->char_offset,
					expected_char_offset);
			else
			{
				printf("OK: got correct error: %s\n", json_tokener_error_desc(jerr));
				this_step_ok = 1;
			}
		}
		else
		{
			if (new_obj == NULL)
				printf("ERROR: expected valid object, instead: %s\n",
					json_tokener_error_desc(jerr));
			else if (tok->char_offset != expected_char_offset)
				printf("ERROR: wrong char_offset %d != expected %d\n",
					tok->char_offset,
					expected_char_offset);
			else
			{
				printf("OK: got object of type [%s]: %s\n",
					json_type_to_name(json_object_get_type(new_obj)),
					json_object_to_json_string(new_obj));
				this_step_ok = 1;
			}
		}

		if (new_obj)
			json_object_put(new_obj);

		if (step->reset_tokener & 1)
			json_tokener_reset(tok);

		if (this_step_ok)
			num_ok++;
		else
			num_error++;
	}

	json_tokener_free(tok);

	printf("End Incremental Tests OK=%d ERROR=%d\n", num_ok, num_error);

	return;
}