176 #include "config_auto.h" 190 #include <sys/stat.h> 192 #include <sys/stat.h> 193 #include <sys/types.h> 198 #include "allheaders.h" 202 #define DEBUG_REWRITE 0 220 PROCNAME(
"stringNew");
223 L_WARNING(
"src not defined\n", procName);
228 if ((dest = (
char *)LEPT_CALLOC(len + 1,
sizeof(
char))) == NULL)
229 return (
char *)ERROR_PTR(
"dest not made", procName, NULL);
261 PROCNAME(
"stringCopy");
264 return ERROR_INT(
"dest not defined", procName, 1);
269 for (i = 0; i < n && src[i] !=
'\0'; i++)
304 PROCNAME(
"stringCopySegment");
307 return (
char *)ERROR_PTR(
"src not defined", procName, NULL);
309 if (start < 0 || start > len - 1)
310 return (
char *)ERROR_PTR(
"invalid start", procName, NULL);
312 nbytes = len - start;
313 if (start + nbytes > len)
314 nbytes = len - start;
315 if ((dest = (
char *)LEPT_CALLOC(nbytes + 1,
sizeof(
char))) == NULL)
316 return (
char *)ERROR_PTR(
"dest not made", procName, NULL);
340 PROCNAME(
"stringReplace");
343 return ERROR_INT(
"pdest not defined", procName, 1);
378 PROCNAME(
"stringLength");
381 return ERROR_INT(
"src not defined", procName, 0);
385 for (i = 0; i < size; i++) {
419 l_int32 lendest, lensrc;
421 PROCNAME(
"stringCat");
424 return ERROR_INT(
"dest not defined", procName, -1);
426 return ERROR_INT(
"size < 1; too small", procName, -1);
432 return ERROR_INT(
"no terminating nul byte", procName, -1);
436 n = (lendest + lensrc > size - 1 ? size - lendest - 1 : lensrc);
438 return ERROR_INT(
"dest too small for append", procName, -1);
440 for (i = 0; i < n; i++)
441 dest[lendest + i] = src[i];
442 dest[lendest + n] =
'\0';
469 if (!first)
return NULL;
472 va_start(args, first);
474 while ((arg = va_arg(args,
const char *)) != NULL)
477 result = (
char *)LEPT_CALLOC(len + 1,
sizeof(
char));
480 va_start(args, first);
485 while ((arg = va_arg(args,
const char *)) != NULL) {
513 l_int32 srclen1, srclen2, destlen;
515 PROCNAME(
"stringJoin");
517 srclen1 = (src1) ? strlen(src1) : 0;
518 srclen2 = (src2) ? strlen(src2) : 0;
519 destlen = srclen1 + srclen2 + 3;
521 if ((dest = (
char *)LEPT_CALLOC(destlen,
sizeof(
char))) == NULL)
522 return (
char *)ERROR_PTR(
"calloc fail for dest", procName, NULL);
527 strncat(dest, src2, srclen2);
569 PROCNAME(
"stringJoinIP");
572 return ERROR_INT(
"&src1 not defined", procName, 1);
593 PROCNAME(
"stringReverse");
596 return (
char *)ERROR_PTR(
"src not defined", procName, NULL);
598 if ((dest = (
char *)LEPT_CALLOC(len + 1,
sizeof(
char))) == NULL)
599 return (
char *)ERROR_PTR(
"calloc fail for dest", procName, NULL);
600 for (i = 0; i < len; i++)
601 dest[i] = src[len - 1 - i];
645 char *start, *substr;
646 l_int32 istart, i, j, nchars;
648 PROCNAME(
"strtokSafe");
651 return (
char *)ERROR_PTR(
"seps not defined", procName, NULL);
653 return (
char *)ERROR_PTR(
"&saveptr not defined", procName, NULL);
667 for (istart = 0;; istart++) {
668 if ((nextc = start[istart]) ==
'\0') {
672 if (!strchr(seps, nextc))
679 for (i = istart;; i++) {
680 if ((nextc = start[i]) ==
'\0')
682 if (strchr(seps, nextc))
688 substr = (
char *)LEPT_CALLOC(nchars + 1,
sizeof(
char));
694 if ((nextc = start[j]) ==
'\0') {
698 if (!strchr(seps, nextc)) {
699 *psaveptr = start + j;
741 PROCNAME(
"stringSplitOnToken");
744 return ERROR_INT(
"&head not defined", procName, 1);
746 return ERROR_INT(
"&tail not defined", procName, 1);
747 *phead = *ptail = NULL;
749 return ERROR_INT(
"cstr not defined", procName, 1);
751 return ERROR_INT(
"seps not defined", procName, 1);
785 PROCNAME(
"stringCheckForChars");
788 return ERROR_INT(
"&found not defined", procName, 1);
791 return ERROR_INT(
"src and chars not both defined", procName, 1);
794 for (i = 0; i < n; i++) {
796 if (strchr(chars, ch)) {
814 const char *remchars)
820 PROCNAME(
"stringRemoveChars");
823 return (
char *)ERROR_PTR(
"src not defined", procName, NULL);
827 if ((dest = (
char *)LEPT_CALLOC(strlen(src) + 1,
sizeof(
char))) == NULL)
828 return (
char *)ERROR_PTR(
"dest not made", procName, NULL);
830 for (i = 0, k = 0; i < nsrc; i++) {
832 if (!strchr(remchars, ch))
864 PROCNAME(
"stringFindSubstr");
867 return ERROR_INT(
"src not defined", procName, 0);
869 return ERROR_INT(
"sub not defined", procName, 0);
870 if (ploc) *ploc = -1;
871 if (strlen(sub) == 0)
872 return ERROR_INT(
"substring length 0", procName, 0);
873 if (strlen(src) == 0)
876 if ((ptr = strstr(src, sub)) == NULL)
918 l_int32 nsrc, nsub1, nsub2, len, npre, loc;
920 PROCNAME(
"stringReplaceSubstr");
923 return (
char *)ERROR_PTR(
"src not defined", procName, NULL);
925 return (
char *)ERROR_PTR(
"sub1 not defined", procName, NULL);
927 return (
char *)ERROR_PTR(
"sub2 not defined", procName, NULL);
935 if ((ptr = strstr(src + loc, sub1)) == NULL) {
942 nsub1 = strlen(sub1);
943 nsub2 = strlen(sub2);
944 len = nsrc + nsub2 - nsub1;
945 if ((dest = (
char *)LEPT_CALLOC(len + 1,
sizeof(
char))) == NULL)
946 return (
char *)ERROR_PTR(
"dest not made", procName, NULL);
948 memcpy(dest, src, npre);
949 strcpy(dest + npre, sub2);
950 strcpy(dest + npre + nsub2, ptr + nsub1);
952 *ploc = npre + nsub2;
982 char *currstr, *newstr;
985 PROCNAME(
"stringReplaceEachSubstr");
987 if (pcount) *pcount = 0;
989 return (
char *)ERROR_PTR(
"src not defined", procName, NULL);
991 return (
char *)ERROR_PTR(
"sub1 not defined", procName, NULL);
993 return (
char *)ERROR_PTR(
"sub2 not defined", procName, NULL);
1034 const l_uint8 *sequence,
1037 l_int32 start, offset, realoffset, found;
1040 PROCNAME(
"arrayFindEachSequence");
1042 if (!data || !sequence)
1043 return (
L_DNA *)ERROR_PTR(
"data & sequence not both defined",
1054 realoffset = start + offset;
1056 start = realoffset + seqlen;
1057 if (start >= datalen)
1094 const l_uint8 *sequence,
1099 l_int32 i, j, found, lastpos;
1101 PROCNAME(
"arrayFindSequence");
1103 if (poffset) *poffset = 0;
1104 if (pfound) *pfound = FALSE;
1105 if (!data || !sequence)
1106 return ERROR_INT(
"data & sequence not both defined", procName, 1);
1107 if (!poffset || !pfound)
1108 return ERROR_INT(
"&offset and &found not defined", procName, 1);
1110 lastpos = datalen - seqlen + 1;
1112 for (i = 0; i < lastpos; i++) {
1113 for (j = 0; j < seqlen; j++) {
1114 if (data[i + j] != sequence[j])
1116 if (j == seqlen - 1)
1123 if (found == TRUE) {
1169 PROCNAME(
"reallocNew");
1172 return ERROR_PTR(
"input data not defined", procName, NULL);
1184 if ((newdata = (
void *)LEPT_CALLOC(1, newsize)) == NULL)
1185 return ERROR_PTR(
"newdata not made", procName, NULL);
1190 if ((newdata = (
void *)LEPT_CALLOC(1, newsize)) == NULL)
1191 return ERROR_PTR(
"newdata not made", procName, NULL);
1192 minsize = L_MIN(oldsize, newsize);
1193 memcpy(newdata, indata, minsize);
1218 PROCNAME(
"l_binaryRead");
1221 return (l_uint8 *)ERROR_PTR(
"pnbytes not defined", procName, NULL);
1224 return (l_uint8 *)ERROR_PTR(
"filename not defined", procName, NULL);
1227 return (l_uint8 *)ERROR_PTR(
"file stream not opened", procName, NULL);
1266 l_int32 seekable, navail, nadd, nread;
1269 PROCNAME(
"l_binaryReadStream");
1272 return (l_uint8 *)ERROR_PTR(
"&nbytes not defined", procName, NULL);
1275 return (l_uint8 *)ERROR_PTR(
"fp not defined", procName, NULL);
1281 seekable = (ftell(fp) == 0) ? 1 : 0;
1290 if (navail < 4096) {
1291 nadd = L_MAX(bb->
nalloc, 4096);
1294 nread = fread((
void *)(bb->
array + bb->
n), 1, 4096, fp);
1296 if (nread != 4096)
break;
1301 if ((data = (l_uint8 *)LEPT_CALLOC(bb->
n + 1,
sizeof(l_uint8))) != NULL) {
1302 memcpy(data, bb->
array, bb->
n);
1305 L_ERROR(
"calloc fail for data\n", procName);
1337 PROCNAME(
"l_binaryReadSelect");
1340 return (l_uint8 *)ERROR_PTR(
"pnread not defined", procName, NULL);
1343 return (l_uint8 *)ERROR_PTR(
"filename not defined", procName, NULL);
1346 return (l_uint8 *)ERROR_PTR(
"file stream not opened", procName, NULL);
1380 size_t bytesleft, bytestoread, nread, filebytes;
1382 PROCNAME(
"l_binaryReadSelectStream");
1385 return (l_uint8 *)ERROR_PTR(
"&nread not defined", procName, NULL);
1388 return (l_uint8 *)ERROR_PTR(
"stream not defined", procName, NULL);
1391 fseek(fp, 0, SEEK_END);
1392 filebytes = ftell(fp);
1393 fseek(fp, 0, SEEK_SET);
1394 if (start > filebytes) {
1395 L_ERROR(
"start = %lu but filebytes = %lu\n", procName,
1396 (
unsigned long)start, (
unsigned long)filebytes);
1400 return (l_uint8 *)LEPT_CALLOC(1, 1);
1401 bytesleft = filebytes - start;
1402 if (nbytes == 0) nbytes = bytesleft;
1403 bytestoread = (bytesleft >= nbytes) ? nbytes : bytesleft;
1406 if ((data = (l_uint8 *)LEPT_CALLOC(1, bytestoread + 1)) == NULL)
1407 return (l_uint8 *)ERROR_PTR(
"calloc fail for data", procName, NULL);
1408 fseek(fp, start, SEEK_SET);
1409 nread = fread(data, 1, bytestoread, fp);
1410 if (nbytes != nread)
1411 L_INFO(
"%lu bytes requested; %lu bytes read\n", procName,
1412 (
unsigned long)nbytes, (
unsigned long)nread);
1414 fseek(fp, 0, SEEK_SET);
1430 const char *operation,
1434 char actualOperation[20];
1437 PROCNAME(
"l_binaryWrite");
1440 return ERROR_INT(
"filename not defined", procName, 1);
1442 return ERROR_INT(
"operation not defined", procName, 1);
1444 return ERROR_INT(
"data not defined", procName, 1);
1446 return ERROR_INT(
"nbytes must be > 0", procName, 1);
1448 if (strcmp(operation,
"w") && strcmp(operation,
"a"))
1449 return ERROR_INT(
"operation not one of {'w','a'}", procName, 1);
1454 strncat(actualOperation,
"b", 2);
1457 return ERROR_INT(
"stream not opened", procName, 1);
1458 fwrite(data, 1, nbytes, fp);
1476 PROCNAME(
"nbytesInFile");
1479 return ERROR_INT(
"filename not defined", procName, 0);
1481 return ERROR_INT(
"stream not opened", procName, 0);
1497 l_int64 pos, nbytes;
1499 PROCNAME(
"fnbytesInFile");
1502 return ERROR_INT(
"stream not open", procName, 0);
1506 return ERROR_INT(
"seek position must be > 0", procName, 0);
1507 fseek(fp, 0, SEEK_END);
1509 fseek(fp, pos, SEEK_SET);
1538 PROCNAME(
"l_binaryCopy");
1541 return (l_uint8 *)ERROR_PTR(
"datas not defined", procName, NULL);
1543 if ((datad = (l_uint8 *)LEPT_CALLOC(size + 4,
sizeof(l_uint8))) == NULL)
1544 return (l_uint8 *)ERROR_PTR(
"datad not made", procName, NULL);
1545 memcpy(datad, datas, size);
1562 const char *newfile)
1568 PROCNAME(
"fileCopy");
1571 return ERROR_INT(
"srcfile not defined", procName, 1);
1573 return ERROR_INT(
"newfile not defined", procName, 1);
1576 return ERROR_INT(
"data not returned", procName, 1);
1592 const char *destfile)
1597 PROCNAME(
"fileConcatenate");
1600 return ERROR_INT(
"srcfile not defined", procName, 1);
1602 return ERROR_INT(
"destfile not defined", procName, 1);
1624 PROCNAME(
"fileAppendString");
1627 return ERROR_INT(
"filename not defined", procName, 1);
1629 return ERROR_INT(
"str not defined", procName, 1);
1632 return ERROR_INT(
"stream not opened", procName, 1);
1633 fprintf(fp,
"%s", str);
1662 PROCNAME(
"fopenReadStream");
1665 return (FILE *)ERROR_PTR(
"filename not defined", procName, NULL);
1669 fp = fopen(fname,
"rb");
1675 fp = fopen(tail,
"rb");
1679 return (FILE *)ERROR_PTR(
"file not found", procName, NULL);
1701 const char *modestring)
1706 PROCNAME(
"fopenWriteStream");
1709 return (FILE *)ERROR_PTR(
"filename not defined", procName, NULL);
1712 fp = fopen(fname, modestring);
1715 return (FILE *)ERROR_PTR(
"stream not opened", procName, NULL);
1739 PROCNAME(
"fopenReadFromMemory");
1742 return (FILE *)ERROR_PTR(
"data not defined", procName, NULL);
1745 if ((fp = fmemopen((
void *)data, size,
"rb")) == NULL)
1746 return (FILE *)ERROR_PTR(
"stream not opened", procName, NULL);
1748 L_INFO(
"work-around: writing to a temp file\n", procName);
1751 return (FILE *)ERROR_PTR(
"tmpfile stream not opened", procName, NULL);
1753 if ((fp = tmpfile()) == NULL)
1754 return (FILE *)ERROR_PTR(
"tmpfile stream not opened", procName, NULL);
1756 fwrite(data, 1, size, fp);
1787 PROCNAME(
"fopenWriteWinTempfile");
1790 L_ERROR(
"l_makeTempFilename failed, %s\n", procName, strerror(errno));
1794 handle = _open(filename, _O_CREAT | _O_RDWR | _O_SHORT_LIVED |
1795 _O_TEMPORARY | _O_BINARY, _S_IREAD | _S_IWRITE);
1798 L_ERROR(
"_open failed, %s\n", procName, strerror(errno));
1802 if ((fp = _fdopen(handle,
"r+b")) == NULL) {
1803 L_ERROR(
"_fdopen failed, %s\n", procName, strerror(errno));
1841 PROCNAME(
"lept_fopen");
1844 return (FILE *)ERROR_PTR(
"filename not defined", procName, NULL);
1846 return (FILE *)ERROR_PTR(
"mode not defined", procName, NULL);
1870 PROCNAME(
"lept_fclose");
1873 return ERROR_INT(
"stream not defined", procName, 1);
1897 if (nmemb <= 0 || size <= 0)
1899 return LEPT_CALLOC(nmemb, size);
1951 l_uint32 attributes;
1954 PROCNAME(
"lept_mkdir");
1957 L_INFO(
"making named temp subdirectory %s is disabled\n",
1963 return ERROR_INT(
"subdir not defined", procName, 1);
1964 if ((strlen(subdir) == 0) || (subdir[0] ==
'.') || (subdir[0] ==
'/'))
1965 return ERROR_INT(
"subdir not an actual subdirectory", procName, 1);
1968 sarraySplitString(sa, subdir,
"/");
1973 ret = mkdir(dir, 0777);
1975 attributes = GetFileAttributes(dir);
1976 if (attributes == INVALID_FILE_ATTRIBUTES)
1977 ret = (CreateDirectory(dir, NULL) ? 0 : 1);
1980 for (i = 0; i < n; i++) {
1983 ret += mkdir(tmpdir, 0777);
1985 if (CreateDirectory(tmpdir, NULL) == 0)
1986 ret += (GetLastError () != ERROR_ALREADY_EXISTS);
1994 L_ERROR(
"failure to create %d directories\n", procName, ret);
2023 char *dir, *realdir, *fname, *fullname;
2024 l_int32 exists, ret, i, nfiles;
2030 PROCNAME(
"lept_rmdir");
2033 return ERROR_INT(
"subdir not defined", procName, 1);
2034 if ((strlen(subdir) == 0) || (subdir[0] ==
'.') || (subdir[0] ==
'/'))
2035 return ERROR_INT(
"subdir not an actual subdirectory", procName, 1);
2040 return ERROR_INT(
"directory name not made", procName, 1);
2049 L_ERROR(
"directory %s does not exist!\n", procName, dir);
2055 for (i = 0; i < nfiles; i++) {
2059 LEPT_FREE(fullname);
2064 ret = rmdir(realdir);
2068 ret = (RemoveDirectory(newpath) ? 0 : 1);
2100 if (!pexists)
return;
2109 l_int32 err = stat(realdir, &s);
2110 if (err != -1 && S_ISDIR(s.st_mode))
2114 l_uint32 attributes;
2115 attributes = GetFileAttributes(realdir);
2116 if (attributes != INVALID_FILE_ATTRIBUTES &&
2117 (attributes & FILE_ATTRIBUTE_DIRECTORY)) {
2161 PROCNAME(
"lept_rm_match");
2165 return ERROR_INT(
"sa not made", procName, -1);
2168 L_WARNING(
"no matching files found\n", procName);
2174 for (i = 0; i < n; i++) {
2178 L_ERROR(
"failed to remove %s\n", procName, path);
2210 PROCNAME(
"lept_rm");
2212 if (!tail || strlen(tail) == 0)
2213 return ERROR_INT(
"tail undefined or empty", procName, 1);
2216 return ERROR_INT(
"temp dirname not made", procName, 1);
2247 PROCNAME(
"lept_rmfile");
2249 if (!filepath || strlen(filepath) == 0)
2250 return ERROR_INT(
"filepath undefined or empty", procName, 1);
2253 ret =
remove(filepath);
2256 SetFileAttributes(filepath, FILE_ATTRIBUTE_NORMAL);
2257 ret = DeleteFile(filepath) ? 0 : 1;
2300 const char *newtail,
2303 char *srcpath, *newpath, *realpath, *dir, *srctail;
2307 PROCNAME(
"lept_mv");
2310 return ERROR_INT(
"srcfile not defined", procName, 1);
2314 return ERROR_INT(
"newdir not NULL or a subdir of /tmp", procName, 1);
2324 if (!newtail || newtail[0] ==
'\0')
2325 newpath =
pathJoin(newtemp, srctail);
2327 newpath =
pathJoin(newtemp, newtail);
2335 LEPT_FREE(realpath);
2342 if (!newtail || newtail[0] ==
'\0')
2349 ret = MoveFileEx(srcpath, newpath,
2350 MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING) ? 0 : 1;
2355 *pnewpath = newpath;
2399 const char *newtail,
2402 char *srcpath, *newpath, *dir, *srctail;
2406 PROCNAME(
"lept_cp");
2409 return ERROR_INT(
"srcfile not defined", procName, 1);
2413 return ERROR_INT(
"newdir not NULL or a subdir of /tmp", procName, 1);
2423 if (!newtail || newtail[0] ==
'\0')
2424 newpath =
pathJoin(newtemp, srctail);
2426 newpath =
pathJoin(newtemp, newtail);
2436 if (!newtail || newtail[0] ==
'\0')
2443 ret = CopyFile(srcpath, newpath, FALSE) ? 0 : 1;
2448 *pnewpath = newpath;
2478 PROCNAME(
"callSystemDebug");
2481 L_ERROR(
"cmd not defined\n", procName);
2484 if (LeptDebugOK == FALSE) {
2485 L_INFO(
"'system' calls are disabled\n", procName);
2489 #if defined (__APPLE__) 2491 #include "TargetConditionals.h" 2492 #if !defined(TARGET_OS_IPHONE) && !defined(OS_IOS) 2495 L_ERROR(
"iOS 11 does not support system()\n", procName);
2544 char *cpathname, *lastslash;
2546 PROCNAME(
"splitPathAtDirectory");
2548 if (!pdir && !ptail)
2549 return ERROR_INT(
"null input for both strings", procName, 1);
2550 if (pdir) *pdir = NULL;
2551 if (ptail) *ptail = NULL;
2553 return ERROR_INT(
"pathname not defined", procName, 1);
2557 lastslash = strrchr(cpathname,
'/');
2562 *(lastslash + 1) =
'\0';
2565 LEPT_FREE(cpathname);
2573 LEPT_FREE(cpathname);
2611 char *tail, *dir, *lastdot;
2614 PROCNAME(
"splitPathExtension");
2616 if (!pbasename && !pextension)
2617 return ERROR_INT(
"null input for both strings", procName, 1);
2618 if (pbasename) *pbasename = NULL;
2619 if (pextension) *pextension = NULL;
2621 return ERROR_INT(
"pathname not defined", procName, 1);
2628 if ((lastdot = strrchr(tail,
'.'))) {
2689 const char *slash =
"/";
2691 l_int32 i, n1, n2, emptydir;
2696 PROCNAME(
"pathJoin");
2700 if (dir && strlen(dir) >= 2 && dir[0] ==
'.' && dir[1] ==
'.')
2701 return (
char *)ERROR_PTR(
"dir starts with '..'", procName, NULL);
2702 if (fname && strlen(fname) >= 2 && fname[0] ==
'.' && fname[1] ==
'.')
2703 return (
char *)ERROR_PTR(
"fname starts with '..'", procName, NULL);
2710 if (dir && strlen(dir) > 0) {
2713 sarraySplitString(sa1, dir,
"/");
2715 for (i = 0; i < n1; i++) {
2723 emptydir = dir && strlen(dir) == 0;
2724 if ((!dir || emptydir) && fname && strlen(fname) > 0 && fname[0] ==
'/')
2728 if (fname && strlen(fname) > 0) {
2729 sarraySplitString(sa2, fname,
"/");
2731 for (i = 0; i < n2; i++) {
2740 if (size > 1 && dest[size - 1] ==
'/')
2741 dest[size - 1] =
'\0';
2766 const char *subdirs)
2769 size_t len1, len2, len3, len4;
2771 PROCNAME(
"appendSubdirs");
2773 if (!basedir || !subdirs)
2774 return (
char *)ERROR_PTR(
"basedir and subdirs not both defined",
2777 len1 = strlen(basedir);
2778 len2 = strlen(subdirs);
2779 len3 = len1 + len2 + 6;
2780 if ((newdir = (
char *)LEPT_CALLOC(len3 + 1, 1)) == NULL)
2781 return (
char *)ERROR_PTR(
"newdir not made", procName, NULL);
2782 strncat(newdir, basedir, len3);
2783 if (newdir[len1 - 1] !=
'/')
2785 if (subdirs[0] ==
'/')
2786 strncat(newdir, subdirs + 1, len3);
2788 strncat(newdir, subdirs, len3);
2789 len4 = strlen(newdir);
2790 if (newdir[len4 - 1] ==
'/')
2791 newdir[len4 - 1] =
'\0';
2823 PROCNAME(
"convertSepCharsInPath");
2825 return ERROR_INT(
"path not defined", procName, 1);
2826 if (type != UNIX_PATH_SEPCHAR && type != WIN_PATH_SEPCHAR)
2827 return ERROR_INT(
"invalid type", procName, 1);
2830 if (type == UNIX_PATH_SEPCHAR) {
2831 for (i = 0; i < len; i++) {
2832 if (path[i] ==
'\\')
2836 for (i = 0; i < len; i++) {
2883 l_int32 is_win32 = FALSE;
2884 char *cdir, *pathout;
2885 l_int32 dirlen, namelen, size;
2887 PROCNAME(
"genPathname");
2890 return (
char *)ERROR_PTR(
"no input", procName, NULL);
2893 if (!dir || dir[0] ==
'\0') {
2894 if ((cdir = getcwd(NULL, 0)) == NULL)
2895 return (
char *)ERROR_PTR(
"no current dir found", procName, NULL);
2903 dirlen = strlen(cdir);
2904 if (cdir[dirlen - 1] ==
'/' && dirlen != 1) {
2905 cdir[dirlen - 1] =
'\0';
2909 namelen = (fname) ? strlen(fname) : 0;
2910 size = dirlen + namelen + 256;
2911 if ((pathout = (
char *)LEPT_CALLOC(size,
sizeof(
char))) == NULL) {
2913 return (
char *)ERROR_PTR(
"pathout not made", procName, NULL);
2924 if (!is_win32 || dirlen < 4 ||
2925 (dirlen == 4 && strncmp(cdir,
"/tmp", 4) != 0) ||
2926 (dirlen > 4 && strncmp(cdir,
"/tmp/", 5) != 0)) {
2931 char tmpdir[MAX_PATH];
2932 GetTempPath(
sizeof(tmpdir), tmpdir);
2933 tmpdirlen = strlen(tmpdir);
2934 if (tmpdirlen > 0 && tmpdir[tmpdirlen - 1] ==
'\\') {
2935 tmpdir[tmpdirlen - 1] =
'\0';
2937 tmpdirlen = strlen(tmpdir);
2947 if (fname && strlen(fname) > 0) {
2948 dirlen = strlen(pathout);
2949 pathout[dirlen] =
'/';
2950 strncat(pathout, fname, namelen);
2994 PROCNAME(
"makeTempDirname");
2997 return ERROR_INT(
"result not defined", procName, 1);
2998 if (subdir && ((subdir[0] ==
'.') || (subdir[0] ==
'/')))
2999 return ERROR_INT(
"subdir not an actual subdirectory", procName, 1);
3001 memset(result, 0, nbytes);
3008 pathlen = strlen(path);
3009 if (pathlen < nbytes - 1) {
3010 strncpy(result, path, pathlen);
3012 L_ERROR(
"result array too small for path\n", procName);
3043 PROCNAME(
"modifyTrailingSlash");
3046 return ERROR_INT(
"path not defined", procName, 1);
3048 return ERROR_INT(
"invalid flag", procName, 1);
3051 lastchar = path[len - 1];
3054 path[len + 1] =
'\0';
3056 path[len - 1] =
'\0';
3090 PROCNAME(
"l_makeTempFilename");
3093 return (
char *)ERROR_PTR(
"failed to make dirname", procName, NULL);
3100 fd = mkstemp(pattern);
3103 return (
char *)ERROR_PTR(
"mkstemp failed", procName, NULL);
3110 char fname[MAX_PATH];
3112 if (GetTempFileName(dirname,
"lp.", 0, fname) == 0)
3113 return (
char *)ERROR_PTR(
"GetTempFileName failed", procName, NULL);
3114 if ((fp = fopen(fname,
"wb")) == NULL)
3115 return (
char *)ERROR_PTR(
"file cannot be written to", procName, NULL);
3146 char *tail, *basename;
3147 l_int32 len, nret, num;
3149 PROCNAME(
"extractNumberFromFilename");
3152 return ERROR_INT(
"fname not defined", procName, -1);
3158 len = strlen(basename);
3159 if (numpre + numpost > len - 1) {
3160 LEPT_FREE(basename);
3161 return ERROR_INT(
"numpre + numpost too big", procName, -1);
3164 basename[len - numpost] =
'\0';
3165 nret = sscanf(basename + numpre,
"%d", &num);
3166 LEPT_FREE(basename);
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_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()
void l_dnaDestroy(L_DNA **pda)
l_dnaDestroy()
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 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()
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_uint8 * l_binaryCopy(l_uint8 *datas, size_t size)
l_binaryCopy()
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()
void * reallocNew(void **pindata, l_int32 oldsize, l_int32 newsize)
reallocNew()
l_ok modifyTrailingSlash(char *path, size_t nbytes, l_int32 flag)
modifyTrailingSlash()
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()
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()
FILE * fopenWriteWinTempfile()
fopenWriteWinTempfile()
char * sarrayGetString(SARRAY *sa, l_int32 index, l_int32 copyflag)
sarrayGetString()
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 * 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 bbufferDestroy(L_BBUFFER **pbb)
bbufferDestroy()
FILE * fopenWriteStream(const char *filename, const char *modestring)
fopenWriteStream()
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()
char * l_makeTempFilename()
l_makeTempFilename()
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()
char * stringReplaceSubstr(const char *src, const char *sub1, const char *sub2, l_int32 *pfound, l_int32 *ploc)
stringReplaceSubstr()
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()