void * _list_process(list_t *list, reduce_t reducer, void *data) { listprocessor_t *lp = lp_create(list, reducer, data); void *ret = lp_run(lp); lp_free(lp); return ret; }
void cleanup(void) { if (ldmProxy != NULL) { lp_free(ldmProxy); ldmProxy = NULL; } log_fini(); }
/** Read textfile, textfile should describe an lp in (FIXME format) * the specified format. Prints the integer 0-1 solutions to stdout * @param filename name of file to read * @return EXIT_FAILURE if file in wrong format * EXIT_SUCCESS if solved */ int solve_lp(const char *filename) { LinearProgram *lp = new_lp_from_file(filename); if (NULL == lp) { /* NULL if not enough memory or wrong format */ return EXIT_FAILURE; } print_matrix(lp); uint64_t sols = get_bin_solutions_lp(lp); printf("found %lu feasible solutions\n", sols); lp_free(lp); return EXIT_SUCCESS; }
// taken from ex4_readline.c // adapted to special needs LinearProgram *new_lp_from_file(const char* filename) { assert(NULL != filename); assert(0 < strlen(filename)); int rows = 0; int cols; char* end_ptr = NULL; LinearProgram* lp = NULL; /* counts the constraint that were read from file * if constraints < rows -> error * */ int constraints = 0; /* used to mark distinguish the current state of the parser */ int parser_state = READ_COLS; FILE* fp; char buf[MAX_LINE_LEN]; char* s; int lines = 0; if (NULL == (fp = fopen(filename, "r"))) { fprintf(stderr, "Can't open file %s\n", filename); return NULL; } while(NULL != (s = fgets(buf, sizeof(buf), fp))) { char* t = strpbrk(s, "#\n\r"); lines++; if (NULL != t) /* else line is not terminated or too long */ *t = '\0'; /* clip comment or newline */ /* Skip over leading space */ s = skip_spaces(s); /* Skip over empty lines */ if (!*s) { /* <=> (*s == '\0') */ continue; } /* line is nonempty, so try to parse data */ if (parser_state == READ_COLS) { cols = (int) strtol(s, &end_ptr, 10); if (cols <= 0) { fprintf(stderr, "please specify a positive number of cols.\n"); goto read_error; } parser_state = READ_ROWS; } else if (parser_state == READ_ROWS) { rows = (int) strtol(s, &end_ptr, 10); if (rows <= 0) { fprintf(stderr, "please specify a positive number of rows.\n"); goto read_error; } lp = lp_new(rows, cols); parser_state = READ_CONSTRAINTS; } else { /* stop if a row does not match the format */ if (constraints >= rows) { lp_free(lp); fprintf(stderr, "too many constraints"); goto read_error; } bool valid_format = parse_row(s, constraints, lp); if (!valid_format) { lp_free(lp); goto read_error; } constraints++; } } fclose(fp); if (constraints != rows) { fprintf(stderr, "speciefied #(rows) does not match: %d expected, %d found\n", rows, constraints); return NULL; } if (can_overflow(lp)) { return NULL; } printf("%d lines\n", lines); return lp; read_error: printf("error in line %d\n", lines); fclose(fp); return NULL; }
/* * Returns: * 0 Success * SYSTEM_ERROR O/S failure. "log_add()" called. * LP_TIMEDOUT The RPC call timed-out. "log_add()" called. * LP_RPC_ERROR RPC error. "log_add()" called. * LP_LDM_ERROR LDM error. "log_add()" called. */ int main( int ac, char* av[]) { char myname[_POSIX_HOST_NAME_MAX]; char* progname = av[0]; prod_class_t clss; prod_spec spec; int seq_start = 0; int status; ErrorObj* error; unsigned remotePort = LDM_PORT; /* * Set up error logging */ (void)log_init(progname); remote = "localhost"; (void)set_timestamp(&clss.from); clss.to = TS_ENDT; clss.psa.psa_len = 1; clss.psa.psa_val = &spec; spec.feedtype = DEFAULT_FEEDTYPE; spec.pattern = ".*"; { extern int optind; extern char *optarg; int ch; while ((ch = getopt(ac, av, "vxl:h:f:P:s:")) != EOF) switch (ch) { case 'v': if (!log_is_enabled_info) (void)log_set_level(LOG_LEVEL_INFO); break; case 'x': (void)log_set_level(LOG_LEVEL_DEBUG); break; case 'l': (void)log_set_destination(optarg); break; case 'h': remote = optarg; break; case 'f': spec.feedtype = atofeedtypet(optarg); if(spec.feedtype == NONE) { fprintf(stderr, "Unknown feedtype \"%s\"\n", optarg); usage(progname); } break; case 'P': { char* suffix = ""; long port; errno = 0; port = strtol(optarg, &suffix, 0); if (0 != errno || 0 != *suffix || 0 >= port || 0xffff < port) { (void)fprintf(stderr, "%s: invalid port %s\n", av[0], optarg); usage(av[0]); } remotePort = (unsigned)port; break; } case 's': seq_start = atoi(optarg); break; case '?': usage(progname); break; } ac -= optind; av += optind; if(ac < 1) usage(progname); } /* * Register the exit handler */ if(atexit(cleanup) != 0) { log_syserr_q("atexit"); exit(SYSTEM_ERROR); } /* * Set up signal handlers */ set_sigactions(); (void) strncpy(myname, ghostname(), sizeof(myname)); myname[sizeof(myname)-1] = 0; (void)exitIfDone(INTERRUPTED); /* * Connect to the LDM. */ status = lp_new(remote, &ldmProxy); if (0 != status) { log_flush_error(); status = (LP_SYSTEM == status) ? SYSTEM_ERROR : CONNECTION_ABORTED; } else { log_debug("version %u", lp_version(ldmProxy)); status = ldmsend(ldmProxy, &clss, myname, seq_start, ac, av); if (0 != status) log_flush_error(); lp_free(ldmProxy); ldmProxy = NULL; } /* "ldmProxy" allocated */ return status; }