Example #1
0
struct nh_roles_info *nhnet_get_roles(void)
{
    struct nh_roles_info *ri = NULL;
    json_t *jmsg, *jroles_m, *jroles_f, *jraces, *jgenders, *jaligns, *jmatrix;
    nh_bool *matrix;
    int i, size;

    if (!nhnet_active())
        return nh_get_roles();

    if (!api_entry())
        return NULL;

    jmsg = send_receive_msg("get_roles", json_object());
    ri = xmalloc(sizeof(struct nh_roles_info));
    if (json_unpack(jmsg, "{si,si,si,si,si,si,si,si,so,so,so,so,so,so}",
                    "num_roles", &ri->num_roles, "num_races", &ri->num_races,
                    "num_genders", &ri->num_genders, "num_aligns", &ri->num_aligns,
                    "def_role", &ri->def_role, "def_race", &ri->def_race,
                    "def_gend", &ri->def_gend, "def_align", &ri->def_align,
                    "rolenames_m", &jroles_m, "rolenames_f", &jroles_f,
                    "racenames", &jraces, "gendnames", &jgenders,
                    "alignnames", &jaligns, "matrix", &jmatrix) == -1 ||
            !json_is_array(jroles_m) || !json_is_array(jroles_f) ||
            !json_is_array(jraces)   || !json_is_array(jgenders) ||
            !json_is_array(jaligns)  || !json_is_array(jmatrix)  ||
            json_array_size(jroles_m) != ri->num_roles ||
            json_array_size(jroles_f) != ri->num_roles ||
            json_array_size(jraces)   != ri->num_races ||
            json_array_size(jgenders) != ri->num_genders ||
            json_array_size(jaligns)  != ri->num_aligns) {
        print_error("Incorrect return object in nhnet_get_roles");
        ri = NULL;
    } else {
        ri->rolenames_m = read_string_array(jroles_m);
        ri->rolenames_f = read_string_array(jroles_f);
        ri->racenames = read_string_array(jraces);
        ri->gendnames = read_string_array(jgenders);
        ri->alignnames = read_string_array(jaligns);

        size = json_array_size(jmatrix);
        matrix = xmalloc(size * sizeof(nh_bool));
        for (i = 0; i < size; i++)
            matrix[i] = json_integer_value(json_array_get(jmatrix, i));
        ri->matrix = matrix;
    }

    json_decref(jmsg);
    api_exit();
    return ri;
}
Example #2
0
/**
 * Loads the handler's data field. Allowed fields are: <UL>
 *  <LI> JSR211_FIELD_TYPES, <LI> JSR211_FIELD_SUFFIXES, 
 *  <LI> JSR211_FIELD_ACTIONS, <LI> JSR211_FIELD_LOCALES, 
 *  <LI> JSR211_FIELD_ACTION_MAP, <LI> and JSR211_FIELD_ACCESSES. </UL>
 *
 * @param id requested handler ID.
 * @param field_id requested field.
 * @param result output structure where requested array is placed to.
 *  <br>Use @link jsr211_fillStringArray function to fill this structure.
 * @return status of the operation
 */
jsr211_result jsr211_get_handler_field(const pcsl_string* id, 
                                    jsr211_field field_id, 
                                    /*OUT*/ JSR211_RESULT_STRARRAY* result) {
  int result_len = 0;
  pcsl_string* buf = NULL;

  if (record_struct[CHR_INDEX((int)(field_id))] != field_array) {
    return JSR211_FAILED;
  }

  if (open_table_file(0) != JSR211_OK) {
    return JSR211_FAILED;
  }

  if (find_next_by_field(NULL, JSR211_FIELD_ID, id, JSR211_TRUE, find_exact) 
                                                                        == -1) {
    close_table_file();
    return JSR211_FAILED;
  }

  /* reading field */
  position_field(CHR_INDEX((int)(field_id)));
  read_string_array(&result_len, &buf);
  if (result_len > 0) {
    jsr211_fillStringArray(buf, result_len, result);
    free_pcsl_string_list(buf, result_len);
  }

  close_table_file();
  return JSR211_OK;
}
Example #3
0
void bear_read_message(int fd, bear_message_t * e)
{
    e->pid = read_pid(fd);
    e->ppid = read_pid(fd);
    e->fun = read_string(fd);
    e->cwd = read_string(fd);
    e->cmd = read_string_array(fd);
}
Example #4
0
struct pb_Parameters *
pb_ReadParameters(int *_argc, char **argv)
{
  char *err_message;
  struct argparse ap;
  struct pb_Parameters *ret =
    (struct pb_Parameters *)malloc(sizeof(struct pb_Parameters));

  /* Initialize the parameters structure */
  ret->outFile = NULL;
  ret->inpFiles = (char **)malloc(sizeof(char *));
  ret->inpFiles[0] = NULL;

  /* Each argument */
  initialize_argparse(&ap, *_argc, argv);
  while(!is_end_of_arguments(&ap)) {
    char *arg = get_argument(&ap);

    /* Single-character flag */
    if ((arg[0] == '-') && (arg[1] != 0) && (arg[2] == 0)) {
      delete_argument(&ap);	/* This argument is consumed here */

      switch(arg[1]) {
      case 'o':			/* Output file name */
	if (is_end_of_arguments(&ap))
	  {
	    err_message = "Expecting file name after '-o'\n";
	    goto error;
	  }
	free(ret->outFile);
	ret->outFile = strdup(consume_argument(&ap));
	break;
      case 'i':			/* Input file name */
	if (is_end_of_arguments(&ap))
	  {
	    err_message = "Expecting file name after '-i'\n";
	    goto error;
	  }
	ret->inpFiles = read_string_array(consume_argument(&ap));
	break;
      case '-':			/* End of options */
	goto end_of_options;
      default:
	err_message = "Unexpected command-line parameter\n";
	goto error;
      }
    }
    else {
      /* Other parameters are ignored */
      next_argument(&ap);
    }
  } /* end for each argument */

 end_of_options:
  *_argc = ap.argc;		/* Save the modified argc value */
  finalize_argparse(&ap);

  return ret;

 error:
  fputs(err_message, stderr);
  pb_FreeParameters(ret);
  return NULL;
}
Example #5
0
/**
 * Returns all found values for specified field. Tha allowed fields are: <ul>
 *    <li> JSR211_FIELD_ID, <li> JSR211_FIELD_TYPES, <li> JSR211_FIELD_SUFFIXES,
 *    <li> and JSR211_FIELD_ACTIONS. </ul>
 * Values should be selected only from handlers accessible for given caller_id.
 *
 * @param caller_id calling application identifier.
 * @param field search field id
 * @param result output structure where result is placed to.
 *  <br>Use @link jsr211_fillStringArray function to fill this structure.
 * @return status of the operation
 */
jsr211_result jsr211_get_all(const pcsl_string* caller_id, jsr211_field field,
                        /*OUT*/ JSR211_RESULT_STRARRAY* result) {
  long current_position;
  int found_num, access, current_size;
  pcsl_string *res_buffer = NULL;
  pcsl_string *current_result, *last;
  int chr = CHR_INDEX((int)(field));
  int isString;

  switch (record_struct[chr]) {
    case field_string:
      isString = 1;
      break;
    case field_array:
      isString = 0;
      break;
    default:
      return JSR211_FAILED;
  }

  if (open_table_file(0) != JSR211_OK) {
    return JSR211_FAILED;
  }
  res_buffer = alloc_pcsl_string_list(JSR211_MAX_RESULT_SET);
  if (res_buffer == NULL) {
    close_table_file();
    return JSR211_FAILED;
  }

  current_position = 0;
  for (current_result = res_buffer, last = res_buffer + JSR211_MAX_RESULT_SET;
       current_result < last;) {
    
    access = check_access(caller_id, current_position, &current_size);
    if (access == -1) {
      break;
    }
    
    if (access && position_field(chr) > 0) {
      if (isString) {
        read_string(current_result);
        if (!findStringInArray(res_buffer, current_result - res_buffer, 
                                                            current_result)) {
          current_result++;
        }
      }
      else {
        pcsl_string *array_res = NULL, *results, *last_res;
        int array_length = 0;

        read_string_array(&array_length, &array_res);
        if (array_length > 0) {
            for (results = array_res, last_res = array_res + array_length;
                 results < last_res && current_result < last;
                 results++) {
              if (!findStringInArray(res_buffer, current_result - res_buffer, 
                                                                    results)) {
                *(current_result++) = *results;
                *results = PCSL_STRING_NULL;
              }
            }
            free_pcsl_string_list(array_res, array_length);
        }
      }
    }
    
    storagePosition(&io_error_message, table_file, current_position);
    current_position += current_size;
    goto_next_record();
  }
  
  if ((found_num = (int)(current_result - res_buffer))) {
    jsr211_fillStringArray(res_buffer, found_num, result);
  }

  free_pcsl_string_list(res_buffer, JSR211_MAX_RESULT_SET);
  close_table_file();
  return JSR211_OK;
}