// lib - A fledgling library in paraFlow global class lib { int version = 1; // Library version number. Should change very slowly. 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, char c) into int count /* Count the number of times c is in s. */ { for (b in s) if (c == b) count += 1; } to getPathArray() into array of string paths // Get the system path into an array of directory strings. { 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; pos -= 1) if (s[pos] == c) return; pos = -1; } to subChar(dyString s, char oldChar, char newChar) // Substitute newChar for oldChar throughout string s { for (int i=0; i= 0) { dirEnd += 1; 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 = ""; } } to startsWith(string prefix, string s) into bit result // Return 1 if s starts with prefix { if (s.size >= prefix.size) { for (int i=0; i= 0) { for (int i=0; i= qCount); return; } // Actually have a real character after wild card. // Skip through s until find a match to it. c = wild[wPos]; for (;;) { if (sPos >= s.size) return; // Didn't find it if (c == s[sPos]) break; sPos += 1; } sPos += 1; wPos += 1; } else if (c == s[sPos]) { sPos += 1; wPos += 1; } else return; } } } global lib lib = ();