182#include <config_auto.h>
188#define getcwd _getcwd
200#include <sys/types.h>
210#include "allheaders.h"
212#if defined(__APPLE__) || defined(_WIN32)
233 L_WARNING(
"src not defined\n", __func__);
238 if ((dest = (
char *)LEPT_CALLOC(len + 1,
sizeof(
char))) == NULL)
239 return (
char *)ERROR_PTR(
"dest not made", __func__, NULL);
272 return ERROR_INT(
"dest not defined", __func__, 1);
277 for (i = 0; i < n && src[i] !=
'\0'; i++)
313 return (
char *)ERROR_PTR(
"src not defined", __func__, NULL);
315 if (start < 0 || start > len - 1)
316 return (
char *)ERROR_PTR(
"invalid start", __func__, NULL);
318 nbytes = len - start;
319 if (start + nbytes > len)
320 nbytes = len - start;
321 if ((dest = (
char *)LEPT_CALLOC(nbytes + 1,
sizeof(
char))) == NULL)
322 return (
char *)ERROR_PTR(
"dest not made", __func__, NULL);
347 return ERROR_INT(
"pdest not defined", __func__, 1);
386 return ERROR_INT(
"size < 1; too small", __func__, 0);
388 for (i = 0; i < size; i++) {
394 L_ERROR(
"NUL byte not found in %d bytes\n", __func__, size);
426l_int32 lendest, lensrc;
429 return ERROR_INT(
"dest not defined", __func__, -1);
431 return ERROR_INT(
"size < 1; too small", __func__, -1);
437 return ERROR_INT(
"no terminating nul byte", __func__, -1);
441 n = (lendest + lensrc > size - 1) ? 0 : lensrc;
443 return ERROR_INT(
"dest too small for append", __func__, -1);
445 for (i = 0; i < n; i++)
446 dest[lendest + i] = src[i];
447 dest[lendest + n] =
'\0';
474 if (!first)
return NULL;
477 va_start(args, first);
479 while ((arg = va_arg(args,
const char *)) != NULL)
482 result = (
char *)LEPT_CALLOC(len + 1,
sizeof(
char));
485 va_start(args, first);
490 while ((arg = va_arg(args,
const char *)) != NULL) {
518l_int32 srclen1, srclen2, destlen;
520 srclen1 = (src1) ? strlen(src1) : 0;
521 srclen2 = (src2) ? strlen(src2) : 0;
522 destlen = srclen1 + srclen2 + 3;
524 if ((dest = (
char *)LEPT_CALLOC(destlen,
sizeof(
char))) == NULL)
525 return (
char *)ERROR_PTR(
"calloc fail for dest", __func__, NULL);
573 return ERROR_INT(
"&src1 not defined", __func__, 1);
595 return (
char *)ERROR_PTR(
"src not defined", __func__, NULL);
597 if ((dest = (
char *)LEPT_CALLOC(len + 1,
sizeof(
char))) == NULL)
598 return (
char *)ERROR_PTR(
"calloc fail for dest", __func__, NULL);
599 for (i = 0; i < len; i++)
600 dest[i] = src[len - 1 - i];
645l_int32 istart, i, j, nchars;
648 return (
char *)ERROR_PTR(
"seps not defined", __func__, NULL);
650 return (
char *)ERROR_PTR(
"&saveptr not defined", __func__, NULL);
664 for (istart = 0;; istart++) {
665 if ((nextc = start[istart]) ==
'\0') {
669 if (!strchr(seps, nextc))
676 for (i = istart;; i++) {
677 if ((nextc = start[i]) ==
'\0')
679 if (strchr(seps, nextc))
685 substr = (
char *)LEPT_CALLOC(nchars + 1,
sizeof(
char));
691 if ((nextc = start[j]) ==
'\0') {
695 if (!strchr(seps, nextc)) {
696 *psaveptr = start + j;
739 return ERROR_INT(
"&head not defined", __func__, 1);
741 return ERROR_INT(
"&tail not defined", __func__, 1);
742 *phead = *ptail = NULL;
744 return ERROR_INT(
"cstr not defined", __func__, 1);
746 return ERROR_INT(
"seps not defined", __func__, 1);
781 return ERROR_INT(
"&found not defined", __func__, 1);
784 return ERROR_INT(
"src and chars not both defined", __func__, 1);
787 for (i = 0; i < n; i++) {
789 if (strchr(chars, ch)) {
807 const char *remchars)
814 return (
char *)ERROR_PTR(
"src not defined", __func__, NULL);
818 if ((dest = (
char *)LEPT_CALLOC(strlen(src) + 1,
sizeof(
char))) == NULL)
819 return (
char *)ERROR_PTR(
"dest not made", __func__, NULL);
821 for (i = 0, k = 0; i < nsrc; i++) {
823 if (!strchr(remchars, ch))
867 if (pcount) *pcount = 0;
868 if (!src || !sub1 || !sub2)
869 return (
char *)ERROR_PTR(
"src, sub1, sub2 not all defined",
872 if (strlen(sub2) > 0) {
874 (
const l_uint8 *)src, strlen(src),
875 (
const l_uint8 *)sub1, strlen(sub1),
876 (
const l_uint8 *)sub2, strlen(sub2),
880 (
const l_uint8 *)src, strlen(src),
881 (
const l_uint8 *)sub1, strlen(sub1),
882 NULL, 0, &datalen, pcount);
923l_int32 nsrc, nsub1, nsub2, len, npre, loc;
925 if (pfound) *pfound = 0;
926 if (!src || !sub1 || !sub2)
927 return (
char *)ERROR_PTR(
"src, sub1, sub2 not all defined",
934 if (!strcmp(sub1, sub2))
936 if ((ptr = strstr(src + loc, sub1)) == NULL)
938 if (pfound) *pfound = 1;
941 nsub1 = strlen(sub1);
942 nsub2 = strlen(sub2);
943 len = nsrc + nsub2 - nsub1;
944 if ((dest = (
char *)LEPT_CALLOC(len + 1,
sizeof(
char))) == NULL)
945 return (
char *)ERROR_PTR(
"dest not made", __func__, NULL);
947 memcpy(dest, src, npre);
948 strcpy(dest + npre, sub2);
949 strcpy(dest + npre + nsub2, ptr + nsub1);
950 if (ploc) *ploc = npre + nsub2;
978 return (
L_DNA *)ERROR_PTR(
"src, sub not both defined", __func__, NULL);
981 (
const l_uint8 *)sub, strlen(sub));
1009 if (ploc) *ploc = -1;
1011 return ERROR_INT(
"src and sub not both defined", __func__, 0);
1012 if (strlen(sub) == 0)
1013 return ERROR_INT(
"substring length 0", __func__, 0);
1014 if (strlen(src) == 0)
1017 if ((ptr = strstr(src, sub)) == NULL)
1060 const l_uint8 *newseq,
1067l_int32 n, i, j, di, si, index, incr;
1070 if (pcount) *pcount = 0;
1072 return (l_uint8 *)ERROR_PTR(
"datas & seq not both defined",
1075 return (l_uint8 *)ERROR_PTR(
"&datadlen not defined", __func__, NULL);
1081 *pdatadlen = dataslen;
1086 n = l_dnaGetCount(da);
1087 if (pcount) *pcount = n;
1088 if (!newseq) newseqlen = 0;
1089 newsize = dataslen + n * (newseqlen - seqlen) + 4;
1090 if ((datad = (l_uint8 *)LEPT_CALLOC(newsize,
sizeof(l_uint8))) == NULL) {
1092 return (l_uint8 *)ERROR_PTR(
"datad not made", __func__, NULL);
1096 l_dnaGetIValue(da, 0, &si);
1097 for (i = 0, di = 0, index = 0; i < dataslen; i++) {
1101 l_dnaGetIValue(da, index, &si);
1102 incr = L_MIN(seqlen, si - i);
1108 for (j = 0; j < newseqlen; j++)
1109 datad[di++] = newseq[j];
1112 datad[di++] = datas[i];
1147 const l_uint8 *sequence,
1150l_int32 start, offset, realoffset, found;
1153 if (!data || !sequence)
1154 return (
L_DNA *)ERROR_PTR(
"data & sequence not both defined",
1157 da = l_dnaCreate(0);
1165 realoffset = start + offset;
1166 l_dnaAddNumber(da, realoffset);
1167 start = realoffset + seqlen;
1168 if (start >= datalen)
1172 if (l_dnaGetCount(da) == 0)
1207 const l_uint8 *sequence,
1212l_int32 i, j, found, lastpos;
1214 if (poffset) *poffset = 0;
1215 if (pfound) *pfound = FALSE;
1216 if (!data || !sequence)
1217 return ERROR_INT(
"data & sequence not both defined", __func__, 1);
1218 if (!poffset || !pfound)
1219 return ERROR_INT(
"&offset and &found not defined", __func__, 1);
1221 lastpos = datalen - seqlen + 1;
1223 for (i = 0; i < lastpos; i++) {
1224 for (j = 0; j < seqlen; j++) {
1225 if (data[i + j] != sequence[j])
1227 if (j == seqlen - 1)
1234 if (found == TRUE) {
1281 return ERROR_PTR(
"input data not defined", __func__, NULL);
1293 if ((newdata = (
void *)LEPT_CALLOC(1, newsize)) == NULL)
1294 return ERROR_PTR(
"newdata not made", __func__, NULL);
1299 if ((newdata = (
void *)LEPT_CALLOC(1, newsize)) == NULL)
1300 return ERROR_PTR(
"newdata not made", __func__, NULL);
1301 minsize = L_MIN(oldsize, newsize);
1302 memcpy(newdata, indata, minsize);
1327 return (l_uint8 *)ERROR_PTR(
"pnbytes not defined", __func__, NULL);
1330 return (l_uint8 *)ERROR_PTR(
"filename not defined", __func__, NULL);
1333 return (l_uint8 *)ERROR_PTR_1(
"file stream not opened",
1334 filename, __func__, NULL);
1373l_int32 seekable, navail, nadd, nread;
1377 return (l_uint8 *)ERROR_PTR(
"&nbytes not defined", __func__, NULL);
1380 return (l_uint8 *)ERROR_PTR(
"fp not defined", __func__, NULL);
1386 seekable = (ftell(fp) == 0) ? 1 : 0;
1392 bb = bbufferCreate(NULL, 4096);
1395 if (navail < 4096) {
1396 nadd = L_MAX(bb->
nalloc, 4096);
1397 bbufferExtendArray(bb, nadd);
1399 nread = fread((
void *)(bb->
array + bb->
n), 1, 4096, fp);
1401 if (nread != 4096)
break;
1406 if ((data = (l_uint8 *)LEPT_CALLOC(bb->
n + 1,
sizeof(l_uint8))) != NULL) {
1407 memcpy(data, bb->
array, bb->
n);
1410 L_ERROR(
"calloc fail for data\n", __func__);
1413 bbufferDestroy(&bb);
1443 return (l_uint8 *)ERROR_PTR(
"pnread not defined", __func__, NULL);
1446 return (l_uint8 *)ERROR_PTR(
"filename not defined", __func__, NULL);
1449 return (l_uint8 *)ERROR_PTR_1(
"file stream not opened",
1450 filename, __func__, NULL);
1484size_t bytesleft, bytestoread, nread, filebytes;
1487 return (l_uint8 *)ERROR_PTR(
"&nread not defined", __func__, NULL);
1490 return (l_uint8 *)ERROR_PTR(
"stream not defined", __func__, NULL);
1493 fseek(fp, 0, SEEK_END);
1494 filebytes = ftell(fp);
1495 fseek(fp, 0, SEEK_SET);
1496 if (start > filebytes) {
1497 L_ERROR(
"start = %zu but filebytes = %zu\n", __func__,
1502 return (l_uint8 *)LEPT_CALLOC(1, 1);
1503 bytesleft = filebytes - start;
1504 if (nbytes == 0) nbytes = bytesleft;
1505 bytestoread = (bytesleft >= nbytes) ? nbytes : bytesleft;
1508 if ((data = (l_uint8 *)LEPT_CALLOC(1, bytestoread + 1)) == NULL)
1509 return (l_uint8 *)ERROR_PTR(
"calloc fail for data", __func__, NULL);
1510 fseek(fp, start, SEEK_SET);
1511 nread = fread(data, 1, bytestoread, fp);
1512 if (nbytes != nread)
1513 L_INFO(
"%zu bytes requested; %zu bytes read\n", __func__,
1516 fseek(fp, 0, SEEK_SET);
1532 const char *operation,
1536char actualOperation[20];
1540 return ERROR_INT(
"filename not defined", __func__, 1);
1542 return ERROR_INT(
"operation not defined", __func__, 1);
1544 return ERROR_INT(
"data not defined", __func__, 1);
1546 return ERROR_INT(
"nbytes must be > 0", __func__, 1);
1548 if (strcmp(operation,
"w") && strcmp(operation,
"a"))
1549 return ERROR_INT(
"operation not one of {'w','a'}", __func__, 1);
1557 return ERROR_INT_1(
"stream not opened", filename, __func__, 1);
1558 fwrite(data, 1, nbytes, fp);
1577 return ERROR_INT(
"filename not defined", __func__, 0);
1579 return ERROR_INT_1(
"stream not opened", filename, __func__, 0);
1598 return ERROR_INT(
"stream not open", __func__, 0);
1602 return ERROR_INT(
"seek position must be > 0", __func__, 0);
1603 fseek(fp, 0, SEEK_END);
1606 return ERROR_INT(
"nbytes is < 0", __func__, 0);
1607 fseek(fp, pos, SEEK_SET);
1637 return (l_uint8 *)ERROR_PTR(
"datas not defined", __func__, NULL);
1639 if ((datad = (l_uint8 *)LEPT_CALLOC(size + 4,
sizeof(l_uint8))) == NULL)
1640 return (l_uint8 *)ERROR_PTR(
"datad not made", __func__, NULL);
1641 memcpy(datad, datas, size);
1667 const l_uint8 *data2,
1674 return ERROR_INT(
"&same not defined", __func__, 1);
1676 if (!data1 || !data2)
1677 return ERROR_INT(
"data1 and data2 not both defined", __func__, 1);
1678 if (size1 != size2)
return 0;
1679 for (i = 0; i < size1; i++) {
1680 if (data1[i] != data2[i])
1700 const char *newfile)
1707 return ERROR_INT(
"srcfile not defined", __func__, 1);
1709 return ERROR_INT(
"newfile not defined", __func__, 1);
1712 return ERROR_INT(
"data not returned", __func__, 1);
1728 const char *destfile)
1734 return ERROR_INT(
"srcfile not defined", __func__, 1);
1736 return ERROR_INT(
"destfile not defined", __func__, 1);
1759 return ERROR_INT(
"filename not defined", __func__, 1);
1761 return ERROR_INT(
"str not defined", __func__, 1);
1764 return ERROR_INT_1(
"stream not opened", filename, __func__, 1);
1765 fprintf(fp,
"%s", str);
1804 const char *rootpath,
1807l_int32 i, totlines, nlines, index;
1816 return ERROR_INT(
"filename not defined", __func__, 1);
1818 return ERROR_INT(
"rootpath not defined", __func__, 1);
1820 return ERROR_INT(
"n must be > 0", __func__, 1);
1821 if (save_empty != 0 && save_empty != 1)
1822 return ERROR_INT(
"save_empty not 0 or 1", __func__, 1);
1826 return ERROR_INT(
"data not read", __func__, 1);
1827 sa = sarrayCreateLinesFromString((
const char *)data, save_empty);
1830 return ERROR_INT(
"sa not made", __func__, 1);
1831 totlines = sarrayGetCount(sa);
1834 L_ERROR(
"num files = %d > num lines = %d\n", __func__, n, totlines);
1839 na = numaGetUniformBinSizes(totlines, n);
1841 for (i = 0; i < n; i++) {
1843 snprintf(outname,
sizeof(outname),
"%s_%d", rootpath, i);
1845 snprintf(outname,
sizeof(outname),
"%s_%d%s", rootpath, i, ext);
1846 numaGetIValue(na, i, &nlines);
1847 str = sarrayToStringRange(sa, index, nlines, 1);
1882 return (FILE *)ERROR_PTR(
"filename not defined", __func__, NULL);
1886 fp = fopen(fname,
"rb");
1893 return (FILE*)ERROR_PTR_1(
"tail not found", filename, __func__, NULL);
1894 fp = fopen(tail,
"rb");
1896 fp = (FILE *)ERROR_PTR_1(
"file not found", tail, __func__, NULL);
1919 const char *modestring)
1925 return (FILE *)ERROR_PTR(
"filename not defined", __func__, NULL);
1928 fp = fopen(fname, modestring);
1930 fp = (FILE *)ERROR_PTR_1(
"stream not opened", fname, __func__, NULL);
1956 return (FILE *)ERROR_PTR(
"data not defined", __func__, NULL);
1959 if ((fp = fmemopen((
void *)data, size,
"rb")) == NULL)
1960 return (FILE *)ERROR_PTR(
"stream not opened", __func__, NULL);
1962 L_INFO(
"no fmemopen API --> work-around: write to temp file\n", __func__);
1965 return (FILE *)ERROR_PTR(
"tmpfile stream not opened", __func__, NULL);
1967 if ((fp = tmpfile()) == NULL)
1968 return (FILE *)ERROR_PTR(
"tmpfile stream not opened", __func__, NULL);
1970 fwrite(data, 1, size, fp);
2002 L_ERROR(
"l_makeTempFilename failed, %s\n", __func__, strerror(errno));
2006 handle = _open(filename, _O_CREAT | _O_RDWR | _O_SHORT_LIVED |
2007 _O_TEMPORARY | _O_BINARY, _S_IREAD | _S_IWRITE);
2010 L_ERROR(
"_open failed, %s\n", __func__, strerror(errno));
2014 if ((fp = _fdopen(handle,
"r+b")) == NULL) {
2015 L_ERROR(
"_fdopen failed, %s\n", __func__, strerror(errno));
2054 return (FILE *)ERROR_PTR(
"filename not defined", __func__, NULL);
2056 return (FILE *)ERROR_PTR(
"mode not defined", __func__, NULL);
2081 return ERROR_INT(
"stream not defined", __func__, 1);
2105 if (nmemb <= 0 || size <= 0)
2107 return LEPT_CALLOC(nmemb, size);
2162 L_INFO(
"making named temp subdirectory %s is disabled\n",
2168 return ERROR_INT(
"subdir not defined", __func__, 1);
2169 if ((strlen(subdir) == 0) || (subdir[0] ==
'.') || (subdir[0] ==
'/'))
2170 return ERROR_INT(
"subdir not an actual subdirectory", __func__, 1);
2172 sa = sarrayCreate(0);
2173 sarraySplitString(sa, subdir,
"/");
2174 n = sarrayGetCount(sa);
2178 ret = mkdir(dir, 0777);
2180 attributes = GetFileAttributesA(dir);
2181 if (attributes == INVALID_FILE_ATTRIBUTES)
2182 ret = (CreateDirectoryA(dir, NULL) ? 0 : 1);
2185 for (i = 0; i < n; i++) {
2188 ret += mkdir(tmpdir, 0777);
2190 if (CreateDirectoryA(tmpdir, NULL) == 0)
2191 ret += (GetLastError() != ERROR_ALREADY_EXISTS);
2199 L_ERROR(
"failure to create %d directories\n", __func__, ret);
2228char *dir, *fname, *fullname;
2229l_int32 exists, ret, i, nfiles;
2238 return ERROR_INT(
"subdir not defined", __func__, 1);
2239 if ((strlen(subdir) == 0) || (subdir[0] ==
'.') || (subdir[0] ==
'/'))
2240 return ERROR_INT(
"subdir not an actual subdirectory", __func__, 1);
2245 return ERROR_INT(
"directory name not made", __func__, 1);
2253 if ((sa = getFilenamesInDirectory(dir)) == NULL) {
2254 L_ERROR(
"directory %s does not exist!\n", __func__, dir);
2258 nfiles = sarrayGetCount(sa);
2260 for (i = 0; i < nfiles; i++) {
2261 fname = sarrayGetString(sa, i,
L_NOCOPY);
2264 LEPT_FREE(fullname);
2269 ret = rmdir(realdir);
2273 ret = (RemoveDirectoryA(newpath) ? 0 : 1);
2304 if (!pexists)
return;
2313 l_int32 err = stat(realdir, &s);
2314 if (err != -1 && S_ISDIR(s.st_mode))
2319 l_uint32 attributes;
2320 attributes = GetFileAttributesA(realdir);
2321 if (attributes != INVALID_FILE_ATTRIBUTES &&
2322 (attributes & FILE_ATTRIBUTE_DIRECTORY))
2365 if ((sa = getSortedPathnamesInDirectory(tempdir, substr, 0, 0)) == NULL)
2366 return ERROR_INT(
"sa not made", __func__, -1);
2367 n = sarrayGetCount(sa);
2369 L_WARNING(
"no matching files found\n", __func__);
2375 for (i = 0; i < n; i++) {
2376 fname = sarrayGetString(sa, i,
L_NOCOPY);
2379 L_ERROR(
"failed to remove %s\n", __func__, path);
2410 if (!tail || strlen(tail) == 0)
2411 return ERROR_INT(
"tail undefined or empty", __func__, 1);
2414 return ERROR_INT(
"temp dirname not made", __func__, 1);
2448 if (!filepath || strlen(filepath) == 0)
2449 return ERROR_INT(
"filepath undefined or empty", __func__, 1);
2452 ret = remove(filepath);
2455 SetFileAttributesA(filepath, FILE_ATTRIBUTE_NORMAL);
2456 ret = DeleteFileA(filepath) ? 0 : 1;
2498 const char *newtail,
2501char *srcpath, *newpath, *dir, *srctail;
2506 return ERROR_INT(
"srcfile not defined", __func__, 1);
2510 return ERROR_INT(
"newdir not NULL or a subdir of /tmp", __func__, 1);
2520 if (!newtail || newtail[0] ==
'\0')
2521 newpath =
pathJoin(newtemp, srctail);
2523 newpath =
pathJoin(newtemp, newtail);
2531 LEPT_FREE(realpath);
2538 if (!newtail || newtail[0] ==
'\0')
2545 ret = MoveFileExA(srcpath, newpath,
2546 MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING) ? 0 : 1;
2551 *pnewpath = newpath;
2594 const char *newtail,
2597char *srcpath, *newpath, *dir, *srctail;
2602 return ERROR_INT(
"srcfile not defined", __func__, 1);
2606 return ERROR_INT(
"newdir not NULL or a subdir of /tmp", __func__, 1);
2616 if (!newtail || newtail[0] ==
'\0')
2617 newpath =
pathJoin(newtemp, srctail);
2619 newpath =
pathJoin(newtemp, newtail);
2629 if (!newtail || newtail[0] ==
'\0')
2636 ret = CopyFileA(srcpath, newpath, FALSE) ? 0 : 1;
2641 *pnewpath = newpath;
2651#if defined(__APPLE__)
2652 #include "TargetConditionals.h"
2676 L_ERROR(
"cmd not defined\n", __func__);
2679 if (LeptDebugOK == FALSE) {
2680 L_INFO(
"'system' calls are disabled\n", __func__);
2684#if defined(__APPLE__)
2686 #if (defined(TARGET_OS_OSX) && TARGET_OS_OSX == 1)
2688 #elif TARGET_OS_IPHONE || defined(OS_IOS)
2689 L_ERROR(
"iOS 11 does not support system()\n", __func__);
2745char *cpathname, *lastslash;
2747 if (!pdir && !ptail)
2748 return ERROR_INT(
"null input for both strings", __func__, 1);
2749 if (pdir) *pdir = NULL;
2750 if (ptail) *ptail = NULL;
2752 return ERROR_INT(
"pathname not defined", __func__, 1);
2756 lastslash = strrchr(cpathname,
'/');
2761 *(lastslash + 1) =
'\0';
2764 LEPT_FREE(cpathname);
2772 LEPT_FREE(cpathname);
2813char *tail, *dir, *lastdot;
2816 if (!pbasename && !pextension)
2817 return ERROR_INT(
"null input for both strings", __func__, 1);
2818 if (pbasename) *pbasename = NULL;
2819 if (pextension) *pextension = NULL;
2821 return ERROR_INT(
"pathname not defined", __func__, 1);
2828 if ((lastdot = strrchr(tail,
'.'))) {
2889const char *slash =
"/";
2891l_int32 i, n1, n2, emptydir;
2898 if (dir && strlen(dir) >= 2 && dir[0] ==
'.' && dir[1] ==
'.')
2899 return (
char *)ERROR_PTR(
"dir starts with '..'", __func__, NULL);
2900 if (fname && strlen(fname) >= 2 && fname[0] ==
'.' && fname[1] ==
'.')
2901 return (
char *)ERROR_PTR(
"fname starts with '..'", __func__, NULL);
2903 sa1 = sarrayCreate(0);
2904 sa2 = sarrayCreate(0);
2905 ba = l_byteaCreate(4);
2908 if (dir && strlen(dir) > 0) {
2910 l_byteaAppendString(ba, slash);
2911 sarraySplitString(sa1, dir,
"/");
2912 n1 = sarrayGetCount(sa1);
2913 for (i = 0; i < n1; i++) {
2914 str = sarrayGetString(sa1, i,
L_NOCOPY);
2915 l_byteaAppendString(ba, str);
2916 l_byteaAppendString(ba, slash);
2921 emptydir = dir && strlen(dir) == 0;
2922 if ((!dir || emptydir) && fname && strlen(fname) > 0 && fname[0] ==
'/')
2923 l_byteaAppendString(ba, slash);
2926 if (fname && strlen(fname) > 0) {
2927 sarraySplitString(sa2, fname,
"/");
2928 n2 = sarrayGetCount(sa2);
2929 for (i = 0; i < n2; i++) {
2930 str = sarrayGetString(sa2, i,
L_NOCOPY);
2931 l_byteaAppendString(ba, str);
2932 l_byteaAppendString(ba, slash);
2937 dest = (
char *)l_byteaCopyData(ba, &size);
2938 if (size > 1 && dest[size - 1] ==
'/')
2939 dest[size - 1] =
'\0';
2941 sarrayDestroy(&sa1);
2942 sarrayDestroy(&sa2);
2943 l_byteaDestroy(&ba);
2964 const char *subdirs)
2967size_t len1, len2, len3, len4;
2969 if (!basedir || !subdirs)
2970 return (
char *)ERROR_PTR(
"basedir and subdirs not both defined",
2973 len1 = strlen(basedir);
2974 len2 = strlen(subdirs);
2975 len3 = len1 + len2 + 8;
2976 if ((newdir = (
char *)LEPT_CALLOC(len3, 1)) == NULL)
2977 return (
char *)ERROR_PTR(
"newdir not made", __func__, NULL);
2979 if (newdir[len1 - 1] !=
'/')
2981 if (subdirs[0] ==
'/')
2985 len4 = strlen(newdir);
2986 if (newdir[len4 - 1] ==
'/')
2987 newdir[len4 - 1] =
'\0';
3024 return ERROR_INT(
"path not defined", __func__, 1);
3025 if (type != UNIX_PATH_SEPCHAR && type != WIN_PATH_SEPCHAR)
3026 return ERROR_INT(
"invalid type", __func__, 1);
3029 if (type == UNIX_PATH_SEPCHAR) {
3031 for (i = 0; i < len; i++) {
3032 if (path[i] ==
'\\')
3037 for (i = 0; i < len; i++) {
3086#if defined(REWRITE_TMP)
3087l_int32 rewrite_tmp = TRUE;
3089l_int32 rewrite_tmp = FALSE;
3091char *cdir, *pathout;
3092l_int32 dirlen, namelen;
3096 return (
char *)ERROR_PTR(
"no input", __func__, NULL);
3099 if (!dir || dir[0] ==
'\0') {
3100 if ((cdir = getcwd(NULL, 0)) == NULL)
3101 return (
char *)ERROR_PTR(
"no current dir found", __func__, NULL);
3104 return (
char *)ERROR_PTR(
"stringNew failed", __func__, NULL);
3110 dirlen = strlen(cdir);
3111 if (cdir[dirlen - 1] ==
'/' && dirlen != 1) {
3112 cdir[dirlen - 1] =
'\0';
3116 namelen = (fname) ? strlen(fname) : 0;
3117 size = dirlen + namelen + 256;
3118 if ((pathout = (
char *)LEPT_CALLOC(size,
sizeof(
char))) == NULL) {
3120 return (
char *)ERROR_PTR(
"pathout not made", __func__, NULL);
3127 if (!rewrite_tmp || dirlen < 4 ||
3128 (dirlen == 4 && strncmp(cdir,
"/tmp", 4) != 0) ||
3129 (dirlen > 4 && strncmp(cdir,
"/tmp/", 5) != 0)) {
3132#if defined(__APPLE__)
3133 size_t n = confstr(_CS_DARWIN_USER_TEMP_DIR, pathout, size);
3134 if (n == 0 || n > size) {
3142#elif defined(_WIN32)
3144 char tmpdir[MAX_PATH];
3145 GetTempPathA(
sizeof(tmpdir), tmpdir);
3146 tmpdirlen = strlen(tmpdir);
3147 if (tmpdirlen > 0 && tmpdir[tmpdirlen - 1] ==
'\\') {
3148 tmpdir[tmpdirlen - 1] =
'\0';
3150 tmpdirlen = strlen(tmpdir);
3160 if (fname && strlen(fname) > 0) {
3161 dirlen = strlen(pathout);
3162 pathout[dirlen] =
'/';
3208 return ERROR_INT(
"result not defined", __func__, 1);
3209 if (subdir && ((subdir[0] ==
'.') || (subdir[0] ==
'/')))
3210 return ERROR_INT(
"subdir not an actual subdirectory", __func__, 1);
3212 memset(result, 0, nbytes);
3216#if defined(REWRITE_TMP)
3221 pathlen = strlen(path);
3222 if (pathlen < nbytes - 1) {
3225 L_ERROR(
"result array too small for path\n", __func__);
3257 return ERROR_INT(
"path not defined", __func__, 1);
3259 return ERROR_INT(
"invalid flag", __func__, 1);
3262 lastchar = path[len - 1];
3265 path[len + 1] =
'\0';
3267 path[len - 1] =
'\0';
3302 return (
char *)ERROR_PTR(
"failed to make dirname", __func__, NULL);
3309 fd = mkstemp(pattern);
3312 return (
char *)ERROR_PTR(
"mkstemp failed", __func__, NULL);
3319 char fname[MAX_PATH];
3321 if (GetTempFileNameA(dirname,
"lp.", 0, fname) == 0)
3322 return (
char *)ERROR_PTR(
"GetTempFileName failed", __func__, NULL);
3323 if ((fp = fopen(fname,
"wb")) == NULL)
3324 return (
char *)ERROR_PTR(
"file cannot be written to", __func__, NULL);
3355char *tail, *basename;
3356l_int32 len, nret, num;
3359 return ERROR_INT(
"fname not defined", __func__, -1);
3365 len = strlen(basename);
3366 if (numpre + numpost > len - 1) {
3367 LEPT_FREE(basename);
3368 return ERROR_INT(
"numpre + numpost too big", __func__, -1);
3371 basename[len - numpost] =
'\0';
3372 nret = sscanf(basename + numpre,
"%d", &num);
3373 LEPT_FREE(basename);
l_int32 stringLength(const char *src, size_t size)
stringLength()
size_t nbytesInFile(const char *filename)
nbytesInFile()
l_int32 lept_rmdir(const char *subdir)
lept_rmdir()
char * stringCopySegment(const char *src, l_int32 start, l_int32 nbytes)
stringCopySegment()
void lept_direxists(const char *dir, l_int32 *pexists)
lept_direxists()
l_int32 lept_rm(const char *subdir, const char *tail)
lept_rm()
l_ok lept_fclose(FILE *fp)
lept_fclose()
char * stringReverse(const char *src)
stringReverse()
l_int32 lept_rmfile(const char *filepath)
lept_rmfile()
FILE * fopenWriteWinTempfile(void)
fopenWriteWinTempfile()
char * pathJoin(const char *dir, const char *fname)
pathJoin()
l_uint8 * l_binaryCopy(const l_uint8 *datas, size_t size)
l_binaryCopy()
l_uint8 * l_binaryReadSelectStream(FILE *fp, size_t start, size_t nbytes, size_t *pnread)
l_binaryReadSelectStream()
l_ok fileAppendString(const char *filename, const char *str)
fileAppendString()
l_int32 lept_rm_match(const char *subdir, const char *substr)
lept_rm_match()
l_ok stringReplace(char **pdest, const char *src)
stringReplace()
char * strtokSafe(char *cstr, const char *seps, char **psaveptr)
strtokSafe()
l_ok fileConcatenate(const char *srcfile, const char *destfile)
fileConcatenate()
l_ok stringJoinIP(char **psrc1, const char *src2)
stringJoinIP()
char * stringRemoveChars(const char *src, const char *remchars)
stringRemoveChars()
FILE * fopenWriteStream(const char *filename, const char *modestring)
fopenWriteStream()
void lept_free(void *ptr)
lept_free()
char * l_makeTempFilename(void)
l_makeTempFilename()
l_ok modifyTrailingSlash(char *path, size_t nbytes, l_int32 flag)
modifyTrailingSlash()
l_ok splitPathAtExtension(const char *pathname, char **pbasename, char **pextension)
splitPathAtExtension()
char * genPathname(const char *dir, const char *fname)
genPathname()
void callSystemDebug(const char *cmd)
callSystemDebug()
l_int32 lept_cp(const char *srcfile, const char *newdir, const char *newtail, char **pnewpath)
lept_cp()
l_int32 lept_mv(const char *srcfile, const char *newdir, const char *newtail, char **pnewpath)
lept_mv()
l_ok stringCheckForChars(const char *src, const char *chars, l_int32 *pfound)
stringCheckForChars()
FILE * lept_fopen(const char *filename, const char *mode)
lept_fopen()
char * stringReplaceEachSubstr(const char *src, const char *sub1, const char *sub2, l_int32 *pcount)
stringReplaceEachSubstr()
char * stringConcatNew(const char *first,...)
stringConcatNew()
l_ok l_binaryCompare(const l_uint8 *data1, size_t size1, const l_uint8 *data2, size_t size2, l_int32 *psame)
l_binaryCompare()
size_t fnbytesInFile(FILE *fp)
fnbytesInFile()
l_ok splitPathAtDirectory(const char *pathname, char **pdir, char **ptail)
splitPathAtDirectory()
l_uint8 * l_binaryReadSelect(const char *filename, size_t start, size_t nbytes, size_t *pnread)
l_binaryReadSelect()
l_uint8 * l_binaryRead(const char *filename, size_t *pnbytes)
l_binaryRead()
FILE * fopenReadFromMemory(const l_uint8 *data, size_t size)
fopenReadFromMemory()
l_int32 stringFindSubstr(const char *src, const char *sub, l_int32 *ploc)
stringFindSubstr()
char * stringJoin(const char *src1, const char *src2)
stringJoin()
l_ok convertSepCharsInPath(char *path, l_int32 type)
convertSepCharsInPath()
l_int32 stringCat(char *dest, size_t size, const char *src)
stringCat()
FILE * fopenReadStream(const char *filename)
fopenReadStream()
l_uint8 * l_binaryReadStream(FILE *fp, size_t *pnbytes)
l_binaryReadStream()
char * stringReplaceSubstr(const char *src, const char *sub1, const char *sub2, l_int32 *ploc, l_int32 *pfound)
stringReplaceSubstr()
void * reallocNew(void **pindata, size_t oldsize, size_t newsize)
reallocNew()
l_int32 lept_mkdir(const char *subdir)
lept_mkdir()
L_DNA * arrayFindEachSequence(const l_uint8 *data, size_t datalen, const l_uint8 *sequence, size_t seqlen)
arrayFindEachSequence()
l_uint8 * arrayReplaceEachSequence(const l_uint8 *datas, size_t dataslen, const l_uint8 *seq, size_t seqlen, const l_uint8 *newseq, size_t newseqlen, size_t *pdatadlen, l_int32 *pcount)
arrayReplaceEachSequence()
l_ok l_binaryWrite(const char *filename, const char *operation, const void *data, size_t nbytes)
l_binaryWrite()
l_ok makeTempDirname(char *result, size_t nbytes, const char *subdir)
makeTempDirname()
l_ok stringSplitOnToken(char *cstr, const char *seps, char **phead, char **ptail)
stringSplitOnToken()
void * lept_calloc(size_t nmemb, size_t size)
lept_calloc()
L_DNA * stringFindEachSubstr(const char *src, const char *sub)
stringFindEachSubstr()
char * stringNew(const char *src)
stringNew()
l_ok stringCopy(char *dest, const char *src, l_int32 n)
stringCopy()
l_ok fileSplitLinesUniform(const char *filename, l_int32 n, l_int32 save_empty, const char *rootpath, const char *ext)
fileSplitLinesUniform()
l_int32 extractNumberFromFilename(const char *fname, l_int32 numpre, l_int32 numpost)
extractNumberFromFilename()
char * appendSubdirs(const char *basedir, const char *subdirs)
appendSubdirs()
l_ok arrayFindSequence(const l_uint8 *data, size_t datalen, const l_uint8 *sequence, size_t seqlen, l_int32 *poffset, l_int32 *pfound)
arrayFindSequence()
l_ok fileCopy(const char *srcfile, const char *newfile)
fileCopy()