// -*- Mode: Eagle -*- /* * * String utilities. * * Copyright 2007 - 2009 by John Johnson Software, LLC. * All Rights Reserved. * */ int STR_NOT_FOUND = -1; char REC_SEP = '\v'; char FIELD_SEP = '\t'; string leftstr(string s, int n) { return strsub(s, 0, n); } string rightstr(string s, int n) { return strsub(s, strlen(s) - n); } string ltrim(string s) { while (leftstr(s, 1) == " ") { s = strsub(s, 1); } return s; } string rtrim(string s) { while (strsub(s, strlen(s) - 1, 1) == " ") { s = strsub(s, 0, strlen(s) - 1); } return s; } string trim(string s) { return ltrim(rtrim(s)); } string remove_last_slash(string s) { if (rightstr(s, 1) == "/") { s = leftstr(s, strlen(s) - 1); } return s; } string remove_last_dir(string s) { int pos; pos = strrstr(s, "/"); return leftstr(s, pos); } string key_value_record(string a, string b) { return a + FIELD_SEP + b + REC_SEP; } /* * Replace text in a string with other text. * */ string substitute(string s, string replacements) { int pos; string keys[]; string values[]; string records[]; string fields[]; int num_keys; int num_fields; int i; string temp; int last_match; num_keys = strsplit(records, replacements, REC_SEP); num_keys--; for (i=0; i width) { break; } pos = pos - strlen(path) - 1; } if (keep_pos <= 0) { return path; } return "..." + strsub(path, keep_pos); } string fi(string f, int i) { string str; if (strcnt(f, '%') < 1) { return ""; } else { sprintf(str, f, i); } return str; } string fir(string f, int i, real r) { string str; if (strcnt(f, '%') < 1) { return fi(f, i); } else { sprintf(str, f, i, r); } return str; } string fr(string f, real r) { string str; if (strcnt(f, '%') < 1) { return ""; } else { sprintf(str, f, r); } return str; } string frr(string f, real r1, real r2) { string str; if (strcnt(f, '%') < 2) { return fr(f, r1); } else { sprintf(str, f, r1, r2); } return str; } string frrr(string f, real r1, real r2, real r3) { string str; if (strcnt(f, '%') < 3) { return frr(f, r1, r2); } else { sprintf(str, f, r1, r2, r3); } return str; } string frrrr(string f, real r1, real r2, real r3, real r4) { string str; if (strcnt(f, '%') < 4) { return frrr(f, r1, r2, r3); } else { sprintf(str, f, r1, r2, r3, r4); } return str; } string fs(string f, string s) { string str; if (strcnt(f, '%') < 1) { return ""; } else { sprintf(str, f, s); } return str; }