// lib - A fledgling library in paraFlow global class lib { int version = 1; // Library version number. Should change very slowly. int subversion = 1; to randomIx(int size) into int ix /* Return a random number in range 0 <= num < size */ { ix = random() * size; if (ix == size) ix = 0; } to getEnv() into dir of string env /* Get a dir containing the environment strings passed * into program. */ { static dir of string cached; if (!cached) { array of string envArray = getEnvArray(); env = (); for (s in envArray) { int eqPos = s.find("="); string key = s.first(eqPos); string val = s.rest(eqPos+1); env[key] = val; } cached = env; } env = cached; } to countChars(string s, string c) into int count /* Count the number of times c is in s. */ { while (s) { if (s.first(1) == c) count += 1; s = s.rest(1); } } to getPathArray() into array of string paths { dir of string env = lib.getEnv(); string path = env["PATH"]; int startPos, endPos = 0; int colonCount = lib.countChars(path, ':'); array[colonCount+1] of string results; // Might be nice to express this instead as // paths = [colonCount+1] // and dispense with the results variable. for (int i=0; i=0; p=s.findNext(c,p)) pos=p; } to splitPath(string path) into (string dir, string name, string extension) // Split "some/dirs/and/file.ext" into "some/dirs/and/" "file" ".ext" { int dirEnd = lib.findLastUseOfChar(path, '/'); if (dirEnd >= 0) { dir = path.first(dirEnd); path = path.rest(dirEnd); } else { dir = ""; } int extStart = lib.findLastUseOfChar(path, '.'); if (extStart >= 0) { name = path.first(extStart); extension = path.rest(extStart); } else { name = path; extension = ""; } } } global lib lib = ();