/** * Appends on last dimension. */ static unsigned hdf5_write(ndio_t file, nd_t src) { ndio_hdf5_t self=(ndio_hdf5_t)ndioContext(file); hid_t filespace=-1,dset; dset=make_dataset(self,ndtype(src),ndndim(src),ndshape(src),&filespace); HTRY(H5Dwrite(dset, nd_to_hdf5_type(ndtype(src)), make_space(self,ndndim(src),ndshape(src)), /*mem-space selector*/ filespace, /*file-space selector ... set up by make_dataset()*/ H5P_DEFAULT,/*xfer props*/ nddata(src) )); if(filespace>0) HTRY(H5Sclose(filespace)); return 1; Error: return 0; }
main(int argc, char **argv) { char inbuf[BUFLEN], *s; Btree *bt = NULL; FILE *logfile = NULL; for (;;) { fflush(stdout); fputs("Action (? for help): ", stdout); fflush(stdout); fgets(inbuf, BUFLEN, stdin); s = inbuf + strlen(inbuf); while(iscntrl(*s)) *s-- = 0; if (logfile) fprintf(logfile, "%s\n", inbuf); if (!bt && strchr("@adfkKmMsSwW", inbuf[0])) { fputs(" **no open dataset\n", stdout); continue; } switch (inbuf[0]) { case '?': fputs( "@file - load strings in file into tree\n" "a string - add name;addr to tree\n" "d string - delete name;addr from tree\n" "dup [0|1] - disallow/allow duplicates\n" "f string - find name;addr in tree\n" "k/K [file] - display key counts (K = overwrite file)\n" "l file - log actions to file\n" "l - turn off action logging\n" "m/M [file] - display block usage map\n" "n file - make a new dataset\n" "o file - open an existing dataset\n" "s/S [file] - display tree (S = overwrite file)\n" "w/W [file] - walk tree, (W = overwrite file)\n" "q - quit\n" , stdout); fflush(stdout); break; case '@': LoadFile(bt, inbuf + 1); break; case 'a': if (inbuf[1] != ' ' || !inbuf[2] || !strchr(inbuf,';')) fputs(" Not a valid command\n", stdout); else if (DoData(bt, inbuf + 2, 1) == TREE_FAIL) fputs(" ** Insertion failed\n", stdout); break; case 'd': if (inbuf[1] == 'u' && inbuf[2] == 'p') { if (inbuf[3] == ' ' && (inbuf[4] == '0' || inbuf[4] == '1')) bt -> duplicatesOK = inbuf[4] == '0' ? 0 : 1; fputs("duplicates are ", stdout); if (bt -> duplicatesOK == 0) fputs("not ", stdout); fputs("allowed.\n", stdout); break; } if (inbuf[1] != ' ' || inbuf[2] == 0) fputs(" Not a valid command\n", stdout); else { if (DoData(bt, inbuf + 2, 0) == TREE_FAIL) fputs(" ** Delete failed\n", stdout); } break; case 'f': if (inbuf[1] != ' ' || inbuf[2] == 0) fputs(" Not a valid command\n", stdout); else { UR record; inbuf[12] = '\0'; if (bt_find(bt, &record, inbuf+2) == TREE_FAIL) fputs(" ** Find failed\n", stdout); else fprintf(stdout, "found %s;%s\n", record.name, record.addr); } break; case 'k': case 'K': { FILE *out; if (inbuf[1] == ' ' && inbuf[2] != 0) { out = fopen(inbuf+2, inbuf[0] == 'k' ? "w" : "a"); if (!out) printf("Can't open %s\n", inbuf + 2); else { show_btree(bt, out, 0); fclose(out); } } else show_btree(bt, stdout, 0); } break; case 'l': if (inbuf[1] != ' ' || inbuf[2] == 0) { if (logfile) { fclose(logfile); logfile = NULL; } else fputs(" Logfile not open\n", stdout); } else { logfile = fopen(inbuf + 2, "w"); if (logfile == NULL) printf("Can't open %s\n", inbuf + 2); } break; case 'm': case 'M': { FILE *out; if (inbuf[1] == ' ' && inbuf[2] != 0) { out = fopen(inbuf+2, inbuf[0] == 'm' ? "w" : "a"); if (!out) printf("Can't open %s\n", inbuf + 2); else { show_btree_map(bt, out); fclose(out); } } else show_btree_map(bt, stdout); } break; case 'n': if (inbuf[1] != ' ' || inbuf[2] == 0) fputs(" Not a valid command\n", stdout); else make_dataset(inbuf+2); break; case 'o': if (inbuf[1] != ' ' || inbuf[2] == 0) fputs(" Not a valid command\n", stdout); else { if (bt) { if (bt_close(bt)) printf("\nClose failed: %s\n", ErrorText[bt->error_code]); else printf("\nData files closed.\n"); bt = NULL; } bt = open_dataset(inbuf+2); } break; case 'q': if (logfile) fclose(logfile); if (bt) { if (bt_close(bt)) printf("\nClose failed: %s\n", ErrorText[bt->error_code]); else printf("\nData files closed.\n"); } return; case 's': case 'S': { FILE *out; if (inbuf[1] == ' ' && inbuf[2] != 0) { out = fopen(inbuf+2, inbuf[0] == 's' ? "w" : "a"); if (!out) printf("Can't open %s\n", inbuf + 2); else { show_btree(bt, out, 1); fclose(out); } } else show_btree(bt, stdout, 1); } break; case 'w': case 'W': { if (inbuf[1] == ' ' && inbuf[2] != 0) { outfile = fopen(inbuf+2, inbuf[0] == 'w' ? "w" : "a"); if (!outfile) printf("Can't open %s\n", inbuf + 2); else { DataCount = 0; bt_walk(bt, DisplayFunc); fprintf(outfile, "%d items\n", DataCount); fclose(outfile); outfile = NULL; } } else { DataCount = 0; outfile = stdout; bt_walk(bt, DisplayFunc); fprintf(outfile, "%d items\n", DataCount); outfile = NULL; } } break; case ';': break; /* comment */ default: fputs(" Not a valid command\n", stdout); break; } if (bt && bt->error_code) { printf("ERROR: %s\n", ErrorText[bt->error_code]); bt->error_code = 0; } } }
int main (int argc, char **argv) { /* Return code of the program. */ int ret = EXIT_SUCCESS; /* The mode of the generator, either START or ITERATING. */ int mode; /* The argument index which points to the current lattice parameter to be * converted. */ int param_arg; /* Total number of lattice parameters */ int nparams; /* Array of the converted lattice parameters. */ double *params; /* Various file streams for reading and writing. */ FILE *output_inp_file, *output_pot_file, *input_pot_new_file = NULL; /* We've got to have at least one argument to do anything! */ if (argc == 1) usage (); /* Check which mode we're operating in. We assume that we're in start mode, * unless we specifically find the option for iterating. */ mode = (strcmp (argv[1], "-i")) ? START : ITERATING; if (mode == ITERATING) { /* We'll need a .pot_new file and at least one lattice parameter. */ if (argc < 4) usage (); input_pot_new_file = fopen_checked (argv[2], "r", "previous potential file"); param_arg = 3; } else { /* We may or may not have the "-s" explicitly, so we need to check. */ if (!strcmp (argv[1], "-s")) { if (argc < 3) usage (); param_arg = 2; } else { param_arg = 1; } } /* Calculate the number of lattice parameters we've been given, and check * that it's the correct number. */ nparams = argc - param_arg; if (!correct_number_of_parameters (nparams)) usage (); /* Allocate space for these lattice parameters. */ params = malloc (nparams * sizeof (*params)); if (!params) { fprintf (stderr, "Couldn't allocate memory to store lattice parameters.\n"); exit (EXIT_FAILURE); } /* Do the conversion for each parameter into a double. */ for (int i = 0; param_arg < argc; param_arg++, i++) params[i] = strtod_checked (argv[param_arg]); /* Get the name of the dataset. */ make_dataset (params); /* Generate the filenames for the two output files. */ snprintf (output_inp_filename, sizeof(output_inp_filename), "%s.inp", dataset); snprintf (output_pot_filename, sizeof(output_pot_filename), "%s.pot", dataset); /* Open the output files for writing. */ output_inp_file = fopen_checked (output_inp_filename, "w", "input file"); output_pot_file = fopen_checked (output_pot_filename, "w", "potential file"); /* Write the output files and check for success. */ if (write_inp_file (output_inp_file, mode) || write_pot_file (output_pot_file, params, mode)) { ret = EXIT_FAILURE; } else { /* Print out the name of the dataset so bash scripts can use it. */ puts (dataset); } /* Handle the cleanup operations. */ fclose (output_inp_file); fclose (output_pot_file); if (mode == ITERATING) fclose (input_pot_new_file); free (params); return ret; }