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 %zu 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 L_ERROR(
"failed to open locally with tail %s for filename %s\n",
1897 __func__, tail, filename);
1920 const char *modestring)
1926 return (FILE *)ERROR_PTR(
"filename not defined", __func__, NULL);
1929 fp = fopen(fname, modestring);
1931 fp = (FILE *)ERROR_PTR_1(
"stream not opened", fname, __func__, NULL);
1957 return (FILE *)ERROR_PTR(
"data not defined", __func__, NULL);
1960 if ((fp = fmemopen((
void *)data, size,
"rb")) == NULL)
1961 return (FILE *)ERROR_PTR(
"stream not opened", __func__, NULL);
1963 L_INFO(
"no fmemopen API --> work-around: write to temp file\n", __func__);
1966 return (FILE *)ERROR_PTR(
"tmpfile stream not opened", __func__, NULL);
1968 if ((fp = tmpfile()) == NULL)
1969 return (FILE *)ERROR_PTR(
"tmpfile stream not opened", __func__, NULL);
1971 fwrite(data, 1, size, fp);
2003 L_ERROR(
"l_makeTempFilename failed, %s\n", __func__, strerror(errno));
2007 handle = _open(filename, _O_CREAT | _O_RDWR | _O_SHORT_LIVED |
2008 _O_TEMPORARY | _O_BINARY, _S_IREAD | _S_IWRITE);
2011 L_ERROR(
"_open failed, %s\n", __func__, strerror(errno));
2015 if ((fp = _fdopen(handle,
"r+b")) == NULL) {
2016 L_ERROR(
"_fdopen failed, %s\n", __func__, strerror(errno));
2055 return (FILE *)ERROR_PTR(
"filename not defined", __func__, NULL);
2057 return (FILE *)ERROR_PTR(
"mode not defined", __func__, NULL);
2082 return ERROR_INT(
"stream not defined", __func__, 1);
2106 if (nmemb <= 0 || size <= 0)
2108 return LEPT_CALLOC(nmemb, size);
2163 L_INFO(
"making named temp subdirectory %s is disabled\n",
2169 return ERROR_INT(
"subdir not defined", __func__, 1);
2170 if ((strlen(subdir) == 0) || (subdir[0] ==
'.') || (subdir[0] ==
'/'))
2171 return ERROR_INT(
"subdir not an actual subdirectory", __func__, 1);
2173 sa = sarrayCreate(0);
2174 sarraySplitString(sa, subdir,
"/");
2175 n = sarrayGetCount(sa);
2179 ret = mkdir(dir, 0777);
2181 attributes = GetFileAttributesA(dir);
2182 if (attributes == INVALID_FILE_ATTRIBUTES)
2183 ret = (CreateDirectoryA(dir, NULL) ? 0 : 1);
2186 for (i = 0; i < n; i++) {
2189 ret += mkdir(tmpdir, 0777);
2191 if (CreateDirectoryA(tmpdir, NULL) == 0)
2192 ret += (GetLastError() != ERROR_ALREADY_EXISTS);
2200 L_ERROR(
"failure to create %d directories\n", __func__, ret);
2229char *dir, *fname, *fullname;
2230l_int32 exists, ret, i, nfiles;
2239 return ERROR_INT(
"subdir not defined", __func__, 1);
2240 if ((strlen(subdir) == 0) || (subdir[0] ==
'.') || (subdir[0] ==
'/'))
2241 return ERROR_INT(
"subdir not an actual subdirectory", __func__, 1);
2246 return ERROR_INT(
"directory name not made", __func__, 1);
2254 if ((sa = getFilenamesInDirectory(dir)) == NULL) {
2255 L_ERROR(
"directory %s does not exist!\n", __func__, dir);
2259 nfiles = sarrayGetCount(sa);
2261 for (i = 0; i < nfiles; i++) {
2262 fname = sarrayGetString(sa, i,
L_NOCOPY);
2265 LEPT_FREE(fullname);
2270 ret = rmdir(realdir);
2274 ret = (RemoveDirectoryA(newpath) ? 0 : 1);
2305 if (!pexists)
return;
2314 l_int32 err = stat(realdir, &s);
2315 if (err != -1 && S_ISDIR(s.st_mode))
2320 l_uint32 attributes;
2321 attributes = GetFileAttributesA(realdir);
2322 if (attributes != INVALID_FILE_ATTRIBUTES &&
2323 (attributes & FILE_ATTRIBUTE_DIRECTORY))
2366 if ((sa = getSortedPathnamesInDirectory(tempdir, substr, 0, 0)) == NULL)
2367 return ERROR_INT(
"sa not made", __func__, -1);
2368 n = sarrayGetCount(sa);
2370 L_WARNING(
"no matching files found\n", __func__);
2376 for (i = 0; i < n; i++) {
2377 fname = sarrayGetString(sa, i,
L_NOCOPY);
2380 L_ERROR(
"failed to remove %s\n", __func__, path);
2411 if (!tail || strlen(tail) == 0)
2412 return ERROR_INT(
"tail undefined or empty", __func__, 1);
2415 return ERROR_INT(
"temp dirname not made", __func__, 1);
2449 if (!filepath || strlen(filepath) == 0)
2450 return ERROR_INT(
"filepath undefined or empty", __func__, 1);
2453 ret = remove(filepath);
2456 SetFileAttributesA(filepath, FILE_ATTRIBUTE_NORMAL);
2457 ret = DeleteFileA(filepath) ? 0 : 1;
2499 const char *newtail,
2502char *srcpath, *newpath, *dir, *srctail;
2507 return ERROR_INT(
"srcfile not defined", __func__, 1);
2511 return ERROR_INT(
"newdir not NULL or a subdir of /tmp", __func__, 1);
2521 if (!newtail || newtail[0] ==
'\0')
2522 newpath =
pathJoin(newtemp, srctail);
2524 newpath =
pathJoin(newtemp, newtail);
2532 LEPT_FREE(realpath);
2539 if (!newtail || newtail[0] ==
'\0')
2546 ret = MoveFileExA(srcpath, newpath,
2547 MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING) ? 0 : 1;
2552 *pnewpath = newpath;
2595 const char *newtail,
2598char *srcpath, *newpath, *dir, *srctail;
2603 return ERROR_INT(
"srcfile not defined", __func__, 1);
2607 return ERROR_INT(
"newdir not NULL or a subdir of /tmp", __func__, 1);
2617 if (!newtail || newtail[0] ==
'\0')
2618 newpath =
pathJoin(newtemp, srctail);
2620 newpath =
pathJoin(newtemp, newtail);
2630 if (!newtail || newtail[0] ==
'\0')
2637 ret = CopyFileA(srcpath, newpath, FALSE) ? 0 : 1;
2642 *pnewpath = newpath;
2652#if defined(__APPLE__)
2653 #include "TargetConditionals.h"
2677 L_ERROR(
"cmd not defined\n", __func__);
2680 if (LeptDebugOK == FALSE) {
2681 L_INFO(
"'system' calls are disabled\n", __func__);
2685#if defined(__APPLE__)
2687 #if (defined(TARGET_OS_OSX) && TARGET_OS_OSX == 1)
2689 #elif TARGET_OS_IPHONE || defined(OS_IOS)
2690 L_ERROR(
"iOS 11 does not support system()\n", __func__);
2748char *cpathname, *lastslash;
2750 if (!pdir && !ptail)
2751 return ERROR_INT(
"null input for both strings", __func__, 1);
2752 if (pdir) *pdir = NULL;
2753 if (ptail) *ptail = NULL;
2755 return ERROR_INT(
"pathname not defined", __func__, 1);
2759 lastslash = strrchr(cpathname,
'/');
2764 *(lastslash + 1) =
'\0';
2767 LEPT_FREE(cpathname);
2775 LEPT_FREE(cpathname);
2816char *tail, *dir, *lastdot;
2819 if (!pbasename && !pextension)
2820 return ERROR_INT(
"null input for both strings", __func__, 1);
2821 if (pbasename) *pbasename = NULL;
2822 if (pextension) *pextension = NULL;
2824 return ERROR_INT(
"pathname not defined", __func__, 1);
2831 if ((lastdot = strrchr(tail,
'.'))) {
2892const char *slash =
"/";
2894l_int32 i, n1, n2, emptydir;
2901 if (dir && strlen(dir) >= 2 && dir[0] ==
'.' && dir[1] ==
'.')
2902 return (
char *)ERROR_PTR(
"dir starts with '..'", __func__, NULL);
2903 if (fname && strlen(fname) >= 2 && fname[0] ==
'.' && fname[1] ==
'.')
2904 return (
char *)ERROR_PTR(
"fname starts with '..'", __func__, NULL);
2906 sa1 = sarrayCreate(0);
2907 sa2 = sarrayCreate(0);
2908 ba = l_byteaCreate(4);
2911 if (dir && strlen(dir) > 0) {
2913 l_byteaAppendString(ba, slash);
2914 sarraySplitString(sa1, dir,
"/");
2915 n1 = sarrayGetCount(sa1);
2916 for (i = 0; i < n1; i++) {
2917 str = sarrayGetString(sa1, i,
L_NOCOPY);
2918 l_byteaAppendString(ba, str);
2919 l_byteaAppendString(ba, slash);
2924 emptydir = dir && strlen(dir) == 0;
2925 if ((!dir || emptydir) && fname && strlen(fname) > 0 && fname[0] ==
'/')
2926 l_byteaAppendString(ba, slash);
2929 if (fname && strlen(fname) > 0) {
2930 sarraySplitString(sa2, fname,
"/");
2931 n2 = sarrayGetCount(sa2);
2932 for (i = 0; i < n2; i++) {
2933 str = sarrayGetString(sa2, i,
L_NOCOPY);
2934 l_byteaAppendString(ba, str);
2935 l_byteaAppendString(ba, slash);
2940 dest = (
char *)l_byteaCopyData(ba, &size);
2941 if (size > 1 && dest[size - 1] ==
'/')
2942 dest[size - 1] =
'\0';
2944 sarrayDestroy(&sa1);
2945 sarrayDestroy(&sa2);
2946 l_byteaDestroy(&ba);
2967 const char *subdirs)
2970size_t len1, len2, len3, len4;
2972 if (!basedir || !subdirs)
2973 return (
char *)ERROR_PTR(
"basedir and subdirs not both defined",
2976 len1 = strlen(basedir);
2977 len2 = strlen(subdirs);
2978 len3 = len1 + len2 + 8;
2979 if ((newdir = (
char *)LEPT_CALLOC(len3, 1)) == NULL)
2980 return (
char *)ERROR_PTR(
"newdir not made", __func__, NULL);
2982 if (newdir[len1 - 1] !=
'/')
2984 if (subdirs[0] ==
'/')
2988 len4 = strlen(newdir);
2989 if (newdir[len4 - 1] ==
'/')
2990 newdir[len4 - 1] =
'\0';
3027 return ERROR_INT(
"path not defined", __func__, 1);
3028 if (type != UNIX_PATH_SEPCHAR && type != WIN_PATH_SEPCHAR)
3029 return ERROR_INT(
"invalid type", __func__, 1);
3032 if (type == UNIX_PATH_SEPCHAR) {
3034 for (i = 0; i < len; i++) {
3035 if (path[i] ==
'\\')
3040 for (i = 0; i < len; i++) {
3089#if defined(REWRITE_TMP)
3090l_int32 rewrite_tmp = TRUE;
3092l_int32 rewrite_tmp = FALSE;
3094char *cdir, *pathout;
3095l_int32 dirlen, namelen;
3099 return (
char *)ERROR_PTR(
"no input", __func__, NULL);
3102 if (!dir || dir[0] ==
'\0') {
3103 if ((cdir = getcwd(NULL, 0)) == NULL)
3104 return (
char *)ERROR_PTR(
"no current dir found", __func__, NULL);
3107 return (
char *)ERROR_PTR(
"stringNew failed", __func__, NULL);
3113 dirlen = strlen(cdir);
3114 if (cdir[dirlen - 1] ==
'/' && dirlen != 1) {
3115 cdir[dirlen - 1] =
'\0';
3119 namelen = (fname) ? strlen(fname) : 0;
3120 size = dirlen + namelen + 256;
3121 if ((pathout = (
char *)LEPT_CALLOC(size,
sizeof(
char))) == NULL) {
3123 return (
char *)ERROR_PTR(
"pathout not made", __func__, NULL);
3130 if (!rewrite_tmp || dirlen < 4 ||
3131 (dirlen == 4 && strncmp(cdir,
"/tmp", 4) != 0) ||
3132 (dirlen > 4 && strncmp(cdir,
"/tmp/", 5) != 0)) {
3135#if defined(__APPLE__)
3136 size_t n = confstr(_CS_DARWIN_USER_TEMP_DIR, pathout, size);
3137 if (n == 0 || n > size) {
3145#elif defined(_WIN32)
3147 char tmpdir[MAX_PATH];
3148 GetTempPathA(
sizeof(tmpdir), tmpdir);
3149 tmpdirlen = strlen(tmpdir);
3150 if (tmpdirlen > 0 && tmpdir[tmpdirlen - 1] ==
'\\') {
3151 tmpdir[tmpdirlen - 1] =
'\0';
3153 tmpdirlen = strlen(tmpdir);
3163 if (fname && strlen(fname) > 0) {
3164 dirlen = strlen(pathout);
3165 pathout[dirlen] =
'/';
3211 return ERROR_INT(
"result not defined", __func__, 1);
3212 if (subdir && ((subdir[0] ==
'.') || (subdir[0] ==
'/')))
3213 return ERROR_INT(
"subdir not an actual subdirectory", __func__, 1);
3215 memset(result, 0, nbytes);
3219#if defined(REWRITE_TMP)
3224 pathlen = strlen(path);
3225 if (pathlen < nbytes - 1) {
3228 L_ERROR(
"result array too small for path\n", __func__);
3260 return ERROR_INT(
"path not defined", __func__, 1);
3262 return ERROR_INT(
"invalid flag", __func__, 1);
3265 lastchar = path[len - 1];
3268 path[len + 1] =
'\0';
3270 path[len - 1] =
'\0';
3305 return (
char *)ERROR_PTR(
"failed to make dirname", __func__, NULL);
3312 fd = mkstemp(pattern);
3315 return (
char *)ERROR_PTR(
"mkstemp failed", __func__, NULL);
3322 char fname[MAX_PATH];
3324 if (GetTempFileNameA(dirname,
"lp.", 0, fname) == 0)
3325 return (
char *)ERROR_PTR(
"GetTempFileName failed", __func__, NULL);
3326 if ((fp = fopen(fname,
"wb")) == NULL)
3327 return (
char *)ERROR_PTR(
"file cannot be written to", __func__, NULL);
3358char *tail, *basename;
3359l_int32 len, nret, num;
3362 return ERROR_INT(
"fname not defined", __func__, -1);
3368 len = strlen(basename);
3369 if (numpre + numpost > len - 1) {
3370 LEPT_FREE(basename);
3371 return ERROR_INT(
"numpre + numpost too big", __func__, -1);
3374 basename[len - numpost] =
'\0';
3375 nret = sscanf(basename + numpre,
"%d", &num);
3376 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()
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()
l_int32 callSystemDebug(const char *cmd)
callSystemDebug()
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()