182 #include <config_auto.h>
188 #define getcwd _getcwd
197 #include <sys/stat.h>
199 #include <sys/stat.h>
200 #include <sys/types.h>
210 #include "allheaders.h"
229 L_WARNING(
"src not defined\n", __func__);
234 if ((dest = (
char *)LEPT_CALLOC(len + 1,
sizeof(
char))) == NULL)
235 return (
char *)ERROR_PTR(
"dest not made", __func__, NULL);
268 return ERROR_INT(
"dest not defined", __func__, 1);
273 for (i = 0; i < n && src[i] !=
'\0'; i++)
309 return (
char *)ERROR_PTR(
"src not defined", __func__, NULL);
311 if (start < 0 || start > len - 1)
312 return (
char *)ERROR_PTR(
"invalid start", __func__, NULL);
314 nbytes = len - start;
315 if (start + nbytes > len)
316 nbytes = len - start;
317 if ((dest = (
char *)LEPT_CALLOC(nbytes + 1,
sizeof(
char))) == NULL)
318 return (
char *)ERROR_PTR(
"dest not made", __func__, NULL);
343 return ERROR_INT(
"pdest not defined", __func__, 1);
379 return ERROR_INT(
"src not defined", __func__, 0);
383 for (i = 0; i < size; i++) {
418 l_int32 lendest, lensrc;
421 return ERROR_INT(
"dest not defined", __func__, -1);
423 return ERROR_INT(
"size < 1; too small", __func__, -1);
429 return ERROR_INT(
"no terminating nul byte", __func__, -1);
433 n = (lendest + lensrc > size - 1 ? 0 : lensrc);
435 return ERROR_INT(
"dest too small for append", __func__, -1);
437 for (i = 0; i < n; i++)
438 dest[lendest + i] = src[i];
439 dest[lendest + n] =
'\0';
466 if (!first)
return NULL;
469 va_start(args, first);
471 while ((arg = va_arg(args,
const char *)) != NULL)
474 result = (
char *)LEPT_CALLOC(len + 1,
sizeof(
char));
477 va_start(args, first);
482 while ((arg = va_arg(args,
const char *)) != NULL) {
510 l_int32 srclen1, srclen2, destlen;
512 srclen1 = (src1) ? strlen(src1) : 0;
513 srclen2 = (src2) ? strlen(src2) : 0;
514 destlen = srclen1 + srclen2 + 3;
516 if ((dest = (
char *)LEPT_CALLOC(destlen,
sizeof(
char))) == NULL)
517 return (
char *)ERROR_PTR(
"calloc fail for dest", __func__, NULL);
565 return ERROR_INT(
"&src1 not defined", __func__, 1);
587 return (
char *)ERROR_PTR(
"src not defined", __func__, NULL);
589 if ((dest = (
char *)LEPT_CALLOC(len + 1,
sizeof(
char))) == NULL)
590 return (
char *)ERROR_PTR(
"calloc fail for dest", __func__, NULL);
591 for (i = 0; i < len; i++)
592 dest[i] = src[len - 1 - i];
636 char *start, *substr;
637 l_int32 istart, i, j, nchars;
640 return (
char *)ERROR_PTR(
"seps not defined", __func__, NULL);
642 return (
char *)ERROR_PTR(
"&saveptr not defined", __func__, NULL);
656 for (istart = 0;; istart++) {
657 if ((nextc = start[istart]) ==
'\0') {
661 if (!strchr(seps, nextc))
668 for (i = istart;; i++) {
669 if ((nextc = start[i]) ==
'\0')
671 if (strchr(seps, nextc))
677 substr = (
char *)LEPT_CALLOC(nchars + 1,
sizeof(
char));
683 if ((nextc = start[j]) ==
'\0') {
687 if (!strchr(seps, nextc)) {
688 *psaveptr = start + j;
731 return ERROR_INT(
"&head not defined", __func__, 1);
733 return ERROR_INT(
"&tail not defined", __func__, 1);
734 *phead = *ptail = NULL;
736 return ERROR_INT(
"cstr not defined", __func__, 1);
738 return ERROR_INT(
"seps not defined", __func__, 1);
773 return ERROR_INT(
"&found not defined", __func__, 1);
776 return ERROR_INT(
"src and chars not both defined", __func__, 1);
779 for (i = 0; i < n; i++) {
781 if (strchr(chars, ch)) {
799 const char *remchars)
806 return (
char *)ERROR_PTR(
"src not defined", __func__, NULL);
810 if ((dest = (
char *)LEPT_CALLOC(strlen(src) + 1,
sizeof(
char))) == NULL)
811 return (
char *)ERROR_PTR(
"dest not made", __func__, NULL);
813 for (i = 0, k = 0; i < nsrc; i++) {
815 if (!strchr(remchars, ch))
859 if (pcount) *pcount = 0;
860 if (!src || !sub1 || !sub2)
861 return (
char *)ERROR_PTR(
"src, sub1, sub2 not all defined",
864 if (strlen(sub2) > 0) {
866 (
const l_uint8 *)src, strlen(src),
867 (
const l_uint8 *)sub1, strlen(sub1),
868 (
const l_uint8 *)sub2, strlen(sub2),
872 (
const l_uint8 *)src, strlen(src),
873 (
const l_uint8 *)sub1, strlen(sub1),
874 NULL, 0, &datalen, pcount);
915 l_int32 nsrc, nsub1, nsub2, len, npre, loc;
917 if (pfound) *pfound = 0;
918 if (!src || !sub1 || !sub2)
919 return (
char *)ERROR_PTR(
"src, sub1, sub2 not all defined",
926 if (!strcmp(sub1, sub2))
928 if ((ptr = strstr(src + loc, sub1)) == NULL)
930 if (pfound) *pfound = 1;
933 nsub1 = strlen(sub1);
934 nsub2 = strlen(sub2);
935 len = nsrc + nsub2 - nsub1;
936 if ((dest = (
char *)LEPT_CALLOC(len + 1,
sizeof(
char))) == NULL)
937 return (
char *)ERROR_PTR(
"dest not made", __func__, NULL);
939 memcpy(dest, src, npre);
940 strcpy(dest + npre, sub2);
941 strcpy(dest + npre + nsub2, ptr + nsub1);
942 if (ploc) *ploc = npre + nsub2;
970 return (
L_DNA *)ERROR_PTR(
"src, sub not both defined", __func__, NULL);
973 (
const l_uint8 *)sub, strlen(sub));
1001 if (ploc) *ploc = -1;
1003 return ERROR_INT(
"src and sub not both defined", __func__, 0);
1004 if (strlen(sub) == 0)
1005 return ERROR_INT(
"substring length 0", __func__, 0);
1006 if (strlen(src) == 0)
1009 if ((ptr = strstr(src, sub)) == NULL)
1052 const l_uint8 *newseq,
1059 l_int32 n, i, j, di, si, index, incr;
1062 if (pcount) *pcount = 0;
1064 return (l_uint8 *)ERROR_PTR(
"datas & seq not both defined",
1067 return (l_uint8 *)ERROR_PTR(
"&datadlen not defined", __func__, NULL);
1073 *pdatadlen = dataslen;
1079 if (pcount) *pcount = n;
1080 if (!newseq) newseqlen = 0;
1081 newsize = dataslen + n * (newseqlen - seqlen) + 4;
1082 if ((datad = (l_uint8 *)LEPT_CALLOC(newsize,
sizeof(l_uint8))) == NULL) {
1084 return (l_uint8 *)ERROR_PTR(
"datad not made", __func__, NULL);
1089 for (i = 0, di = 0, index = 0; i < dataslen; i++) {
1094 incr = L_MIN(seqlen, si - i);
1100 for (j = 0; j < newseqlen; j++)
1101 datad[di++] = newseq[j];
1104 datad[di++] = datas[i];
1139 const l_uint8 *sequence,
1142 l_int32 start, offset, realoffset, found;
1145 if (!data || !sequence)
1146 return (
L_DNA *)ERROR_PTR(
"data & sequence not both defined",
1157 realoffset = start + offset;
1159 start = realoffset + seqlen;
1160 if (start >= datalen)
1197 const l_uint8 *sequence,
1202 l_int32 i, j, found, lastpos;
1204 if (poffset) *poffset = 0;
1205 if (pfound) *pfound = FALSE;
1206 if (!data || !sequence)
1207 return ERROR_INT(
"data & sequence not both defined", __func__, 1);
1208 if (!poffset || !pfound)
1209 return ERROR_INT(
"&offset and &found not defined", __func__, 1);
1211 lastpos = datalen - seqlen + 1;
1213 for (i = 0; i < lastpos; i++) {
1214 for (j = 0; j < seqlen; j++) {
1215 if (data[i + j] != sequence[j])
1217 if (j == seqlen - 1)
1224 if (found == TRUE) {
1271 return ERROR_PTR(
"input data not defined", __func__, NULL);
1283 if ((newdata = (
void *)LEPT_CALLOC(1, newsize)) == NULL)
1284 return ERROR_PTR(
"newdata not made", __func__, NULL);
1289 if ((newdata = (
void *)LEPT_CALLOC(1, newsize)) == NULL)
1290 return ERROR_PTR(
"newdata not made", __func__, NULL);
1291 minsize = L_MIN(oldsize, newsize);
1292 memcpy(newdata, indata, minsize);
1317 return (l_uint8 *)ERROR_PTR(
"pnbytes not defined", __func__, NULL);
1320 return (l_uint8 *)ERROR_PTR(
"filename not defined", __func__, NULL);
1323 return (l_uint8 *)ERROR_PTR(
"file stream not opened", __func__, NULL);
1362 l_int32 seekable, navail, nadd, nread;
1366 return (l_uint8 *)ERROR_PTR(
"&nbytes not defined", __func__, NULL);
1369 return (l_uint8 *)ERROR_PTR(
"fp not defined", __func__, NULL);
1375 seekable = (ftell(fp) == 0) ? 1 : 0;
1384 if (navail < 4096) {
1385 nadd = L_MAX(bb->
nalloc, 4096);
1388 nread = fread((
void *)(bb->
array + bb->
n), 1, 4096, fp);
1390 if (nread != 4096)
break;
1395 if ((data = (l_uint8 *)LEPT_CALLOC(bb->
n + 1,
sizeof(l_uint8))) != NULL) {
1396 memcpy(data, bb->
array, bb->
n);
1399 L_ERROR(
"calloc fail for data\n", __func__);
1432 return (l_uint8 *)ERROR_PTR(
"pnread not defined", __func__, NULL);
1435 return (l_uint8 *)ERROR_PTR(
"filename not defined", __func__, NULL);
1438 return (l_uint8 *)ERROR_PTR(
"file stream not opened", __func__, NULL);
1472 size_t bytesleft, bytestoread, nread, filebytes;
1475 return (l_uint8 *)ERROR_PTR(
"&nread not defined", __func__, NULL);
1478 return (l_uint8 *)ERROR_PTR(
"stream not defined", __func__, NULL);
1481 fseek(fp, 0, SEEK_END);
1482 filebytes = ftell(fp);
1483 fseek(fp, 0, SEEK_SET);
1484 if (start > filebytes) {
1485 L_ERROR(
"start = %zu but filebytes = %zu\n", __func__,
1490 return (l_uint8 *)LEPT_CALLOC(1, 1);
1491 bytesleft = filebytes - start;
1492 if (nbytes == 0) nbytes = bytesleft;
1493 bytestoread = (bytesleft >= nbytes) ? nbytes : bytesleft;
1496 if ((data = (l_uint8 *)LEPT_CALLOC(1, bytestoread + 1)) == NULL)
1497 return (l_uint8 *)ERROR_PTR(
"calloc fail for data", __func__, NULL);
1498 fseek(fp, start, SEEK_SET);
1499 nread = fread(data, 1, bytestoread, fp);
1500 if (nbytes != nread)
1501 L_INFO(
"%zu bytes requested; %zu bytes read\n", __func__,
1504 fseek(fp, 0, SEEK_SET);
1520 const char *operation,
1524 char actualOperation[20];
1528 return ERROR_INT(
"filename not defined", __func__, 1);
1530 return ERROR_INT(
"operation not defined", __func__, 1);
1532 return ERROR_INT(
"data not defined", __func__, 1);
1534 return ERROR_INT(
"nbytes must be > 0", __func__, 1);
1536 if (strcmp(operation,
"w") && strcmp(operation,
"a"))
1537 return ERROR_INT(
"operation not one of {'w','a'}", __func__, 1);
1545 return ERROR_INT(
"stream not opened", __func__, 1);
1546 fwrite(data, 1, nbytes, fp);
1565 return ERROR_INT(
"filename not defined", __func__, 0);
1567 return ERROR_INT(
"stream not opened", __func__, 0);
1583 l_int64 pos, nbytes;
1586 return ERROR_INT(
"stream not open", __func__, 0);
1590 return ERROR_INT(
"seek position must be > 0", __func__, 0);
1591 fseek(fp, 0, SEEK_END);
1594 return ERROR_INT(
"nbytes is < 0", __func__, 0);
1595 fseek(fp, pos, SEEK_SET);
1625 return (l_uint8 *)ERROR_PTR(
"datas not defined", __func__, NULL);
1627 if ((datad = (l_uint8 *)LEPT_CALLOC(size + 4,
sizeof(l_uint8))) == NULL)
1628 return (l_uint8 *)ERROR_PTR(
"datad not made", __func__, NULL);
1629 memcpy(datad, datas, size);
1655 const l_uint8 *data2,
1662 return ERROR_INT(
"&same not defined", __func__, 1);
1664 if (!data1 || !data2)
1665 return ERROR_INT(
"data1 and data2 not both defined", __func__, 1);
1666 if (size1 != size2)
return 0;
1667 for (i = 0; i < size1; i++) {
1668 if (data1[i] != data2[i])
1688 const char *newfile)
1695 return ERROR_INT(
"srcfile not defined", __func__, 1);
1697 return ERROR_INT(
"newfile not defined", __func__, 1);
1700 return ERROR_INT(
"data not returned", __func__, 1);
1716 const char *destfile)
1722 return ERROR_INT(
"srcfile not defined", __func__, 1);
1724 return ERROR_INT(
"destfile not defined", __func__, 1);
1747 return ERROR_INT(
"filename not defined", __func__, 1);
1749 return ERROR_INT(
"str not defined", __func__, 1);
1752 return ERROR_INT(
"stream not opened", __func__, 1);
1753 fprintf(fp,
"%s", str);
1792 const char *rootpath,
1795 l_int32 i, totlines, nlines, index;
1804 return ERROR_INT(
"filename not defined", __func__, 1);
1806 return ERROR_INT(
"rootpath not defined", __func__, 1);
1808 return ERROR_INT(
"n must be > 0", __func__, 1);
1809 if (save_empty != 0 && save_empty != 1)
1810 return ERROR_INT(
"save_empty not 0 or 1", __func__, 1);
1814 return ERROR_INT(
"data not read", __func__, 1);
1818 return ERROR_INT(
"sa not made", __func__, 1);
1822 L_ERROR(
"num files = %d > num lines = %d\n", __func__, n, totlines);
1829 for (i = 0; i < n; i++) {
1831 snprintf(outname,
sizeof(outname),
"%s_%d", rootpath, i);
1833 snprintf(outname,
sizeof(outname),
"%s_%d%s", rootpath, i, ext);
1870 return (FILE *)ERROR_PTR(
"filename not defined", __func__, NULL);
1874 fp = fopen(fname,
"rb");
1880 fp = fopen(tail,
"rb");
1884 return (FILE *)ERROR_PTR(
"file not found", __func__, NULL);
1906 const char *modestring)
1912 return (FILE *)ERROR_PTR(
"filename not defined", __func__, NULL);
1915 fp = fopen(fname, modestring);
1918 return (FILE *)ERROR_PTR(
"stream not opened", __func__, NULL);
1943 return (FILE *)ERROR_PTR(
"data not defined", __func__, NULL);
1946 if ((fp = fmemopen((
void *)data, size,
"rb")) == NULL)
1947 return (FILE *)ERROR_PTR(
"stream not opened", __func__, NULL);
1949 L_INFO(
"work-around: writing to a temp file\n", __func__);
1952 return (FILE *)ERROR_PTR(
"tmpfile stream not opened", __func__, NULL);
1954 if ((fp = tmpfile()) == NULL)
1955 return (FILE *)ERROR_PTR(
"tmpfile stream not opened", __func__, NULL);
1957 fwrite(data, 1, size, fp);
1989 L_ERROR(
"l_makeTempFilename failed, %s\n", __func__, strerror(errno));
1993 handle = _open(filename, _O_CREAT | _O_RDWR | _O_SHORT_LIVED |
1994 _O_TEMPORARY | _O_BINARY, _S_IREAD | _S_IWRITE);
1997 L_ERROR(
"_open failed, %s\n", __func__, strerror(errno));
2001 if ((fp = _fdopen(handle,
"r+b")) == NULL) {
2002 L_ERROR(
"_fdopen failed, %s\n", __func__, strerror(errno));
2041 return (FILE *)ERROR_PTR(
"filename not defined", __func__, NULL);
2043 return (FILE *)ERROR_PTR(
"mode not defined", __func__, NULL);
2068 return ERROR_INT(
"stream not defined", __func__, 1);
2092 if (nmemb <= 0 || size <= 0)
2094 return LEPT_CALLOC(nmemb, size);
2145 l_uint32 attributes;
2149 L_INFO(
"making named temp subdirectory %s is disabled\n",
2155 return ERROR_INT(
"subdir not defined", __func__, 1);
2156 if ((strlen(subdir) == 0) || (subdir[0] ==
'.') || (subdir[0] ==
'/'))
2157 return ERROR_INT(
"subdir not an actual subdirectory", __func__, 1);
2160 sarraySplitString(sa, subdir,
"/");
2165 ret = mkdir(dir, 0777);
2167 attributes = GetFileAttributes(dir);
2168 if (attributes == INVALID_FILE_ATTRIBUTES)
2169 ret = (CreateDirectory(dir, NULL) ? 0 : 1);
2172 for (i = 0; i < n; i++) {
2175 ret += mkdir(tmpdir, 0777);
2177 if (CreateDirectory(tmpdir, NULL) == 0)
2178 ret += (GetLastError () != ERROR_ALREADY_EXISTS);
2186 L_ERROR(
"failure to create %d directories\n", __func__, ret);
2215 char *dir, *realdir, *fname, *fullname;
2216 l_int32 exists, ret, i, nfiles;
2223 return ERROR_INT(
"subdir not defined", __func__, 1);
2224 if ((strlen(subdir) == 0) || (subdir[0] ==
'.') || (subdir[0] ==
'/'))
2225 return ERROR_INT(
"subdir not an actual subdirectory", __func__, 1);
2230 return ERROR_INT(
"directory name not made", __func__, 1);
2239 L_ERROR(
"directory %s does not exist!\n", __func__, dir);
2245 for (i = 0; i < nfiles; i++) {
2249 LEPT_FREE(fullname);
2254 ret = rmdir(realdir);
2258 ret = (RemoveDirectory(newpath) ? 0 : 1);
2290 if (!pexists)
return;
2299 l_int32 err = stat(realdir, &s);
2300 if (err != -1 && S_ISDIR(s.st_mode))
2305 l_uint32 attributes;
2306 attributes = GetFileAttributes(realdir);
2307 if (attributes != INVALID_FILE_ATTRIBUTES &&
2308 (attributes & FILE_ATTRIBUTE_DIRECTORY))
2353 return ERROR_INT(
"sa not made", __func__, -1);
2356 L_WARNING(
"no matching files found\n", __func__);
2362 for (i = 0; i < n; i++) {
2366 L_ERROR(
"failed to remove %s\n", __func__, path);
2398 if (!tail || strlen(tail) == 0)
2399 return ERROR_INT(
"tail undefined or empty", __func__, 1);
2402 return ERROR_INT(
"temp dirname not made", __func__, 1);
2433 if (!filepath || strlen(filepath) == 0)
2434 return ERROR_INT(
"filepath undefined or empty", __func__, 1);
2437 ret = remove(filepath);
2440 SetFileAttributes(filepath, FILE_ATTRIBUTE_NORMAL);
2441 ret = DeleteFile(filepath) ? 0 : 1;
2484 const char *newtail,
2487 char *srcpath, *newpath, *dir, *srctail;
2492 return ERROR_INT(
"srcfile not defined", __func__, 1);
2496 return ERROR_INT(
"newdir not NULL or a subdir of /tmp", __func__, 1);
2506 if (!newtail || newtail[0] ==
'\0')
2507 newpath =
pathJoin(newtemp, srctail);
2509 newpath =
pathJoin(newtemp, newtail);
2517 LEPT_FREE(realpath);
2524 if (!newtail || newtail[0] ==
'\0')
2531 ret = MoveFileEx(srcpath, newpath,
2532 MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING) ? 0 : 1;
2537 *pnewpath = newpath;
2581 const char *newtail,
2584 char *srcpath, *newpath, *dir, *srctail;
2589 return ERROR_INT(
"srcfile not defined", __func__, 1);
2593 return ERROR_INT(
"newdir not NULL or a subdir of /tmp", __func__, 1);
2603 if (!newtail || newtail[0] ==
'\0')
2604 newpath =
pathJoin(newtemp, srctail);
2606 newpath =
pathJoin(newtemp, newtail);
2616 if (!newtail || newtail[0] ==
'\0')
2623 ret = CopyFile(srcpath, newpath, FALSE) ? 0 : 1;
2628 *pnewpath = newpath;
2638 #if defined(__APPLE__)
2639 #include "TargetConditionals.h"
2663 L_ERROR(
"cmd not defined\n", __func__);
2666 if (LeptDebugOK == FALSE) {
2667 L_INFO(
"'system' calls are disabled\n", __func__);
2671 #if defined(__APPLE__)
2673 #if (defined(TARGET_OS_OSX) && TARGET_OS_OSX == 1)
2675 #elif TARGET_OS_IPHONE || defined(OS_IOS)
2676 L_ERROR(
"iOS 11 does not support system()\n", __func__);
2732 char *cpathname, *lastslash;
2734 if (!pdir && !ptail)
2735 return ERROR_INT(
"null input for both strings", __func__, 1);
2736 if (pdir) *pdir = NULL;
2737 if (ptail) *ptail = NULL;
2739 return ERROR_INT(
"pathname not defined", __func__, 1);
2743 lastslash = strrchr(cpathname,
'/');
2748 *(lastslash + 1) =
'\0';
2751 LEPT_FREE(cpathname);
2759 LEPT_FREE(cpathname);
2800 char *tail, *dir, *lastdot;
2803 if (!pbasename && !pextension)
2804 return ERROR_INT(
"null input for both strings", __func__, 1);
2805 if (pbasename) *pbasename = NULL;
2806 if (pextension) *pextension = NULL;
2808 return ERROR_INT(
"pathname not defined", __func__, 1);
2815 if ((lastdot = strrchr(tail,
'.'))) {
2876 const char *slash =
"/";
2878 l_int32 i, n1, n2, emptydir;
2885 if (dir && strlen(dir) >= 2 && dir[0] ==
'.' && dir[1] ==
'.')
2886 return (
char *)ERROR_PTR(
"dir starts with '..'", __func__, NULL);
2887 if (fname && strlen(fname) >= 2 && fname[0] ==
'.' && fname[1] ==
'.')
2888 return (
char *)ERROR_PTR(
"fname starts with '..'", __func__, NULL);
2895 if (dir && strlen(dir) > 0) {
2898 sarraySplitString(sa1, dir,
"/");
2900 for (i = 0; i < n1; i++) {
2908 emptydir = dir && strlen(dir) == 0;
2909 if ((!dir || emptydir) && fname && strlen(fname) > 0 && fname[0] ==
'/')
2913 if (fname && strlen(fname) > 0) {
2914 sarraySplitString(sa2, fname,
"/");
2916 for (i = 0; i < n2; i++) {
2925 if (size > 1 && dest[size - 1] ==
'/')
2926 dest[size - 1] =
'\0';
2951 const char *subdirs)
2954 size_t len1, len2, len3, len4;
2956 if (!basedir || !subdirs)
2957 return (
char *)ERROR_PTR(
"basedir and subdirs not both defined",
2960 len1 = strlen(basedir);
2961 len2 = strlen(subdirs);
2962 len3 = len1 + len2 + 8;
2963 if ((newdir = (
char *)LEPT_CALLOC(len3, 1)) == NULL)
2964 return (
char *)ERROR_PTR(
"newdir not made", __func__, NULL);
2966 if (newdir[len1 - 1] !=
'/')
2968 if (subdirs[0] ==
'/')
2972 len4 = strlen(newdir);
2973 if (newdir[len4 - 1] ==
'/')
2974 newdir[len4 - 1] =
'\0';
3011 return ERROR_INT(
"path not defined", __func__, 1);
3012 if (type != UNIX_PATH_SEPCHAR && type != WIN_PATH_SEPCHAR)
3013 return ERROR_INT(
"invalid type", __func__, 1);
3016 if (type == UNIX_PATH_SEPCHAR) {
3018 for (i = 0; i < len; i++) {
3019 if (path[i] ==
'\\')
3024 for (i = 0; i < len; i++) {
3071 l_int32 is_win32 = FALSE;
3072 char *cdir, *pathout;
3073 l_int32 dirlen, namelen;
3077 return (
char *)ERROR_PTR(
"no input", __func__, NULL);
3080 if (!dir || dir[0] ==
'\0') {
3081 if ((cdir = getcwd(NULL, 0)) == NULL)
3082 return (
char *)ERROR_PTR(
"no current dir found", __func__, NULL);
3090 dirlen = strlen(cdir);
3091 if (cdir[dirlen - 1] ==
'/' && dirlen != 1) {
3092 cdir[dirlen - 1] =
'\0';
3096 namelen = (fname) ? strlen(fname) : 0;
3097 size = dirlen + namelen + 256;
3098 if ((pathout = (
char *)LEPT_CALLOC(size,
sizeof(
char))) == NULL) {
3100 return (
char *)ERROR_PTR(
"pathout not made", __func__, NULL);
3111 if (!is_win32 || dirlen < 4 ||
3112 (dirlen == 4 && strncmp(cdir,
"/tmp", 4) != 0) ||
3113 (dirlen > 4 && strncmp(cdir,
"/tmp/", 5) != 0)) {
3118 char tmpdir[MAX_PATH];
3119 GetTempPath(
sizeof(tmpdir), tmpdir);
3120 tmpdirlen = strlen(tmpdir);
3121 if (tmpdirlen > 0 && tmpdir[tmpdirlen - 1] ==
'\\') {
3122 tmpdir[tmpdirlen - 1] =
'\0';
3124 tmpdirlen = strlen(tmpdir);
3134 if (fname && strlen(fname) > 0) {
3135 dirlen = strlen(pathout);
3136 pathout[dirlen] =
'/';
3184 return ERROR_INT(
"result not defined", __func__, 1);
3185 if (subdir && ((subdir[0] ==
'.') || (subdir[0] ==
'/')))
3186 return ERROR_INT(
"subdir not an actual subdirectory", __func__, 1);
3188 memset(result, 0, nbytes);
3192 size_t n = confstr(_CS_DARWIN_USER_TEMP_DIR, result, nbytes);
3194 L_ERROR(
"failed to find tmp dir, %s\n", __func__, strerror(errno));
3196 }
else if (n > nbytes) {
3197 return ERROR_INT(
"result array too small for path\n", __func__, 1);
3210 pathlen = strlen(path);
3211 if (pathlen < nbytes - 1) {
3214 L_ERROR(
"result array too small for path\n", __func__);
3246 return ERROR_INT(
"path not defined", __func__, 1);
3248 return ERROR_INT(
"invalid flag", __func__, 1);
3251 lastchar = path[len - 1];
3254 path[len + 1] =
'\0';
3256 path[len - 1] =
'\0';
3291 return (
char *)ERROR_PTR(
"failed to make dirname", __func__, NULL);
3298 fd = mkstemp(pattern);
3301 return (
char *)ERROR_PTR(
"mkstemp failed", __func__, NULL);
3308 char fname[MAX_PATH];
3310 if (GetTempFileName(dirname,
"lp.", 0, fname) == 0)
3311 return (
char *)ERROR_PTR(
"GetTempFileName failed", __func__, NULL);
3312 if ((fp = fopen(fname,
"wb")) == NULL)
3313 return (
char *)ERROR_PTR(
"file cannot be written to", __func__, NULL);
3344 char *tail, *basename;
3345 l_int32 len, nret, num;
3348 return ERROR_INT(
"fname not defined", __func__, -1);
3354 len = strlen(basename);
3355 if (numpre + numpost > len - 1) {
3356 LEPT_FREE(basename);
3357 return ERROR_INT(
"numpre + numpost too big", __func__, -1);
3360 basename[len - numpost] =
'\0';
3361 nret = sscanf(basename + numpre,
"%d", &num);
3362 LEPT_FREE(basename);
L_BBUFFER * bbufferCreate(const l_uint8 *indata, l_int32 nalloc)
bbufferCreate()
l_ok bbufferExtendArray(L_BBUFFER *bb, l_int32 nbytes)
bbufferExtendArray()
void bbufferDestroy(L_BBUFFER **pbb)
bbufferDestroy()
l_uint8 * l_byteaCopyData(L_BYTEA *ba, size_t *psize)
l_byteaCopyData()
L_BYTEA * l_byteaCreate(size_t nbytes)
l_byteaCreate()
void l_byteaDestroy(L_BYTEA **pba)
l_byteaDestroy()
l_ok l_byteaAppendString(L_BYTEA *ba, const char *str)
l_byteaAppendString()
L_DNA * l_dnaCreate(l_int32 n)
l_dnaCreate()
l_ok l_dnaGetIValue(L_DNA *da, l_int32 index, l_int32 *pival)
l_dnaGetIValue()
l_ok l_dnaAddNumber(L_DNA *da, l_float64 val)
l_dnaAddNumber()
void l_dnaDestroy(L_DNA **pda)
l_dnaDestroy()
l_int32 l_dnaGetCount(L_DNA *da)
l_dnaGetCount()
void numaDestroy(NUMA **pna)
numaDestroy()
l_ok numaGetIValue(NUMA *na, l_int32 index, l_int32 *pival)
numaGetIValue()
NUMA * numaGetUniformBinSizes(l_int32 ntotal, l_int32 nbins)
numaGetUniformBinSizes()
SARRAY * sarrayCreate(l_int32 n)
sarrayCreate()
char * sarrayGetString(SARRAY *sa, l_int32 index, l_int32 copyflag)
sarrayGetString()
char * sarrayToStringRange(SARRAY *sa, l_int32 first, l_int32 nstrings, l_int32 addnlflag)
sarrayToStringRange()
SARRAY * getFilenamesInDirectory(const char *dirname)
getFilenamesInDirectory()
l_int32 sarrayGetCount(SARRAY *sa)
sarrayGetCount()
void sarrayDestroy(SARRAY **psa)
sarrayDestroy()
SARRAY * sarrayCreateLinesFromString(const char *string, l_int32 blankflag)
sarrayCreateLinesFromString()
SARRAY * getSortedPathnamesInDirectory(const char *dirname, const char *substr, l_int32 first, l_int32 nfiles)
getSortedPathnamesInDirectory()
char * stringReplaceSubstr(const char *src, const char *sub1, const char *sub2, l_int32 *ploc, l_int32 *pfound)
stringReplaceSubstr()
l_int32 stringLength(const char *src, size_t size)
stringLength()
size_t nbytesInFile(const char *filename)
nbytesInFile()
char * stringNew(const char *src)
stringNew()
l_int32 lept_rmdir(const char *subdir)
lept_rmdir()
l_uint8 * l_binaryReadStream(FILE *fp, size_t *pnbytes)
l_binaryReadStream()
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()
l_uint8 * l_binaryReadSelectStream(FILE *fp, size_t start, size_t nbytes, size_t *pnread)
l_binaryReadSelectStream()
l_int32 lept_rmfile(const char *filepath)
lept_rmfile()
char * strtokSafe(char *cstr, const char *seps, char **psaveptr)
strtokSafe()
l_ok fileAppendString(const char *filename, const char *str)
fileAppendString()
char * l_makeTempFilename(void)
l_makeTempFilename()
l_int32 lept_rm_match(const char *subdir, const char *substr)
lept_rm_match()
l_ok stringReplace(char **pdest, const char *src)
stringReplace()
l_ok fileConcatenate(const char *srcfile, const char *destfile)
fileConcatenate()
l_ok stringJoinIP(char **psrc1, const char *src2)
stringJoinIP()
void lept_free(void *ptr)
lept_free()
l_ok modifyTrailingSlash(char *path, size_t nbytes, l_int32 flag)
modifyTrailingSlash()
l_ok splitPathAtExtension(const char *pathname, char **pbasename, char **pextension)
splitPathAtExtension()
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()
char * stringReplaceEachSubstr(const char *src, const char *sub1, const char *sub2, l_int32 *pcount)
stringReplaceEachSubstr()
FILE * fopenReadFromMemory(const l_uint8 *data, size_t size)
fopenReadFromMemory()
l_uint8 * l_binaryReadSelect(const char *filename, size_t start, size_t nbytes, size_t *pnread)
l_binaryReadSelect()
char * appendSubdirs(const char *basedir, const char *subdirs)
appendSubdirs()
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()
FILE * lept_fopen(const char *filename, const char *mode)
lept_fopen()
FILE * fopenWriteStream(const char *filename, const char *modestring)
fopenWriteStream()
l_int32 stringFindSubstr(const char *src, const char *sub, l_int32 *ploc)
stringFindSubstr()
char * pathJoin(const char *dir, const char *fname)
pathJoin()
char * stringCopySegment(const char *src, l_int32 start, l_int32 nbytes)
stringCopySegment()
char * genPathname(const char *dir, const char *fname)
genPathname()
l_ok convertSepCharsInPath(char *path, l_int32 type)
convertSepCharsInPath()
l_int32 stringCat(char *dest, size_t size, const char *src)
stringCat()
void * lept_calloc(size_t nmemb, size_t size)
lept_calloc()
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_int32 lept_mkdir(const char *subdir)
lept_mkdir()
char * stringJoin(const char *src1, const char *src2)
stringJoin()
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()
void * reallocNew(void **pindata, size_t oldsize, size_t newsize)
reallocNew()
char * stringConcatNew(const char *first,...)
stringConcatNew()
l_ok stringSplitOnToken(char *cstr, const char *seps, char **phead, char **ptail)
stringSplitOnToken()
FILE * fopenWriteWinTempfile(void)
fopenWriteWinTempfile()
L_DNA * stringFindEachSubstr(const char *src, const char *sub)
stringFindEachSubstr()
FILE * fopenReadStream(const char *filename)
fopenReadStream()
l_uint8 * l_binaryCopy(const l_uint8 *datas, size_t size)
l_binaryCopy()
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_uint8 * l_binaryRead(const char *filename, size_t *pnbytes)
l_binaryRead()
char * stringReverse(const char *src)
stringReverse()
l_int32 extractNumberFromFilename(const char *fname, l_int32 numpre, l_int32 numpost)
extractNumberFromFilename()
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()
char * stringRemoveChars(const char *src, const char *remchars)
stringRemoveChars()