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" 228 PROCNAME(
"stringNew");
231 L_WARNING(
"src not defined\n", procName);
236 if ((dest = (
char *)LEPT_CALLOC(len + 1,
sizeof(
char))) == NULL)
237 return (
char *)ERROR_PTR(
"dest not made", procName, NULL);
269 PROCNAME(
"stringCopy");
272 return ERROR_INT(
"dest not defined", procName, 1);
277 for (i = 0; i < n && src[i] !=
'\0'; i++)
312 PROCNAME(
"stringCopySegment");
315 return (
char *)ERROR_PTR(
"src not defined", procName, NULL);
317 if (start < 0 || start > len - 1)
318 return (
char *)ERROR_PTR(
"invalid start", procName, NULL);
320 nbytes = len - start;
321 if (start + nbytes > len)
322 nbytes = len - start;
323 if ((dest = (
char *)LEPT_CALLOC(nbytes + 1,
sizeof(
char))) == NULL)
324 return (
char *)ERROR_PTR(
"dest not made", procName, NULL);
348 PROCNAME(
"stringReplace");
351 return ERROR_INT(
"pdest not defined", procName, 1);
386 PROCNAME(
"stringLength");
389 return ERROR_INT(
"src not defined", procName, 0);
393 for (i = 0; i < size; i++) {
428 l_int32 lendest, lensrc;
430 PROCNAME(
"stringCat");
433 return ERROR_INT(
"dest not defined", procName, -1);
435 return ERROR_INT(
"size < 1; too small", procName, -1);
441 return ERROR_INT(
"no terminating nul byte", procName, -1);
445 n = (lendest + lensrc > size - 1 ? 0 : lensrc);
447 return ERROR_INT(
"dest too small for append", procName, -1);
449 for (i = 0; i < n; i++)
450 dest[lendest + i] = src[i];
451 dest[lendest + n] =
'\0';
478 if (!first)
return NULL;
481 va_start(args, first);
483 while ((arg = va_arg(args,
const char *)) != NULL)
486 result = (
char *)LEPT_CALLOC(len + 1,
sizeof(
char));
489 va_start(args, first);
494 while ((arg = va_arg(args,
const char *)) != NULL) {
522 l_int32 srclen1, srclen2, destlen;
524 PROCNAME(
"stringJoin");
526 srclen1 = (src1) ? strlen(src1) : 0;
527 srclen2 = (src2) ? strlen(src2) : 0;
528 destlen = srclen1 + srclen2 + 3;
530 if ((dest = (
char *)LEPT_CALLOC(destlen,
sizeof(
char))) == NULL)
531 return (
char *)ERROR_PTR(
"calloc fail for dest", procName, NULL);
578 PROCNAME(
"stringJoinIP");
581 return ERROR_INT(
"&src1 not defined", procName, 1);
602 PROCNAME(
"stringReverse");
605 return (
char *)ERROR_PTR(
"src not defined", procName, NULL);
607 if ((dest = (
char *)LEPT_CALLOC(len + 1,
sizeof(
char))) == NULL)
608 return (
char *)ERROR_PTR(
"calloc fail for dest", procName, NULL);
609 for (i = 0; i < len; i++)
610 dest[i] = src[len - 1 - i];
654 char *start, *substr;
655 l_int32 istart, i, j, nchars;
657 PROCNAME(
"strtokSafe");
660 return (
char *)ERROR_PTR(
"seps not defined", procName, NULL);
662 return (
char *)ERROR_PTR(
"&saveptr not defined", procName, NULL);
676 for (istart = 0;; istart++) {
677 if ((nextc = start[istart]) ==
'\0') {
681 if (!strchr(seps, nextc))
688 for (i = istart;; i++) {
689 if ((nextc = start[i]) ==
'\0')
691 if (strchr(seps, nextc))
697 substr = (
char *)LEPT_CALLOC(nchars + 1,
sizeof(
char));
703 if ((nextc = start[j]) ==
'\0') {
707 if (!strchr(seps, nextc)) {
708 *psaveptr = start + j;
750 PROCNAME(
"stringSplitOnToken");
753 return ERROR_INT(
"&head not defined", procName, 1);
755 return ERROR_INT(
"&tail not defined", procName, 1);
756 *phead = *ptail = NULL;
758 return ERROR_INT(
"cstr not defined", procName, 1);
760 return ERROR_INT(
"seps not defined", procName, 1);
794 PROCNAME(
"stringCheckForChars");
797 return ERROR_INT(
"&found not defined", procName, 1);
800 return ERROR_INT(
"src and chars not both defined", procName, 1);
803 for (i = 0; i < n; i++) {
805 if (strchr(chars, ch)) {
823 const char *remchars)
829 PROCNAME(
"stringRemoveChars");
832 return (
char *)ERROR_PTR(
"src not defined", procName, NULL);
836 if ((dest = (
char *)LEPT_CALLOC(strlen(src) + 1,
sizeof(
char))) == NULL)
837 return (
char *)ERROR_PTR(
"dest not made", procName, NULL);
839 for (i = 0, k = 0; i < nsrc; i++) {
841 if (!strchr(remchars, ch))
885 PROCNAME(
"stringReplaceEachSubstr");
887 if (pcount) *pcount = 0;
888 if (!src || !sub1 || !sub2)
889 return (
char *)ERROR_PTR(
"src, sub1, sub2 not all defined",
892 if (strlen(sub2) > 0) {
894 (
const l_uint8 *)src, strlen(src),
895 (
const l_uint8 *)sub1, strlen(sub1),
896 (
const l_uint8 *)sub2, strlen(sub2),
900 (
const l_uint8 *)src, strlen(src),
901 (
const l_uint8 *)sub1, strlen(sub1),
902 NULL, 0, &datalen, pcount);
943 l_int32 nsrc, nsub1, nsub2, len, npre, loc;
945 PROCNAME(
"stringReplaceSubstr");
947 if (pfound) *pfound = 0;
948 if (!src || !sub1 || !sub2)
949 return (
char *)ERROR_PTR(
"src, sub1, sub2 not all defined",
956 if (!strcmp(sub1, sub2))
958 if ((ptr = strstr(src + loc, sub1)) == NULL)
960 if (pfound) *pfound = 1;
963 nsub1 = strlen(sub1);
964 nsub2 = strlen(sub2);
965 len = nsrc + nsub2 - nsub1;
966 if ((dest = (
char *)LEPT_CALLOC(len + 1,
sizeof(
char))) == NULL)
967 return (
char *)ERROR_PTR(
"dest not made", procName, NULL);
969 memcpy(dest, src, npre);
970 strcpy(dest + npre, sub2);
971 strcpy(dest + npre + nsub2, ptr + nsub1);
972 if (ploc) *ploc = npre + nsub2;
999 PROCNAME(
"stringFindEachSubstr");
1002 return (
L_DNA *)ERROR_PTR(
"src, sub not both defined", procName, NULL);
1005 (
const l_uint8 *)sub, strlen(sub));
1033 PROCNAME(
"stringFindSubstr");
1035 if (ploc) *ploc = -1;
1037 return ERROR_INT(
"src and sub not both defined", procName, 0);
1038 if (strlen(sub) == 0)
1039 return ERROR_INT(
"substring length 0", procName, 0);
1040 if (strlen(src) == 0)
1043 if ((ptr = strstr(src, sub)) == NULL)
1086 const l_uint8 *newseq,
1093 l_int32 n, i, j, di, si, index, incr;
1096 PROCNAME(
"arrayReplaceEachSequence");
1098 if (pcount) *pcount = 0;
1100 return (l_uint8 *)ERROR_PTR(
"datas & seq not both defined",
1103 return (l_uint8 *)ERROR_PTR(
"&datadlen not defined", procName, NULL);
1109 *pdatadlen = dataslen;
1115 if (pcount) *pcount = n;
1116 if (!newseq) newseqlen = 0;
1117 newsize = dataslen + n * (newseqlen - seqlen) + 4;
1118 if ((datad = (l_uint8 *)LEPT_CALLOC(newsize,
sizeof(l_uint8))) == NULL) {
1120 return (l_uint8 *)ERROR_PTR(
"datad not made", procName, NULL);
1125 for (i = 0, di = 0, index = 0; i < dataslen; i++) {
1130 incr = L_MIN(seqlen, si - i);
1136 for (j = 0; j < newseqlen; j++)
1137 datad[di++] = newseq[j];
1140 datad[di++] = datas[i];
1175 const l_uint8 *sequence,
1178 l_int32 start, offset, realoffset, found;
1181 PROCNAME(
"arrayFindEachSequence");
1183 if (!data || !sequence)
1184 return (
L_DNA *)ERROR_PTR(
"data & sequence not both defined",
1195 realoffset = start + offset;
1197 start = realoffset + seqlen;
1198 if (start >= datalen)
1235 const l_uint8 *sequence,
1240 l_int32 i, j, found, lastpos;
1242 PROCNAME(
"arrayFindSequence");
1244 if (poffset) *poffset = 0;
1245 if (pfound) *pfound = FALSE;
1246 if (!data || !sequence)
1247 return ERROR_INT(
"data & sequence not both defined", procName, 1);
1248 if (!poffset || !pfound)
1249 return ERROR_INT(
"&offset and &found not defined", procName, 1);
1251 lastpos = datalen - seqlen + 1;
1253 for (i = 0; i < lastpos; i++) {
1254 for (j = 0; j < seqlen; j++) {
1255 if (data[i + j] != sequence[j])
1257 if (j == seqlen - 1)
1264 if (found == TRUE) {
1310 PROCNAME(
"reallocNew");
1313 return ERROR_PTR(
"input data not defined", procName, NULL);
1325 if ((newdata = (
void *)LEPT_CALLOC(1, newsize)) == NULL)
1326 return ERROR_PTR(
"newdata not made", procName, NULL);
1331 if ((newdata = (
void *)LEPT_CALLOC(1, newsize)) == NULL)
1332 return ERROR_PTR(
"newdata not made", procName, NULL);
1333 minsize = L_MIN(oldsize, newsize);
1334 memcpy(newdata, indata, minsize);
1358 PROCNAME(
"l_binaryRead");
1361 return (l_uint8 *)ERROR_PTR(
"pnbytes not defined", procName, NULL);
1364 return (l_uint8 *)ERROR_PTR(
"filename not defined", procName, NULL);
1367 return (l_uint8 *)ERROR_PTR(
"file stream not opened", procName, NULL);
1406 l_int32 seekable, navail, nadd, nread;
1409 PROCNAME(
"l_binaryReadStream");
1412 return (l_uint8 *)ERROR_PTR(
"&nbytes not defined", procName, NULL);
1415 return (l_uint8 *)ERROR_PTR(
"fp not defined", procName, NULL);
1421 seekable = (ftell(fp) == 0) ? 1 : 0;
1430 if (navail < 4096) {
1431 nadd = L_MAX(bb->
nalloc, 4096);
1434 nread = fread((
void *)(bb->
array + bb->
n), 1, 4096, fp);
1436 if (nread != 4096)
break;
1441 if ((data = (l_uint8 *)LEPT_CALLOC(bb->
n + 1,
sizeof(l_uint8))) != NULL) {
1442 memcpy(data, bb->
array, bb->
n);
1445 L_ERROR(
"calloc fail for data\n", procName);
1477 PROCNAME(
"l_binaryReadSelect");
1480 return (l_uint8 *)ERROR_PTR(
"pnread not defined", procName, NULL);
1483 return (l_uint8 *)ERROR_PTR(
"filename not defined", procName, NULL);
1486 return (l_uint8 *)ERROR_PTR(
"file stream not opened", procName, NULL);
1520 size_t bytesleft, bytestoread, nread, filebytes;
1522 PROCNAME(
"l_binaryReadSelectStream");
1525 return (l_uint8 *)ERROR_PTR(
"&nread not defined", procName, NULL);
1528 return (l_uint8 *)ERROR_PTR(
"stream not defined", procName, NULL);
1531 fseek(fp, 0, SEEK_END);
1532 filebytes = ftell(fp);
1533 fseek(fp, 0, SEEK_SET);
1534 if (start > filebytes) {
1535 L_ERROR(
"start = %zu but filebytes = %zu\n", procName,
1540 return (l_uint8 *)LEPT_CALLOC(1, 1);
1541 bytesleft = filebytes - start;
1542 if (nbytes == 0) nbytes = bytesleft;
1543 bytestoread = (bytesleft >= nbytes) ? nbytes : bytesleft;
1546 if ((data = (l_uint8 *)LEPT_CALLOC(1, bytestoread + 1)) == NULL)
1547 return (l_uint8 *)ERROR_PTR(
"calloc fail for data", procName, NULL);
1548 fseek(fp, start, SEEK_SET);
1549 nread = fread(data, 1, bytestoread, fp);
1550 if (nbytes != nread)
1551 L_INFO(
"%zu bytes requested; %zu bytes read\n", procName,
1554 fseek(fp, 0, SEEK_SET);
1570 const char *operation,
1574 char actualOperation[20];
1577 PROCNAME(
"l_binaryWrite");
1580 return ERROR_INT(
"filename not defined", procName, 1);
1582 return ERROR_INT(
"operation not defined", procName, 1);
1584 return ERROR_INT(
"data not defined", procName, 1);
1586 return ERROR_INT(
"nbytes must be > 0", procName, 1);
1588 if (strcmp(operation,
"w") && strcmp(operation,
"a"))
1589 return ERROR_INT(
"operation not one of {'w','a'}", procName, 1);
1597 return ERROR_INT(
"stream not opened", procName, 1);
1598 fwrite(data, 1, nbytes, fp);
1616 PROCNAME(
"nbytesInFile");
1619 return ERROR_INT(
"filename not defined", procName, 0);
1621 return ERROR_INT(
"stream not opened", procName, 0);
1637 l_int64 pos, nbytes;
1639 PROCNAME(
"fnbytesInFile");
1642 return ERROR_INT(
"stream not open", procName, 0);
1646 return ERROR_INT(
"seek position must be > 0", procName, 0);
1647 fseek(fp, 0, SEEK_END);
1650 return ERROR_INT(
"nbytes is < 0", procName, 0);
1651 fseek(fp, pos, SEEK_SET);
1680 PROCNAME(
"l_binaryCopy");
1683 return (l_uint8 *)ERROR_PTR(
"datas not defined", procName, NULL);
1685 if ((datad = (l_uint8 *)LEPT_CALLOC(size + 4,
sizeof(l_uint8))) == NULL)
1686 return (l_uint8 *)ERROR_PTR(
"datad not made", procName, NULL);
1687 memcpy(datad, datas, size);
1713 const l_uint8 *data2,
1719 PROCNAME(
"l_binaryCompare");
1722 return ERROR_INT(
"&same not defined", procName, 1);
1724 if (!data1 || !data2)
1725 return ERROR_INT(
"data1 and data2 not both defined", procName, 1);
1726 if (size1 != size2)
return 0;
1727 for (i = 0; i < size1; i++) {
1728 if (data1[i] != data2[i])
1748 const char *newfile)
1754 PROCNAME(
"fileCopy");
1757 return ERROR_INT(
"srcfile not defined", procName, 1);
1759 return ERROR_INT(
"newfile not defined", procName, 1);
1762 return ERROR_INT(
"data not returned", procName, 1);
1778 const char *destfile)
1783 PROCNAME(
"fileConcatenate");
1786 return ERROR_INT(
"srcfile not defined", procName, 1);
1788 return ERROR_INT(
"destfile not defined", procName, 1);
1810 PROCNAME(
"fileAppendString");
1813 return ERROR_INT(
"filename not defined", procName, 1);
1815 return ERROR_INT(
"str not defined", procName, 1);
1818 return ERROR_INT(
"stream not opened", procName, 1);
1819 fprintf(fp,
"%s", str);
1858 const char *rootpath,
1861 l_int32 i, totlines, nlines, index;
1869 PROCNAME(
"fileSplitLinesUniform");
1872 return ERROR_INT(
"filename not defined", procName, 1);
1874 return ERROR_INT(
"rootpath not defined", procName, 1);
1876 return ERROR_INT(
"n must be > 0", procName, 1);
1877 if (save_empty != 0 && save_empty != 1)
1878 return ERROR_INT(
"save_empty not 0 or 1", procName, 1);
1882 return ERROR_INT(
"data not read", procName, 1);
1886 return ERROR_INT(
"sa not made", procName, 1);
1890 L_ERROR(
"num files = %d > num lines = %d\n", procName, n, totlines);
1897 for (i = 0; i < n; i++) {
1899 snprintf(outname,
sizeof(outname),
"%s_%d", rootpath, i);
1901 snprintf(outname,
sizeof(outname),
"%s_%d%s", rootpath, i, ext);
1937 PROCNAME(
"fopenReadStream");
1940 return (FILE *)ERROR_PTR(
"filename not defined", procName, NULL);
1944 fp = fopen(fname,
"rb");
1950 fp = fopen(tail,
"rb");
1954 return (FILE *)ERROR_PTR(
"file not found", procName, NULL);
1976 const char *modestring)
1981 PROCNAME(
"fopenWriteStream");
1984 return (FILE *)ERROR_PTR(
"filename not defined", procName, NULL);
1987 fp = fopen(fname, modestring);
1990 return (FILE *)ERROR_PTR(
"stream not opened", procName, NULL);
2014 PROCNAME(
"fopenReadFromMemory");
2017 return (FILE *)ERROR_PTR(
"data not defined", procName, NULL);
2020 if ((fp = fmemopen((
void *)data, size,
"rb")) == NULL)
2021 return (FILE *)ERROR_PTR(
"stream not opened", procName, NULL);
2023 L_INFO(
"work-around: writing to a temp file\n", procName);
2026 return (FILE *)ERROR_PTR(
"tmpfile stream not opened", procName, NULL);
2028 if ((fp = tmpfile()) == NULL)
2029 return (FILE *)ERROR_PTR(
"tmpfile stream not opened", procName, NULL);
2031 fwrite(data, 1, size, fp);
2062 PROCNAME(
"fopenWriteWinTempfile");
2065 L_ERROR(
"l_makeTempFilename failed, %s\n", procName, strerror(errno));
2069 handle = _open(filename, _O_CREAT | _O_RDWR | _O_SHORT_LIVED |
2070 _O_TEMPORARY | _O_BINARY, _S_IREAD | _S_IWRITE);
2073 L_ERROR(
"_open failed, %s\n", procName, strerror(errno));
2077 if ((fp = _fdopen(handle,
"r+b")) == NULL) {
2078 L_ERROR(
"_fdopen failed, %s\n", procName, strerror(errno));
2116 PROCNAME(
"lept_fopen");
2119 return (FILE *)ERROR_PTR(
"filename not defined", procName, NULL);
2121 return (FILE *)ERROR_PTR(
"mode not defined", procName, NULL);
2145 PROCNAME(
"lept_fclose");
2148 return ERROR_INT(
"stream not defined", procName, 1);
2172 if (nmemb <= 0 || size <= 0)
2174 return LEPT_CALLOC(nmemb, size);
2225 l_uint32 attributes;
2228 PROCNAME(
"lept_mkdir");
2231 L_INFO(
"making named temp subdirectory %s is disabled\n",
2237 return ERROR_INT(
"subdir not defined", procName, 1);
2238 if ((strlen(subdir) == 0) || (subdir[0] ==
'.') || (subdir[0] ==
'/'))
2239 return ERROR_INT(
"subdir not an actual subdirectory", procName, 1);
2242 sarraySplitString(sa, subdir,
"/");
2247 ret = mkdir(dir, 0777);
2249 attributes = GetFileAttributes(dir);
2250 if (attributes == INVALID_FILE_ATTRIBUTES)
2251 ret = (CreateDirectory(dir, NULL) ? 0 : 1);
2254 for (i = 0; i < n; i++) {
2257 ret += mkdir(tmpdir, 0777);
2259 if (CreateDirectory(tmpdir, NULL) == 0)
2260 ret += (GetLastError () != ERROR_ALREADY_EXISTS);
2268 L_ERROR(
"failure to create %d directories\n", procName, ret);
2297 char *dir, *realdir, *fname, *fullname;
2298 l_int32 exists, ret, i, nfiles;
2304 PROCNAME(
"lept_rmdir");
2307 return ERROR_INT(
"subdir not defined", procName, 1);
2308 if ((strlen(subdir) == 0) || (subdir[0] ==
'.') || (subdir[0] ==
'/'))
2309 return ERROR_INT(
"subdir not an actual subdirectory", procName, 1);
2314 return ERROR_INT(
"directory name not made", procName, 1);
2323 L_ERROR(
"directory %s does not exist!\n", procName, dir);
2329 for (i = 0; i < nfiles; i++) {
2333 LEPT_FREE(fullname);
2338 ret = rmdir(realdir);
2342 ret = (RemoveDirectory(newpath) ? 0 : 1);
2374 if (!pexists)
return;
2383 l_int32 err = stat(realdir, &s);
2384 if (err != -1 && S_ISDIR(s.st_mode))
2389 l_uint32 attributes;
2390 attributes = GetFileAttributes(realdir);
2391 if (attributes != INVALID_FILE_ATTRIBUTES &&
2392 (attributes & FILE_ATTRIBUTE_DIRECTORY))
2435 PROCNAME(
"lept_rm_match");
2439 return ERROR_INT(
"sa not made", procName, -1);
2442 L_WARNING(
"no matching files found\n", procName);
2448 for (i = 0; i < n; i++) {
2452 L_ERROR(
"failed to remove %s\n", procName, path);
2484 PROCNAME(
"lept_rm");
2486 if (!tail || strlen(tail) == 0)
2487 return ERROR_INT(
"tail undefined or empty", procName, 1);
2490 return ERROR_INT(
"temp dirname not made", procName, 1);
2521 PROCNAME(
"lept_rmfile");
2523 if (!filepath || strlen(filepath) == 0)
2524 return ERROR_INT(
"filepath undefined or empty", procName, 1);
2527 ret = remove(filepath);
2530 SetFileAttributes(filepath, FILE_ATTRIBUTE_NORMAL);
2531 ret = DeleteFile(filepath) ? 0 : 1;
2574 const char *newtail,
2577 char *srcpath, *newpath, *dir, *srctail;
2581 PROCNAME(
"lept_mv");
2584 return ERROR_INT(
"srcfile not defined", procName, 1);
2588 return ERROR_INT(
"newdir not NULL or a subdir of /tmp", procName, 1);
2598 if (!newtail || newtail[0] ==
'\0')
2599 newpath =
pathJoin(newtemp, srctail);
2601 newpath =
pathJoin(newtemp, newtail);
2609 LEPT_FREE(realpath);
2616 if (!newtail || newtail[0] ==
'\0')
2623 ret = MoveFileEx(srcpath, newpath,
2624 MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING) ? 0 : 1;
2629 *pnewpath = newpath;
2673 const char *newtail,
2676 char *srcpath, *newpath, *dir, *srctail;
2680 PROCNAME(
"lept_cp");
2683 return ERROR_INT(
"srcfile not defined", procName, 1);
2687 return ERROR_INT(
"newdir not NULL or a subdir of /tmp", procName, 1);
2697 if (!newtail || newtail[0] ==
'\0')
2698 newpath =
pathJoin(newtemp, srctail);
2700 newpath =
pathJoin(newtemp, newtail);
2710 if (!newtail || newtail[0] ==
'\0')
2717 ret = CopyFile(srcpath, newpath, FALSE) ? 0 : 1;
2722 *pnewpath = newpath;
2732 #if defined(__APPLE__) 2733 #include "TargetConditionals.h" 2756 PROCNAME(
"callSystemDebug");
2759 L_ERROR(
"cmd not defined\n", procName);
2762 if (LeptDebugOK == FALSE) {
2763 L_INFO(
"'system' calls are disabled\n", procName);
2767 #if defined(__APPLE__) 2769 #if (defined(TARGET_OS_OSX) && TARGET_OS_OSX == 1) 2771 #elif TARGET_OS_IPHONE || defined(OS_IOS) 2772 L_ERROR(
"iOS 11 does not support system()\n", procName);
2828 char *cpathname, *lastslash;
2830 PROCNAME(
"splitPathAtDirectory");
2832 if (!pdir && !ptail)
2833 return ERROR_INT(
"null input for both strings", procName, 1);
2834 if (pdir) *pdir = NULL;
2835 if (ptail) *ptail = NULL;
2837 return ERROR_INT(
"pathname not defined", procName, 1);
2841 lastslash = strrchr(cpathname,
'/');
2846 *(lastslash + 1) =
'\0';
2849 LEPT_FREE(cpathname);
2857 LEPT_FREE(cpathname);
2898 char *tail, *dir, *lastdot;
2901 PROCNAME(
"splitPathExtension");
2903 if (!pbasename && !pextension)
2904 return ERROR_INT(
"null input for both strings", procName, 1);
2905 if (pbasename) *pbasename = NULL;
2906 if (pextension) *pextension = NULL;
2908 return ERROR_INT(
"pathname not defined", procName, 1);
2915 if ((lastdot = strrchr(tail,
'.'))) {
2976 const char *slash =
"/";
2978 l_int32 i, n1, n2, emptydir;
2983 PROCNAME(
"pathJoin");
2987 if (dir && strlen(dir) >= 2 && dir[0] ==
'.' && dir[1] ==
'.')
2988 return (
char *)ERROR_PTR(
"dir starts with '..'", procName, NULL);
2989 if (fname && strlen(fname) >= 2 && fname[0] ==
'.' && fname[1] ==
'.')
2990 return (
char *)ERROR_PTR(
"fname starts with '..'", procName, NULL);
2997 if (dir && strlen(dir) > 0) {
3000 sarraySplitString(sa1, dir,
"/");
3002 for (i = 0; i < n1; i++) {
3010 emptydir = dir && strlen(dir) == 0;
3011 if ((!dir || emptydir) && fname && strlen(fname) > 0 && fname[0] ==
'/')
3015 if (fname && strlen(fname) > 0) {
3016 sarraySplitString(sa2, fname,
"/");
3018 for (i = 0; i < n2; i++) {
3027 if (size > 1 && dest[size - 1] ==
'/')
3028 dest[size - 1] =
'\0';
3053 const char *subdirs)
3056 size_t len1, len2, len3, len4;
3058 PROCNAME(
"appendSubdirs");
3060 if (!basedir || !subdirs)
3061 return (
char *)ERROR_PTR(
"basedir and subdirs not both defined",
3064 len1 = strlen(basedir);
3065 len2 = strlen(subdirs);
3066 len3 = len1 + len2 + 8;
3067 if ((newdir = (
char *)LEPT_CALLOC(len3, 1)) == NULL)
3068 return (
char *)ERROR_PTR(
"newdir not made", procName, NULL);
3070 if (newdir[len1 - 1] !=
'/')
3072 if (subdirs[0] ==
'/')
3076 len4 = strlen(newdir);
3077 if (newdir[len4 - 1] ==
'/')
3078 newdir[len4 - 1] =
'\0';
3114 PROCNAME(
"convertSepCharsInPath");
3116 return ERROR_INT(
"path not defined", procName, 1);
3117 if (type != UNIX_PATH_SEPCHAR && type != WIN_PATH_SEPCHAR)
3118 return ERROR_INT(
"invalid type", procName, 1);
3121 if (type == UNIX_PATH_SEPCHAR) {
3123 for (i = 0; i < len; i++) {
3124 if (path[i] ==
'\\')
3129 for (i = 0; i < len; i++) {
3176 l_int32 is_win32 = FALSE;
3177 char *cdir, *pathout;
3178 l_int32 dirlen, namelen;
3181 PROCNAME(
"genPathname");
3184 return (
char *)ERROR_PTR(
"no input", procName, NULL);
3187 if (!dir || dir[0] ==
'\0') {
3188 if ((cdir = getcwd(NULL, 0)) == NULL)
3189 return (
char *)ERROR_PTR(
"no current dir found", procName, NULL);
3197 dirlen = strlen(cdir);
3198 if (cdir[dirlen - 1] ==
'/' && dirlen != 1) {
3199 cdir[dirlen - 1] =
'\0';
3203 namelen = (fname) ? strlen(fname) : 0;
3204 size = dirlen + namelen + 256;
3205 if ((pathout = (
char *)LEPT_CALLOC(size,
sizeof(
char))) == NULL) {
3207 return (
char *)ERROR_PTR(
"pathout not made", procName, NULL);
3218 if (!is_win32 || dirlen < 4 ||
3219 (dirlen == 4 && strncmp(cdir,
"/tmp", 4) != 0) ||
3220 (dirlen > 4 && strncmp(cdir,
"/tmp/", 5) != 0)) {
3225 char tmpdir[MAX_PATH];
3226 GetTempPath(
sizeof(tmpdir), tmpdir);
3227 tmpdirlen = strlen(tmpdir);
3228 if (tmpdirlen > 0 && tmpdir[tmpdirlen - 1] ==
'\\') {
3229 tmpdir[tmpdirlen - 1] =
'\0';
3231 tmpdirlen = strlen(tmpdir);
3241 if (fname && strlen(fname) > 0) {
3242 dirlen = strlen(pathout);
3243 pathout[dirlen] =
'/';
3290 PROCNAME(
"makeTempDirname");
3293 return ERROR_INT(
"result not defined", procName, 1);
3294 if (subdir && ((subdir[0] ==
'.') || (subdir[0] ==
'/')))
3295 return ERROR_INT(
"subdir not an actual subdirectory", procName, 1);
3297 memset(result, 0, nbytes);
3301 size_t n = confstr(_CS_DARWIN_USER_TEMP_DIR, result, nbytes);
3303 L_ERROR(
"failed to find tmp dir, %s\n", procName, strerror(errno));
3305 }
else if (n > nbytes) {
3306 return ERROR_INT(
"result array too small for path\n", procName, 1);
3319 pathlen = strlen(path);
3320 if (pathlen < nbytes - 1) {
3323 L_ERROR(
"result array too small for path\n", procName);
3354 PROCNAME(
"modifyTrailingSlash");
3357 return ERROR_INT(
"path not defined", procName, 1);
3359 return ERROR_INT(
"invalid flag", procName, 1);
3362 lastchar = path[len - 1];
3365 path[len + 1] =
'\0';
3367 path[len - 1] =
'\0';
3401 PROCNAME(
"l_makeTempFilename");
3404 return (
char *)ERROR_PTR(
"failed to make dirname", procName, NULL);
3411 fd = mkstemp(pattern);
3414 return (
char *)ERROR_PTR(
"mkstemp failed", procName, NULL);
3421 char fname[MAX_PATH];
3423 if (GetTempFileName(dirname,
"lp.", 0, fname) == 0)
3424 return (
char *)ERROR_PTR(
"GetTempFileName failed", procName, NULL);
3425 if ((fp = fopen(fname,
"wb")) == NULL)
3426 return (
char *)ERROR_PTR(
"file cannot be written to", procName, NULL);
3457 char *tail, *basename;
3458 l_int32 len, nret, num;
3460 PROCNAME(
"extractNumberFromFilename");
3463 return ERROR_INT(
"fname not defined", procName, -1);
3469 len = strlen(basename);
3470 if (numpre + numpost > len - 1) {
3471 LEPT_FREE(basename);
3472 return ERROR_INT(
"numpre + numpost too big", procName, -1);
3475 basename[len - numpost] =
'\0';
3476 nret = sscanf(basename + numpre,
"%d", &num);
3477 LEPT_FREE(basename);
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_uint8 * l_binaryReadSelectStream(FILE *fp, size_t start, size_t nbytes, size_t *pnread)
l_binaryReadSelectStream()
size_t nbytesInFile(const char *filename)
nbytesInFile()
l_int32 lept_mv(const char *srcfile, const char *newdir, const char *newtail, char **pnewpath)
lept_mv()
l_int32 lept_mkdir(const char *subdir)
lept_mkdir()
l_ok l_byteaAppendString(L_BYTEA *ba, const char *str)
l_byteaAppendString()
l_ok fileSplitLinesUniform(const char *filename, l_int32 n, l_int32 save_empty, const char *rootpath, const char *ext)
fileSplitLinesUniform()
l_int32 l_dnaGetCount(L_DNA *da)
l_dnaGetCount()
L_BBUFFER * bbufferCreate(const l_uint8 *indata, l_int32 nalloc)
bbufferCreate()
l_ok splitPathAtExtension(const char *pathname, char **pbasename, char **pextension)
splitPathAtExtension()
l_int32 stringLength(const char *src, size_t size)
stringLength()
l_ok stringCheckForChars(const char *src, const char *chars, l_int32 *pfound)
stringCheckForChars()
char * genPathname(const char *dir, const char *fname)
genPathname()
L_DNA * arrayFindEachSequence(const l_uint8 *data, size_t datalen, const l_uint8 *sequence, size_t seqlen)
arrayFindEachSequence()
L_BYTEA * l_byteaCreate(size_t nbytes)
l_byteaCreate()
char * l_makeTempFilename(void)
l_makeTempFilename()
void l_dnaDestroy(L_DNA **pda)
l_dnaDestroy()
char * stringReplaceSubstr(const char *src, const char *sub1, const char *sub2, l_int32 *ploc, l_int32 *pfound)
stringReplaceSubstr()
l_ok fileCopy(const char *srcfile, const char *newfile)
fileCopy()
char * stringNew(const char *src)
stringNew()
char * appendSubdirs(const char *basedir, const char *subdirs)
appendSubdirs()
l_uint8 * l_binaryReadSelect(const char *filename, size_t start, size_t nbytes, size_t *pnread)
l_binaryReadSelect()
void * lept_calloc(size_t nmemb, size_t size)
lept_calloc()
l_ok l_dnaGetIValue(L_DNA *da, l_int32 index, l_int32 *pival)
l_dnaGetIValue()
l_ok fileConcatenate(const char *srcfile, const char *destfile)
fileConcatenate()
char * stringCopySegment(const char *src, l_int32 start, l_int32 nbytes)
stringCopySegment()
char * stringRemoveChars(const char *src, const char *remchars)
stringRemoveChars()
SARRAY * sarrayCreate(l_int32 n)
sarrayCreate()
char * sarrayToStringRange(SARRAY *sa, l_int32 first, l_int32 nstrings, l_int32 addnlflag)
sarrayToStringRange()
SARRAY * getFilenamesInDirectory(const char *dirname)
getFilenamesInDirectory()
FILE * fopenReadFromMemory(const l_uint8 *data, size_t size)
fopenReadFromMemory()
l_int32 lept_cp(const char *srcfile, const char *newdir, const char *newtail, char **pnewpath)
lept_cp()
l_int32 lept_rm_match(const char *subdir, const char *substr)
lept_rm_match()
l_ok l_dnaAddNumber(L_DNA *da, l_float64 val)
l_dnaAddNumber()
l_ok modifyTrailingSlash(char *path, size_t nbytes, l_int32 flag)
modifyTrailingSlash()
l_ok l_binaryCompare(const l_uint8 *data1, size_t size1, const l_uint8 *data2, size_t size2, l_int32 *psame)
l_binaryCompare()
l_int32 stringCat(char *dest, size_t size, const char *src)
stringCat()
l_ok l_binaryWrite(const char *filename, const char *operation, const void *data, size_t nbytes)
l_binaryWrite()
l_uint8 * l_binaryRead(const char *filename, size_t *pnbytes)
l_binaryRead()
l_ok stringSplitOnToken(char *cstr, const char *seps, char **phead, char **ptail)
stringSplitOnToken()
NUMA * numaGetUniformBinSizes(l_int32 ntotal, l_int32 nbins)
numaGetUniformBinSizes()
l_ok numaGetIValue(NUMA *na, l_int32 index, l_int32 *pival)
numaGetIValue()
l_ok stringJoinIP(char **psrc1, const char *src2)
stringJoinIP()
void lept_free(void *ptr)
lept_free()
l_ok fileAppendString(const char *filename, const char *str)
fileAppendString()
size_t fnbytesInFile(FILE *fp)
fnbytesInFile()
char * sarrayGetString(SARRAY *sa, l_int32 index, l_int32 copyflag)
sarrayGetString()
void * reallocNew(void **pindata, size_t oldsize, size_t newsize)
reallocNew()
char * stringReplaceEachSubstr(const char *src, const char *sub1, const char *sub2, l_int32 *pcount)
stringReplaceEachSubstr()
l_ok stringReplace(char **pdest, const char *src)
stringReplace()
void lept_direxists(const char *dir, l_int32 *pexists)
lept_direxists()
SARRAY * sarrayCreateLinesFromString(const char *string, l_int32 blankflag)
sarrayCreateLinesFromString()
SARRAY * getSortedPathnamesInDirectory(const char *dirname, const char *substr, l_int32 first, l_int32 nfiles)
getSortedPathnamesInDirectory()
l_int32 extractNumberFromFilename(const char *fname, l_int32 numpre, l_int32 numpost)
extractNumberFromFilename()
l_ok splitPathAtDirectory(const char *pathname, char **pdir, char **ptail)
splitPathAtDirectory()
void numaDestroy(NUMA **pna)
numaDestroy()
void bbufferDestroy(L_BBUFFER **pbb)
bbufferDestroy()
l_uint8 * l_binaryCopy(const l_uint8 *datas, size_t size)
l_binaryCopy()
FILE * fopenWriteStream(const char *filename, const char *modestring)
fopenWriteStream()
FILE * fopenWriteWinTempfile(void)
fopenWriteWinTempfile()
FILE * fopenReadStream(const char *filename)
fopenReadStream()
l_uint8 * l_binaryReadStream(FILE *fp, size_t *pnbytes)
l_binaryReadStream()
l_int32 sarrayGetCount(SARRAY *sa)
sarrayGetCount()
FILE * lept_fopen(const char *filename, const char *mode)
lept_fopen()
L_DNA * stringFindEachSubstr(const char *src, const char *sub)
stringFindEachSubstr()
l_ok lept_fclose(FILE *fp)
lept_fclose()
char * pathJoin(const char *dir, const char *fname)
pathJoin()
L_DNA * l_dnaCreate(l_int32 n)
l_dnaCreate()
char * stringJoin(const char *src1, const char *src2)
stringJoin()
l_ok makeTempDirname(char *result, size_t nbytes, const char *subdir)
makeTempDirname()
l_ok convertSepCharsInPath(char *path, l_int32 type)
convertSepCharsInPath()
l_ok bbufferExtendArray(L_BBUFFER *bb, l_int32 nbytes)
bbufferExtendArray()
void callSystemDebug(const char *cmd)
callSystemDebug()
char * strtokSafe(char *cstr, const char *seps, char **psaveptr)
strtokSafe()
void l_byteaDestroy(L_BYTEA **pba)
l_byteaDestroy()
l_ok stringCopy(char *dest, const char *src, l_int32 n)
stringCopy()
l_int32 stringFindSubstr(const char *src, const char *sub, l_int32 *ploc)
stringFindSubstr()
l_int32 lept_rmdir(const char *subdir)
lept_rmdir()
char * stringReverse(const char *src)
stringReverse()
l_int32 lept_rm(const char *subdir, const char *tail)
lept_rm()
char * stringConcatNew(const char *first,...)
stringConcatNew()
l_uint8 * l_byteaCopyData(L_BYTEA *ba, size_t *psize)
l_byteaCopyData()
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_int32 lept_rmfile(const char *filepath)
lept_rmfile()
void sarrayDestroy(SARRAY **psa)
sarrayDestroy()