Esempio n. 1
0
int find_partition_2d(cloud_2d* data, specs_2d* specs, handler_2d* callback) {

    int return_code = code_success;

    if ( return_code = check_specs(specs) )
        goto exit;

    if ( return_code = check_handler(callback) )
        goto exit;

    double x_min, x_max, y_min, y_max;
    double* min_max[4] = {&x_min, &x_max, &y_min, &y_max};

    if ( return_code = check_data(data, min_max) )
        goto exit;

    treegrid_2d* grid;

    if ( return_code = treegrid_2d_new(&grid, NULL) ) // TODO: define necessary args
        goto exit;

    int num_cells_total = insert_all_points(grid, data);
    if ( num_cells_total < 0) {
        return_code = num_cells_total;
        goto clean_grid;
    }

    double *x_reduc = NULL, *y_reduc = NULL;

    if ( return_code = alloc_output_buffer(&x_reduc, &y_reduc, num_cells_total) )
        goto clean_array;

    add_to_output_buffer(data, grid, x_reduc, y_reduc);

    void* context = callback->use_data_context;
    return_code = callback->use_data(context, x_reduc, y_reduc, num_cells_total);

clean_array:
    if (x_reduc) free(x_reduc);
    if (y_reduc) free(y_reduc);

clean_grid:
    return_code = treegrid_2d_clean(grid);

exit:
    return return_code;

}
Esempio n. 2
0
File: C.c Progetto: mattn/C-win32
int main(int argc, char** argv)
{
  int ret;
  
  setup_dir();
  
  /* init globals */
  gcc = sa_concat(gcc, "gcc"); /* replaced laterwards if c++ */
  gcc = sa_concat(gcc, "-I.");
  
  src_lines =
    sa_concat(src_lines,
	      "#define __LARGE_C__ " VERSION_INT_STR "\n"
	      "#ifdef __cplusplus\n"
	      "extern \"C\" {\n"
	      "#endif\n"
	      "#include <stdio.h>\n"
	      "#include <stdlib.h>\n"
	      "#ifdef __cplusplus\n"
	      "}\n"
	      "#include <iostream>\n"
	      "using namespace std;\n"
	      "#endif\n"
	      "\n"
	      "__LARGE_C_PREFIX__\n");
  
  argv++;
  { /* parse args, determine cache dir */
    char** new_argv = parse_args(argv, NULL, 0);
    for (; argv != new_argv; argv++) {
      add_spec(*argv, strlen(*argv) + 1);
    }
    if (! keep_files && (oneliner || *argv != NULL)) {
      struct stat st;
      if (oneliner) {
	build_store_dir();
      } else if (stat(*argv, &st) == 0) {
	add_spec(*argv, strlen(*argv) + 1);
	add_spec(&st.st_size, sizeof(st.st_size));
	add_spec(&st.st_mtime, sizeof(st.st_mtime));
	build_store_dir();
      }
    }
  }
  
  /* use cache if possible */
  if (store_dir != NULL && check_specs()) {
    char** child_argv = NULL;
#ifdef _WIN32
    _utime(store_dir, NULL); /* update mtime of the directory */
#else
    utimes(store_dir, NULL); /* update mtime of the directory */
#endif
    exec_file = str_concat(str_dup(store_dir), "/"A_OUT);
    child_argv = sa_concat(child_argv, exec_file);
#ifdef _WIN32
    {
      int status;
	  ret = spawn_w32(child_argv, &status);
      if (status == 0) exit(ret);
    }
#else
    execv(exec_file, child_argv);
#endif
    // if execv failed, we compile
    free(exec_file);
    remove_dir(store_dir);
  }
  
  /* prepare files */
  make_temp_dir();
  exec_file = str_concat(str_dup(temp_dir), "/"A_OUT);
  c_file = str_concat(str_dup(temp_dir), "/source.c");
  if ((src_fp = fopen(c_file, "wt")) == NULL) {
    cmd_error("failed to create temporary file: %s : %s\n", c_file,
	      strerror(errno));
  }
  while (src_lines != NULL && *src_lines != NULL) {
    fputs(*src_lines++, src_fp);
  }
  
  /* write source with adjustments */
  if (! oneliner) {
    FILE* fp;
    char* file;
    char* line;
    int line_no = 0;
    if (argv[0] == NULL) {
      fp = stdin;
      file = "stdin";
    } else if (strcmp(argv[0], "-") == 0) {
      fp = stdin;
      argv++;
      file = "stdin";
    } else {
      file = *argv++;
      if ((fp = fopen(file, "rt")) == NULL) {
	cmd_error("cannot open file: %s : %s\n", file, strerror(errno));
      }
      fprintf(src_fp, "# 1 \"%s\" 1\n", file);
    }
    while ((line = get_line(fp)) != NULL) {
      int comment_out = 0;
      line_no++;
      if (line_no == 1 && strncmp(line, "#!", 2) == 0) {
	comment_out = 1;
      } else if (line[0] == '#') {
	char* buf = str_dup(line + 1);
	char** tokens = split_tokens(buf);
	if (*tokens != NULL) {
	  if (strcmp(tokens[0], "option") == 0) {
	    parse_args(tokens + 1, file, line_no);
	    comment_out = 1;
	  }
	}
	free(buf);
	free(tokens);
      }
      if (comment_out == 1) {
	fprintf(src_fp, "// ");
      }
      fputs(line, src_fp);
    }
    fputs("\n", src_fp);
    if (fp != stdin) {
      fclose(fp);
    }
  }
  
  /* close source file */
  fputs("__LARGE_C_SUFFIX__\n", src_fp);
  fclose(src_fp);
  src_fp = NULL;
  
  /* compile */
  if (use_plusplus) {
    gcc[0] = "g++";
  }
  if (use_main) {
    gcc = sa_concat(gcc, "-D__LARGE_C_PREFIX__=");
    gcc = sa_concat(gcc, "-D__LARGE_C_SUFFIX__=");
  } else {
    gcc =
      sa_concat(gcc, "-D__LARGE_C_PREFIX__=int main(int argc, char** argv) {");
    gcc = sa_concat(gcc, "-D__LARGE_C_SUFFIX__=; return 0; }");
  }
  gcc = sa_concat(gcc, "-o");
  gcc = sa_concat(gcc, show_disassembly ? "-" : exec_file);
  gcc = sa_concat(gcc, c_file);
  gcc = sa_merge(gcc, lopts);
  if ((ret = call_proc(gcc, "could not execute compiler")) != 0) {
    cleanup();
    exit(ret);
  }
  
  if (show_disassembly) {
    cleanup();
    exit(0);
  }
  
  { /* execute */
    char** child_argv = NULL;
    if (use_debugger) {
      child_argv = sa_concat(child_argv, "gdb");
    }
    child_argv = sa_concat(child_argv, exec_file);
    child_argv = sa_merge(child_argv, argv);
    ret = call_proc(child_argv, "could not spawn child process");
  }
  
  /* move temp_dir to store_dir, if possible.
   * or, remove work_dir
   */
  if (store_dir == NULL) {
    cleanup();
  } else {
    save_specs();
    update_cache();
    if (rename(temp_dir, store_dir) != 0) {
      cleanup();
    }
  }
  
  return ret;
}