args.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #ifndef __MYARGP__
  2. #define __MYARGP__
  3. #include <argp.h>
  4. #include <igraph.h>
  5. #include "io.h"
  6. #include "operations.h"
  7. #include "graph.h"
  8. /* This structure is used by main to communicate with parse_opt. */
  9. struct arguments {
  10. char **args;
  11. long int ngraphs;
  12. void (*oper)(graph_t *result, igraph_vector_ptr_t *glist);
  13. void (*ofmt)(graph_t *g); /* Output format flag */
  14. void (*ifmt)(igraph_t *g, FILE *ist); /* Input format flag */
  15. char *outfile; /* Argument for -o */
  16. };
  17. /*
  18. OPTIONS. Field 1 in ARGP.
  19. Order of fields: {NAME, KEY, ARG, FLAGS, DOC}.
  20. */
  21. static struct argp_option options[] = {
  22. {"output", 'o', "OUTFILE", 0, "Output to OUTFILE instead of to standard output"},
  23. {"operation", 'w', "OPERATION", 0, "Select OPERATION from union, intersection, xor. (Required.)"},
  24. {"output_format", 'f', "FORMAT", 0,
  25. "Select output format from dot (default), graphml, gml, pajek, edgelist, ncol, lgl, dimacs."},
  26. {"input_format", 'i', "FORMAT", 0,
  27. "Select input format from graphml, gml, pajek, edgelist, ncol, lgl, dimacs, graphdb, dl. (Required.)"},
  28. {0}
  29. };
  30. /*
  31. PARSER. Field 2 in ARGP.
  32. Order of parameters: KEY, ARG, STATE.
  33. */
  34. static error_t parse_opt (int key, char *arg, struct argp_state *state) {
  35. struct arguments *arguments = state->input;
  36. switch (key) {
  37. case 'o':
  38. arguments->outfile = arg;
  39. ost = fopen(arg, "w");
  40. break;
  41. case 'f':
  42. if(strcmp(arg, "graphml") == 0) {
  43. arguments->ofmt = print_graphml;
  44. } else if(strcmp(arg, "gml") == 0) {
  45. arguments->ofmt = print_gml;
  46. } else if(strcmp(arg, "pajek") == 0) {
  47. arguments->ofmt = print_pajek;
  48. } else if(strcmp(arg, "edgelist") == 0) {
  49. arguments->ofmt = print_edgelist;
  50. } else if(strcmp(arg, "ncol") == 0) {
  51. arguments->ofmt = print_ncol;
  52. } else if(strcmp(arg, "lgl") == 0) {
  53. arguments->ofmt = print_lgl;
  54. } else if(strcmp(arg, "dot") == 0) {
  55. arguments->ofmt = print_dot;
  56. } else {
  57. arguments->ofmt = print_error;
  58. }
  59. break;
  60. case 'i':
  61. if(strcmp(arg, "graphml") == 0) {
  62. arguments->ifmt = read_graphml;
  63. } else if(strcmp(arg, "gml") == 0) {
  64. arguments->ifmt = read_gml;
  65. } else if(strcmp(arg, "pajek") == 0) {
  66. arguments->ifmt = read_pajek;
  67. } else if(strcmp(arg, "edgelist") == 0) {
  68. arguments->ifmt = read_edgelist;
  69. } else if(strcmp(arg, "ncol") == 0) {
  70. arguments->ifmt = read_ncol;
  71. } else if(strcmp(arg, "lgl") == 0) {
  72. arguments->ifmt = read_lgl;
  73. } else if(strcmp(arg, "cl") == 0) {
  74. arguments->ifmt = read_dl;
  75. } else if(strcmp(arg, "graphdb") == 0) {
  76. arguments->ifmt = read_graphdb;
  77. } else {
  78. arguments->ifmt = read_error;
  79. }
  80. break;
  81. case 'w':
  82. if(strcmp(arg, "union") == 0) {
  83. arguments->oper = graph_union;
  84. } else if(strcmp(arg, "intersection") == 0) {
  85. arguments->oper = graph_intersection;
  86. } else if(strcmp(arg, "xor") == 0) {
  87. arguments->oper = graph_xor;
  88. } else {
  89. arguments->oper = graph_operation_error;
  90. }
  91. break;
  92. case ARGP_KEY_ARG:
  93. arguments->args = &state->argv[--state->next];
  94. state->next = state->argc;
  95. ++arguments->ngraphs;
  96. //arguments->args[state->arg_num] = arg;
  97. break;
  98. case ARGP_KEY_END:
  99. if(state->arg_num < 2) { // Too few arguments.
  100. argp_usage(state);
  101. }
  102. break;
  103. default:
  104. return ARGP_ERR_UNKNOWN;
  105. }
  106. return 0;
  107. }
  108. /*
  109. ARGS_DOC. Field 3 in ARGP.
  110. A description of the non-option command-line arguments
  111. that we accept.
  112. */
  113. static char args_doc[] = "FILE1 FILE2 ... FILEN";
  114. /*
  115. DOC. Field 4 in ARGP.
  116. Program documentation.
  117. */
  118. static char doc[] = "Ganesha -- perform operations on graphs.";
  119. /*
  120. The ARGP structure itself.
  121. */
  122. static struct argp argp = {options, parse_opt, args_doc, doc};
  123. #endif