/** Logs the contents of the dictionary to the logger, with the string "prefix" *** prepended to each line. This function assumes all values in the dictionary *** are strings. **/ void Dict_dump(Dict *d, char *prefix) { size_t ii; List *l = NULL; Pair *p = NULL; char *v = NULL; Buffer *b = NULL; char n[] = "(null)"; if (d != NULL) { l = (List *)d; b = new_Buffer(0); for (ii = 0; ii < List_length(l); ++ii) { p = (Pair *)List_get(l, ii); v = (char *)p->value; if (prefix != NULL) Buffer_appendString(b, prefix); Buffer_appendString(b, p->key); Buffer_appendString(b, " = "); if (v == NULL) { Buffer_appendString(b, n); } else { Buffer_appendChar(b, '\"'); Buffer_appendString(b, v); Buffer_appendChar(b, '\"'); } Logging_debugf("%s", b->data); Buffer_reset(b); } delete_Buffer(b); } else { Logging_warnf("%s: NULL argument", __FUNCTION__); } }
/** Removes "." and ".." from a path */ char *getCanonicalPath(const char *path){ char *prefix = NULL; char *rest = NULL; char *tmp = NULL; size_t offset = 0; char **src = NULL; List *dst = NULL; size_t ii = 0; Buffer *buf = NULL; char *result = NULL; if (path != NULL && strlen(path) > 0) { tmp = strxreplace(astrcpy(path), '\\', '/'); if (isDosPath(tmp) || isUncPath(tmp)) { if (isDosPath(tmp)) { prefix = getPathPart(tmp, PATH_DRIVE); offset = 0; } else if (isUncPath(tmp)) { prefix = getPathPart(tmp, PATH_HOST); offset = 2; } rest = astrcpy(strchr(&tmp[offset], '/')); } else { rest = astrcpy(tmp); } src = astrtok(rest, "/"); dst = new_List(); while (src[ii] != NULL) { if (strxequals(src[ii], "..")) { List_remove(dst, -1, false); } else if (!strxequals(src[ii], ".")) { List_append(dst, src[ii]); } ii++; } buf = new_Buffer(0); if (prefix != NULL) { Buffer_appendString(buf, prefix); } for (ii = 0; ii < List_length(dst); ++ii) { Buffer_appendString(buf, List_get(dst, ii)); if (ii != (List_length(dst) - 1)) { Buffer_appendChar(buf, '/'); } } result = astrcpy(buf->data); delete_Buffer(buf); delete_List(dst, false); astrtokfree(src); mu_free(prefix); mu_free(rest); mu_free(tmp); } return result; }
/** Joins two paths together, allowing for relative/absolute paths */ char *getCombinedPath(const char *path1, const char *path2) { char *tmp1 = NULL; char *tmp2 = NULL; char *tmp = NULL; Buffer *buf = NULL; char *result = NULL; char *final = NULL; if (!strxnullorempty(path1) && !strxnullorempty(path2)) { tmp1 = strxreplace(astrcpy(path1), '\\', '/'); tmp2 = strxreplace(astrcpy(path2), '\\', '/'); if (isDosPath(tmp2) || isUncPath(tmp2)) { /* Path2 is a fully-qualified DOS/Windows path; return it */ result = astrcpy(tmp2); } else { buf = new_Buffer(0); if (tmp2[0] == '/') { /* Path2 is an absolute path. If Path1 is a DOS/Windows or ** UNC path, prepend the drive letter or hostname; otherwise ** return it. */ if (isDosPath(tmp1)) { tmp = getPathPart(tmp1, PATH_DRIVE); Buffer_appendString(buf, tmp); Buffer_appendString(buf, tmp2); mu_free(tmp); } else if (isUncPath(tmp1)) { tmp = getPathPart(tmp1, PATH_HOST); Buffer_appendString(buf, "//"); Buffer_appendString(buf, tmp); Buffer_appendString(buf, tmp2); mu_free(tmp); } else { Buffer_appendString(buf, tmp2); } } else { /* Simply concatenate the two paths */ Buffer_appendString(buf, tmp1); if (tmp1[strlen(tmp1) - 1] != '/') { Buffer_appendChar(buf, '/'); } Buffer_appendString(buf, tmp2); } result = astrcpy(buf->data); delete_Buffer(buf); } mu_free(tmp1); mu_free(tmp2); } /* Remove any "." and ".." in the path */ final = getCanonicalPath(result);
int main(int argc, char *argv[]){ /* Define a default expression so I can test it without GDB screwing about * with escape characters... */ char defaultExpression[] = "substr(`This is a test`, 1, 4)"; char *expression = NULL; Buffer *b = new_Buffer(256); char *result = NULL; int ii; Expr *e = NULL; /* Variable "scopes" */ Vars *globals = NULL; Vars *locals = NULL; Logging_setup(argv[0], LOG_TOSTDERR | LOG_TOFILE | LOG_LEVELTRACE, "EvalTest.log"); /* Add a few variables */ globals = new_Vars(NULL); locals = new_Vars(globals); Vars_let(globals, "pi", "3.1415", VAR_CONST | VAR_NOSHADOW); Vars_let(locals, "x", "1234", VAR_STD); Vars_let(locals, "t", "This is a test", VAR_STD); /* Build expression off command line if supplied */ if(argc > 1){ for(ii = 1; ii < argc; ++ii){ Buffer_appendString(b, argv[ii]); Buffer_appendString(b, " "); } expression = b->data; } else{ expression = defaultExpression; } Logging_debugf("Input string: \"%s\"", expression); e = new_Expr(expression, variables); result = Expr_evaluate(e); if(result != NULL){ Logging_infof("Expression result: \"%s\"", result); } else{ Logging_warn("Expression result was NULL"); } delete_Vars(globals); delete_Vars(locals); delete_Expr(e); delete_Buffer(b); exit(EXIT_SUCCESS); }
/** Allocates and initialises a new TextFile structure */ TextFile *new_TextFile(const char *fileName){ TextFile *result = NULL; FILE *f = NULL; Buffer *b = NULL; result = (TextFile *)mu_malloc(sizeof(TextFile)); if ((f = fopen(fileName, "rb")) != NULL) { result->f = f; b = new_Buffer(132); result->b = b; result->state = 0; } if (f == NULL) { mu_free(result); Logging_fatalf("%s: Cannot open file \"%s\": %s", __FUNCTION__, fileName, strerror(errno)); } return result; }
Buffer_t * new_Buffer (int n_events, char *file, int enable_cache) { Buffer_t *buffer = NULL; #if defined(HAVE_ONLINE) #if 0 pthread_mutexattr_t attr; #endif int rc; #endif xmalloc(buffer, sizeof(Buffer_t)); buffer->FillCount = 0; buffer->MaxEvents = n_events; xmalloc(buffer->FirstEvt, n_events * sizeof(event_t)); buffer->LastEvt = buffer->FirstEvt + n_events; buffer->HeadEvt = buffer->FirstEvt; buffer->CurEvt = buffer->FirstEvt; if (file == NULL) { buffer->fd = -1; } else { /* * We found a system where the mpirun seems to close the stdin, * then this open assigns the fd 0, and later writes trigger an * error of invalid fd. If the fd assigned is 0, repeat the open. */ do { buffer->fd = open (file, O_CREAT | O_TRUNC | O_RDWR, 0644); } while (buffer->fd == 0); if (buffer->fd == -1) { fprintf(stderr, "new_Buffer: Error opening file '%s'.\n", file); perror("open"); exit(1); } } #if defined(HAVE_ONLINE) #if 0 pthread_mutexattr_init( &attr ); pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_RECURSIVE_NP ); rc = pthread_mutex_init( &(buffer->Lock), &attr ); if ( rc != 0 ) { perror("pthread_mutex_init"); fprintf(stderr, "new_Buffer: Failed to initialize mutex.\n"); pthread_mutexattr_destroy( &attr ); exit(1); } pthread_mutexattr_destroy( &attr ); #else rc = pthread_mutex_init( &(buffer->Lock), NULL ); if ( rc != 0 ) { perror("pthread_mutex_init"); fprintf(stderr, "new_Buffer: Failed to initialize mutex.\n"); exit(1); } #endif #endif xmalloc(buffer->Masks, n_events * sizeof(Mask_t)); Mask_Wipe(buffer); buffer->FlushCallback = CALLBACK_FLUSH; buffer->NumberOfCachedEvents = 0; buffer->CachedEvents = NULL; buffer->VictimCache = NULL; if (enable_cache) { buffer->VictimCache = new_Buffer(BUFFER_CACHE_SIZE, file, 0); } return buffer; }
/** * <p>Factorisation using Pollard's rho method</p> * <p> * Advanced algorithms, KTH – Project 1 * DD2440 “avag11” * </p> * <p> * Enter large integer that you want factorised into primes, exit with empty line * Each prime will be printed on a separate line each, directly after your input, * when the factorisation is complete. * </p> * * @author Mattias Andrée, [email protected], 900223-3253 * @author Joel Mickelin, [email protected], 880729-0070 * * @param argc Number of elements in argv * @param argv The command line used to start the program: the program followed by options */ int main(int argc, String argv[]) { unused argc; unused argv; //Creating seed list, evens are prime, odds are composites seeds = malloc((SEED_LIMIT + 1) * sizeof(llong)); *(seeds + 1) = 10000; *(seeds + 2) = 15319; *(seeds + 3) = 10001; *(seeds + 4) = 15401; *(seeds + 5) = 10002; *(seeds + 6) = 10007; *(seeds + 7) = 10003; *(seeds + 8) = 22541; *(seeds + 9) = 10004; *seeds = 0; //Integers Bignum q; mpz_init(q); //Quotient Bignum r; mpz_init(r); //Remainder int rootOrder; //The order of the integer = max {i : x↑i = integer} int fx; //The number of factors of small primes Bignum integer; mpz_init(integer); //Integer to factorise //Prime factor buffer long* bufptr = malloc(sizeof(long)); Buffer buffer = new_Buffer(1); //Constants, it is very important for speed to test this primes Bignum _3; mpz_init_set_ui(_3, 3); Bignum _5; mpz_init_set_ui(_5, 5); Bignum _7; mpz_init_set_ui(_7, 7); #define appendFactors(X) appendToBuffer(buffer, (*bufptr)++, X) #define printFactors printBuffer(buffer, *bufptr) while (readBignum(integer)) { *bufptr = 0; free(*buffer); *buffer = malloc(0); //Remove all factors of 2 if ((fx = lowestBit(integer)) > 0) { shiftRight(integer, integer, fx); if (fx & 1) appendFactors(STR_1(2)); if (fx & 2) appendFactors(STR_2(2)); if (fx & 4) appendFactors(STR_4(2)); if (fx & 8) appendFactors(STR_8(2)); if (fx & 16) appendFactors(STR_16(2)); if (fx & 32) appendFactors(STR_32(2)); if (fx & 64) appendFactors(STR_64(2)); //limit, 127 factors of 2, we will not reach that if (equals(integer, 1)) { printFactors; printf("\n"); continue; } } //Remove all factors of X #define factorIt(X) \ if (fx = contDiv(integer, integer, _##X, null, null, 0)) \ { \ if (fx & 1) appendFactors(STR_1(X)); \ if (fx & 2) appendFactors(STR_2(X)); \ if (fx & 4) appendFactors(STR_4(X)); \ if (fx & 8) appendFactors(STR_8(X)); \ if (fx & 16) appendFactors(STR_16(X)); \ if (fx & 32) appendFactors(STR_32(X)); \ if (fx & 64) appendFactors(STR_64(X)); \ \ if (equals(integer, 1)) \ { \ printFactors; \ printf("\n"); \ continue; \ } \ } factorIt(3) factorIt(5) //factorIt(7) if (isPrime(integer)) { printFactors; printf("%s\n\n", bignumToString(integer)); } else if (factorisePollardsRho(integer, 0, buffer, bufptr, 1)) { printFactors; printf("\n"); } else printf(FAIL); } free(bufptr); free(buffer); successful; }