fmtqfn.c (1330B)
1 #include "fmtqfn.h" 2 #include "stralloc.h" 3 #include <errno.h> 4 #define AMNESIAC (errno = ENOMEM, -1) 5 #include "fmt.h" 6 #include "auto_split.h" 7 8 /* ForMaT Queue FileName 9 * The bleak Bernstein original. 10 * Requires a buffer long enough for it. 11 * Always no more than FMTQFN bytes (40 if 12 * len(dirslash) le 10) 13 * given inode numbers generally can't exceed 18446744073709551616, 14 * a 20 character string at any rate, this is futureproof. 15 */ 16 unsigned int fmtqfn(char *s, char *dirslash, 17 unsigned long id, int flagsplit) 18 { 19 unsigned int len; 20 unsigned int i; 21 22 len = 0; 23 i = fmt_str(s,dirslash); len += i; if (s) s += i; 24 if (0 != flagsplit) { 25 i = fmt_ulong(s,id % auto_split); // generate the hash 26 len += i; 27 if (s) s += i; // slide the pointer cursor up one byte 28 i = fmt_str(s,"/"); 29 len += i; 30 if (s) s += i; // CURSOR 31 } 32 i = fmt_ulong(s,id); 33 len += i; 34 if (s) s += i; 35 if (s) *s++ = 0; ++len; 36 return len; 37 } 38 39 /* ForMaT Queue FileName StrAlloc 40 * This allows passing in a SA to write the filename to. 41 * This unavoidably calls fmtqfn twice. 42 */ 43 int fmtqfnsa (stralloc *sa, char *dirslash, 44 unsigned long id, int flagsplit) 45 { 46 if (!stralloc_ready(sa, fmtqfn(NULL, dirslash, id, flagsplit))) return AMNESIAC; 47 if (!fmtqfn(sa->s, dirslash, id, flagsplit)) return AMNESIAC; 48 return sa->len; 49 }