163 #include "config_auto.h" 177 #include <sys/stat.h> 179 #include <sys/stat.h> 180 #include <sys/types.h> 185 #include "allheaders.h" 189 #define DEBUG_REWRITE 0 207 PROCNAME(
"stringNew");
210 L_WARNING(
"src not defined\n", procName);
215 if ((dest = (
char *)LEPT_CALLOC(len + 1,
sizeof(
char))) == NULL)
216 return (
char *)ERROR_PTR(
"dest not made", procName, NULL);
248 PROCNAME(
"stringCopy");
251 return ERROR_INT(
"dest not defined", procName, 1);
256 for (i = 0; i < n && src[i] !=
'\0'; i++)
282 PROCNAME(
"stringReplace");
285 return ERROR_INT(
"pdest not defined", procName, 1);
320 PROCNAME(
"stringLength");
323 return ERROR_INT(
"src not defined", procName, 0);
327 for (i = 0; i < size; i++) {
361 l_int32 lendest, lensrc;
363 PROCNAME(
"stringCat");
366 return ERROR_INT(
"dest not defined", procName, -1);
368 return ERROR_INT(
"size < 1; too small", procName, -1);
374 return ERROR_INT(
"no terminating nul byte", procName, -1);
378 n = (lendest + lensrc > size - 1 ? size - lendest - 1 : lensrc);
380 return ERROR_INT(
"dest too small for append", procName, -1);
382 for (i = 0; i < n; i++)
383 dest[lendest + i] = src[i];
384 dest[lendest + n] =
'\0';
411 if (!first)
return NULL;
414 va_start(args, first);
416 while ((arg = va_arg(args,
const char *)) != NULL)
419 result = (
char *)LEPT_CALLOC(len + 1,
sizeof(
char));
422 va_start(args, first);
427 while ((arg = va_arg(args,
const char *)) != NULL) {
455 l_int32 srclen1, srclen2, destlen;
457 PROCNAME(
"stringJoin");
459 srclen1 = (src1) ? strlen(src1) : 0;
460 srclen2 = (src2) ? strlen(src2) : 0;
461 destlen = srclen1 + srclen2 + 3;
463 if ((dest = (
char *)LEPT_CALLOC(destlen,
sizeof(
char))) == NULL)
464 return (
char *)ERROR_PTR(
"calloc fail for dest", procName, NULL);
469 strncat(dest, src2, srclen2);
511 PROCNAME(
"stringJoinIP");
514 return ERROR_INT(
"&src1 not defined", procName, 1);
535 PROCNAME(
"stringReverse");
538 return (
char *)ERROR_PTR(
"src not defined", procName, NULL);
540 if ((dest = (
char *)LEPT_CALLOC(len + 1,
sizeof(
char))) == NULL)
541 return (
char *)ERROR_PTR(
"calloc fail for dest", procName, NULL);
542 for (i = 0; i < len; i++)
543 dest[i] = src[len - 1 - i];
587 char *start, *substr;
588 l_int32 istart, i, j, nchars;
590 PROCNAME(
"strtokSafe");
593 return (
char *)ERROR_PTR(
"seps not defined", procName, NULL);
595 return (
char *)ERROR_PTR(
"&saveptr not defined", procName, NULL);
609 for (istart = 0;; istart++) {
610 if ((nextc = start[istart]) ==
'\0') {
614 if (!strchr(seps, nextc))
621 for (i = istart;; i++) {
622 if ((nextc = start[i]) ==
'\0')
624 if (strchr(seps, nextc))
630 substr = (
char *)LEPT_CALLOC(nchars + 1,
sizeof(
char));
636 if ((nextc = start[j]) ==
'\0') {
640 if (!strchr(seps, nextc)) {
641 *psaveptr = start + j;
683 PROCNAME(
"stringSplitOnToken");
686 return ERROR_INT(
"&head not defined", procName, 1);
688 return ERROR_INT(
"&tail not defined", procName, 1);
689 *phead = *ptail = NULL;
691 return ERROR_INT(
"cstr not defined", procName, 1);
693 return ERROR_INT(
"seps not defined", procName, 1);
727 PROCNAME(
"stringCheckForChars");
730 return ERROR_INT(
"&found not defined", procName, 1);
733 return ERROR_INT(
"src and chars not both defined", procName, 1);
736 for (i = 0; i < n; i++) {
738 if (strchr(chars, ch)) {
756 const char *remchars)
762 PROCNAME(
"stringRemoveChars");
765 return (
char *)ERROR_PTR(
"src not defined", procName, NULL);
769 if ((dest = (
char *)LEPT_CALLOC(strlen(src) + 1,
sizeof(
char))) == NULL)
770 return (
char *)ERROR_PTR(
"dest not made", procName, NULL);
772 for (i = 0, k = 0; i < nsrc; i++) {
774 if (!strchr(remchars, ch))
806 PROCNAME(
"stringFindSubstr");
809 return ERROR_INT(
"src not defined", procName, 0);
811 return ERROR_INT(
"sub not defined", procName, 0);
812 if (ploc) *ploc = -1;
813 if (strlen(sub) == 0)
814 return ERROR_INT(
"substring length 0", procName, 0);
815 if (strlen(src) == 0)
818 if ((ptr = (
char *)strstr(src, sub)) == NULL)
859 l_int32 nsrc, nsub1, nsub2, len, npre, loc;
861 PROCNAME(
"stringReplaceSubstr");
864 return (
char *)ERROR_PTR(
"src not defined", procName, NULL);
866 return (
char *)ERROR_PTR(
"sub1 not defined", procName, NULL);
868 return (
char *)ERROR_PTR(
"sub2 not defined", procName, NULL);
876 if ((ptr = (
char *)strstr(src + loc, sub1)) == NULL) {
883 nsub1 = strlen(sub1);
884 nsub2 = strlen(sub2);
885 len = nsrc + nsub2 - nsub1;
886 if ((dest = (
char *)LEPT_CALLOC(len + 1,
sizeof(
char))) == NULL)
887 return (
char *)ERROR_PTR(
"dest not made", procName, NULL);
889 memcpy(dest, src, npre);
890 strcpy(dest + npre, sub2);
891 strcpy(dest + npre + nsub2, ptr + nsub1);
893 *ploc = npre + nsub2;
923 char *currstr, *newstr;
926 PROCNAME(
"stringReplaceEachSubstr");
928 if (pcount) *pcount = 0;
930 return (
char *)ERROR_PTR(
"src not defined", procName, NULL);
932 return (
char *)ERROR_PTR(
"sub1 not defined", procName, NULL);
934 return (
char *)ERROR_PTR(
"sub2 not defined", procName, NULL);
975 const l_uint8 *sequence,
978 l_int32 start, offset, realoffset, found;
981 PROCNAME(
"arrayFindEachSequence");
983 if (!data || !sequence)
984 return (
L_DNA *)ERROR_PTR(
"data & sequence not both defined",
995 realoffset = start + offset;
997 start = realoffset + seqlen;
998 if (start >= datalen)
1035 const l_uint8 *sequence,
1040 l_int32 i, j, found, lastpos;
1042 PROCNAME(
"arrayFindSequence");
1044 if (poffset) *poffset = 0;
1045 if (pfound) *pfound = FALSE;
1046 if (!data || !sequence)
1047 return ERROR_INT(
"data & sequence not both defined", procName, 1);
1048 if (!poffset || !pfound)
1049 return ERROR_INT(
"&offset and &found not defined", procName, 1);
1051 lastpos = datalen - seqlen + 1;
1053 for (i = 0; i < lastpos; i++) {
1054 for (j = 0; j < seqlen; j++) {
1055 if (data[i + j] != sequence[j])
1057 if (j == seqlen - 1)
1064 if (found == TRUE) {
1110 PROCNAME(
"reallocNew");
1113 return ERROR_PTR(
"input data not defined", procName, NULL);
1125 if ((newdata = (
void *)LEPT_CALLOC(1, newsize)) == NULL)
1126 return ERROR_PTR(
"newdata not made", procName, NULL);
1131 if ((newdata = (
void *)LEPT_CALLOC(1, newsize)) == NULL)
1132 return ERROR_PTR(
"newdata not made", procName, NULL);
1133 minsize = L_MIN(oldsize, newsize);
1134 memcpy((
char *)newdata, (
char *)indata, minsize);
1160 PROCNAME(
"l_binaryRead");
1163 return (l_uint8 *)ERROR_PTR(
"pnbytes not defined", procName, NULL);
1166 return (l_uint8 *)ERROR_PTR(
"filename not defined", procName, NULL);
1169 return (l_uint8 *)ERROR_PTR(
"file stream not opened", procName, NULL);
1204 l_int32 seekable, navail, nadd, nread;
1207 PROCNAME(
"l_binaryReadStream");
1210 return (l_uint8 *)ERROR_PTR(
"&nbytes not defined", procName, NULL);
1213 return (l_uint8 *)ERROR_PTR(
"fp not defined", procName, NULL);
1219 seekable = (ftell(fp) == 0) ? 1 : 0;
1228 if (navail < 4096) {
1229 nadd = L_MAX(bb->
nalloc, 4096);
1232 nread = fread((
void *)(bb->
array + bb->
n), 1, 4096, fp);
1234 if (nread != 4096)
break;
1239 if ((data = (l_uint8 *)LEPT_CALLOC(bb->
n + 1,
sizeof(l_uint8))) != NULL) {
1240 memcpy(data, bb->
array, bb->
n);
1243 L_ERROR(
"calloc fail for data\n", procName);
1275 PROCNAME(
"l_binaryReadSelect");
1278 return (l_uint8 *)ERROR_PTR(
"pnread not defined", procName, NULL);
1281 return (l_uint8 *)ERROR_PTR(
"filename not defined", procName, NULL);
1284 return (l_uint8 *)ERROR_PTR(
"file stream not opened", procName, NULL);
1318 size_t bytesleft, bytestoread, nread, filebytes;
1320 PROCNAME(
"l_binaryReadSelectStream");
1323 return (l_uint8 *)ERROR_PTR(
"&nread not defined", procName, NULL);
1326 return (l_uint8 *)ERROR_PTR(
"stream not defined", procName, NULL);
1329 fseek(fp, 0, SEEK_END);
1330 filebytes = ftell(fp);
1331 fseek(fp, 0, SEEK_SET);
1332 if (start > filebytes) {
1333 L_ERROR(
"start = %lu but filebytes = %lu\n", procName,
1334 (
unsigned long)start, (
unsigned long)filebytes);
1338 return (l_uint8 *)LEPT_CALLOC(1, 1);
1339 bytesleft = filebytes - start;
1340 if (nbytes == 0) nbytes = bytesleft;
1341 bytestoread = (bytesleft >= nbytes) ? nbytes : bytesleft;
1344 if ((data = (l_uint8 *)LEPT_CALLOC(1, bytestoread + 1)) == NULL)
1345 return (l_uint8 *)ERROR_PTR(
"calloc fail for data", procName, NULL);
1346 fseek(fp, start, SEEK_SET);
1347 nread = fread(data, 1, bytestoread, fp);
1348 if (nbytes != nread)
1349 L_INFO(
"%lu bytes requested; %lu bytes read\n", procName,
1350 (
unsigned long)nbytes, (
unsigned long)nread);
1352 fseek(fp, 0, SEEK_SET);
1368 const char *operation,
1372 char actualOperation[20];
1375 PROCNAME(
"l_binaryWrite");
1378 return ERROR_INT(
"filename not defined", procName, 1);
1380 return ERROR_INT(
"operation not defined", procName, 1);
1382 return ERROR_INT(
"data not defined", procName, 1);
1384 return ERROR_INT(
"nbytes must be > 0", procName, 1);
1386 if (strcmp(operation,
"w") && strcmp(operation,
"a"))
1387 return ERROR_INT(
"operation not one of {'w','a'}", procName, 1);
1392 strncat(actualOperation,
"b", 2);
1395 return ERROR_INT(
"stream not opened", procName, 1);
1396 fwrite(data, 1, nbytes, fp);
1414 PROCNAME(
"nbytesInFile");
1417 return ERROR_INT(
"filename not defined", procName, 0);
1419 return ERROR_INT(
"stream not opened", procName, 0);
1435 l_int64 pos, nbytes;
1437 PROCNAME(
"fnbytesInFile");
1440 return ERROR_INT(
"stream not open", procName, 0);
1443 fseek(fp, 0, SEEK_END);
1445 fseek(fp, pos, SEEK_SET);
1474 PROCNAME(
"l_binaryCopy");
1477 return (l_uint8 *)ERROR_PTR(
"datas not defined", procName, NULL);
1479 if ((datad = (l_uint8 *)LEPT_CALLOC(size + 4,
sizeof(l_uint8))) == NULL)
1480 return (l_uint8 *)ERROR_PTR(
"datad not made", procName, NULL);
1481 memcpy(datad, datas, size);
1498 const char *newfile)
1504 PROCNAME(
"fileCopy");
1507 return ERROR_INT(
"srcfile not defined", procName, 1);
1509 return ERROR_INT(
"newfile not defined", procName, 1);
1512 return ERROR_INT(
"data not returned", procName, 1);
1528 const char *destfile)
1533 PROCNAME(
"fileConcatenate");
1536 return ERROR_INT(
"srcfile not defined", procName, 1);
1538 return ERROR_INT(
"destfile not defined", procName, 1);
1560 PROCNAME(
"fileAppendString");
1563 return ERROR_INT(
"filename not defined", procName, 1);
1565 return ERROR_INT(
"str not defined", procName, 1);
1568 return ERROR_INT(
"stream not opened", procName, 1);
1569 fprintf(fp,
"%s", str);
1598 PROCNAME(
"fopenReadStream");
1601 return (FILE *)ERROR_PTR(
"filename not defined", procName, NULL);
1605 fp = fopen(fname,
"rb");
1611 fp = fopen(tail,
"rb");
1615 return (FILE *)ERROR_PTR(
"file not found", procName, NULL);
1637 const char *modestring)
1642 PROCNAME(
"fopenWriteStream");
1645 return (FILE *)ERROR_PTR(
"filename not defined", procName, NULL);
1648 fp = fopen(fname, modestring);
1651 return (FILE *)ERROR_PTR(
"stream not opened", procName, NULL);
1675 PROCNAME(
"fopenReadFromMemory");
1678 return (FILE *)ERROR_PTR(
"data not defined", procName, NULL);
1681 if ((fp = fmemopen((
void *)data, size,
"rb")) == NULL)
1682 return (FILE *)ERROR_PTR(
"stream not opened", procName, NULL);
1684 L_INFO(
"work-around: writing to a temp file\n", procName);
1687 return (FILE *)ERROR_PTR(
"tmpfile stream not opened", procName, NULL);
1689 if ((fp = tmpfile()) == NULL)
1690 return (FILE *)ERROR_PTR(
"tmpfile stream not opened", procName, NULL);
1692 fwrite(data, 1, size, fp);
1723 PROCNAME(
"fopenWriteWinTempfile");
1725 if ((filename = l_makeTempFilename()) == NULL) {
1726 L_ERROR(
"l_makeTempFilename failed, %s\n", procName, strerror(errno));
1730 handle = _open(filename, _O_CREAT | _O_RDWR | _O_SHORT_LIVED |
1731 _O_TEMPORARY | _O_BINARY, _S_IREAD | _S_IWRITE);
1734 L_ERROR(
"_open failed, %s\n", procName, strerror(errno));
1738 if ((fp = _fdopen(handle,
"r+b")) == NULL) {
1739 L_ERROR(
"_fdopen failed, %s\n", procName, strerror(errno));
1777 PROCNAME(
"lept_fopen");
1780 return (FILE *)ERROR_PTR(
"filename not defined", procName, NULL);
1782 return (FILE *)ERROR_PTR(
"mode not defined", procName, NULL);
1806 PROCNAME(
"lept_fclose");
1809 return ERROR_INT(
"stream not defined", procName, 1);
1833 if (nmemb <= 0 || size <= 0)
1835 return LEPT_CALLOC(nmemb, size);
1887 l_uint32 attributes;
1890 PROCNAME(
"lept_mkdir");
1893 return ERROR_INT(
"subdir not defined", procName, 1);
1894 if ((strlen(subdir) == 0) || (subdir[0] ==
'.') || (subdir[0] ==
'/'))
1895 return ERROR_INT(
"subdir not an actual subdirectory", procName, 1);
1898 sarraySplitString(sa, subdir,
"/");
1903 ret = mkdir(dir, 0777);
1905 attributes = GetFileAttributes(dir);
1906 if (attributes == INVALID_FILE_ATTRIBUTES)
1907 ret = (CreateDirectory(dir, NULL) ? 0 : 1);
1910 for (i = 0; i < n; i++) {
1913 ret += mkdir(tmpdir, 0777);
1915 if (CreateDirectory(tmpdir, NULL) == 0)
1916 ret += (GetLastError () != ERROR_ALREADY_EXISTS);
1924 L_ERROR(
"failure to create %d directories\n", procName, ret);
1953 char *dir, *realdir, *fname, *fullname;
1954 l_int32 exists, ret, i, nfiles;
1960 PROCNAME(
"lept_rmdir");
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);
1970 return ERROR_INT(
"directory name not made", procName, 1);
1979 L_ERROR(
"directory %s does not exist!\n", procName, dir);
1985 for (i = 0; i < nfiles; i++) {
1989 LEPT_FREE(fullname);
1994 ret = rmdir(realdir);
1998 ret = (RemoveDirectory(newpath) ? 0 : 1);
2030 if (!pexists)
return;
2039 l_int32 err = stat(realdir, &s);
2040 if (err != -1 && S_ISDIR(s.st_mode))
2044 l_uint32 attributes;
2045 attributes = GetFileAttributes(realdir);
2046 if (attributes != INVALID_FILE_ATTRIBUTES &&
2047 (attributes & FILE_ATTRIBUTE_DIRECTORY)) {
2091 PROCNAME(
"lept_rm_match");
2095 return ERROR_INT(
"sa not made", procName, -1);
2098 L_WARNING(
"no matching files found\n", procName);
2104 for (i = 0; i < n; i++) {
2108 L_ERROR(
"failed to remove %s\n", procName, path);
2140 PROCNAME(
"lept_rm");
2142 if (!tail || strlen(tail) == 0)
2143 return ERROR_INT(
"tail undefined or empty", procName, 1);
2146 return ERROR_INT(
"temp dirname not made", procName, 1);
2177 PROCNAME(
"lept_rmfile");
2179 if (!filepath || strlen(filepath) == 0)
2180 return ERROR_INT(
"filepath undefined or empty", procName, 1);
2183 ret =
remove(filepath);
2186 SetFileAttributes(filepath, FILE_ATTRIBUTE_NORMAL);
2187 ret = DeleteFile(filepath) ? 0 : 1;
2230 const char *newtail,
2233 char *srcpath, *newpath, *realpath, *dir, *srctail;
2237 PROCNAME(
"lept_mv");
2240 return ERROR_INT(
"srcfile not defined", procName, 1);
2244 return ERROR_INT(
"newdir not NULL or a subdir of /tmp", procName, 1);
2254 if (!newtail || newtail[0] ==
'\0')
2255 newpath =
pathJoin(newtemp, srctail);
2257 newpath =
pathJoin(newtemp, newtail);
2265 LEPT_FREE(realpath);
2272 if (!newtail || newtail[0] ==
'\0')
2279 ret = MoveFileEx(srcpath, newpath,
2280 MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING) ? 0 : 1;
2285 *pnewpath = newpath;
2329 const char *newtail,
2332 char *srcpath, *newpath, *dir, *srctail;
2336 PROCNAME(
"lept_cp");
2339 return ERROR_INT(
"srcfile not defined", procName, 1);
2343 return ERROR_INT(
"newdir not NULL or a subdir of /tmp", procName, 1);
2353 if (!newtail || newtail[0] ==
'\0')
2354 newpath =
pathJoin(newtemp, srctail);
2356 newpath =
pathJoin(newtemp, newtail);
2366 if (!newtail || newtail[0] ==
'\0')
2373 ret = CopyFile(srcpath, newpath, FALSE) ? 0 : 1;
2378 *pnewpath = newpath;
2423 char *cpathname, *lastslash;
2425 PROCNAME(
"splitPathAtDirectory");
2427 if (!pdir && !ptail)
2428 return ERROR_INT(
"null input for both strings", procName, 1);
2429 if (pdir) *pdir = NULL;
2430 if (ptail) *ptail = NULL;
2432 return ERROR_INT(
"pathname not defined", procName, 1);
2436 lastslash = strrchr(cpathname,
'/');
2441 *(lastslash + 1) =
'\0';
2444 LEPT_FREE(cpathname);
2452 LEPT_FREE(cpathname);
2490 char *tail, *dir, *lastdot;
2493 PROCNAME(
"splitPathExtension");
2495 if (!pbasename && !pextension)
2496 return ERROR_INT(
"null input for both strings", procName, 1);
2497 if (pbasename) *pbasename = NULL;
2498 if (pextension) *pextension = NULL;
2500 return ERROR_INT(
"pathname not defined", procName, 1);
2507 if ((lastdot = strrchr(tail,
'.'))) {
2568 char *slash = (
char *)
"/";
2570 l_int32 i, n1, n2, emptydir;
2575 PROCNAME(
"pathJoin");
2579 if (dir && strlen(dir) >= 2 && dir[0] ==
'.' && dir[1] ==
'.')
2580 return (
char *)ERROR_PTR(
"dir starts with '..'", procName, NULL);
2581 if (fname && strlen(fname) >= 2 && fname[0] ==
'.' && fname[1] ==
'.')
2582 return (
char *)ERROR_PTR(
"fname starts with '..'", procName, NULL);
2589 if (dir && strlen(dir) > 0) {
2592 sarraySplitString(sa1, dir,
"/");
2594 for (i = 0; i < n1; i++) {
2602 emptydir = dir && strlen(dir) == 0;
2603 if ((!dir || emptydir) && fname && strlen(fname) > 0 && fname[0] ==
'/')
2607 if (fname && strlen(fname) > 0) {
2608 sarraySplitString(sa2, fname,
"/");
2610 for (i = 0; i < n2; i++) {
2619 if (size > 1 && dest[size - 1] ==
'/')
2620 dest[size - 1] =
'\0';
2645 const char *subdirs)
2648 size_t len1, len2, len3, len4;
2650 PROCNAME(
"appendSubdirs");
2652 if (!basedir || !subdirs)
2653 return (
char *)ERROR_PTR(
"basedir and subdirs not both defined",
2656 len1 = strlen(basedir);
2657 len2 = strlen(subdirs);
2658 len3 = len1 + len2 + 6;
2659 if ((newdir = (
char *)LEPT_CALLOC(len3 + 1, 1)) == NULL)
2660 return (
char *)ERROR_PTR(
"newdir not made", procName, NULL);
2661 strncat(newdir, basedir, len3);
2662 if (newdir[len1 - 1] !=
'/')
2664 if (subdirs[0] ==
'/')
2665 strncat(newdir, subdirs + 1, len3);
2667 strncat(newdir, subdirs, len3);
2668 len4 = strlen(newdir);
2669 if (newdir[len4 - 1] ==
'/')
2670 newdir[len4 - 1] =
'\0';
2702 PROCNAME(
"convertSepCharsInPath");
2704 return ERROR_INT(
"path not defined", procName, 1);
2705 if (type != UNIX_PATH_SEPCHAR && type != WIN_PATH_SEPCHAR)
2706 return ERROR_INT(
"invalid type", procName, 1);
2709 if (type == UNIX_PATH_SEPCHAR) {
2710 for (i = 0; i < len; i++) {
2711 if (path[i] ==
'\\')
2715 for (i = 0; i < len; i++) {
2762 l_int32 is_win32 = FALSE;
2763 char *cdir, *pathout;
2764 l_int32 dirlen, namelen, size;
2766 PROCNAME(
"genPathname");
2769 return (
char *)ERROR_PTR(
"no input", procName, NULL);
2772 if (!dir || dir[0] ==
'\0') {
2773 if ((cdir = getcwd(NULL, 0)) == NULL)
2774 return (
char *)ERROR_PTR(
"no current dir found", procName, NULL);
2782 dirlen = strlen(cdir);
2783 if (cdir[dirlen - 1] ==
'/' && dirlen != 1) {
2784 cdir[dirlen - 1] =
'\0';
2788 namelen = (fname) ? strlen(fname) : 0;
2789 size = dirlen + namelen + 256;
2790 if ((pathout = (
char *)LEPT_CALLOC(size,
sizeof(
char))) == NULL) {
2792 return (
char *)ERROR_PTR(
"pathout not made", procName, NULL);
2803 if (!is_win32 || dirlen < 4 ||
2804 (dirlen == 4 && strncmp(cdir,
"/tmp", 4) != 0) ||
2805 (dirlen > 4 && strncmp(cdir,
"/tmp/", 5) != 0)) {
2810 char tmpdir[MAX_PATH];
2811 GetTempPath(
sizeof(tmpdir), tmpdir);
2812 tmpdirlen = strlen(tmpdir);
2813 if (tmpdirlen > 0 && tmpdir[tmpdirlen - 1] ==
'\\') {
2814 tmpdir[tmpdirlen - 1] =
'\0';
2816 tmpdirlen = strlen(tmpdir);
2826 if (fname && strlen(fname) > 0) {
2827 dirlen = strlen(pathout);
2828 pathout[dirlen] =
'/';
2829 strncat(pathout, fname, namelen);
2873 PROCNAME(
"makeTempDirname");
2876 return ERROR_INT(
"result not defined", procName, 1);
2877 if (subdir && ((subdir[0] ==
'.') || (subdir[0] ==
'/')))
2878 return ERROR_INT(
"subdir not an actual subdirectory", procName, 1);
2880 memset(result, 0, nbytes);
2887 pathlen = strlen(path);
2888 if (pathlen < nbytes - 1) {
2889 strncpy(result, path, pathlen);
2891 L_ERROR(
"result array too small for path\n", procName);
2922 PROCNAME(
"modifyTrailingSlash");
2925 return ERROR_INT(
"path not defined", procName, 1);
2927 return ERROR_INT(
"invalid flag", procName, 1);
2930 lastchar = path[len - 1];
2933 path[len + 1] =
'\0';
2935 path[len - 1] =
'\0';
2964 l_makeTempFilename()
2968 PROCNAME(
"l_makeTempFilename");
2971 return (
char *)ERROR_PTR(
"failed to make dirname", procName, NULL);
2978 fd = mkstemp(pattern);
2981 return (
char *)ERROR_PTR(
"mkstemp failed", procName, NULL);
2988 char fname[MAX_PATH];
2990 if (GetTempFileName(dirname,
"lp.", 0, fname) == 0)
2991 return (
char *)ERROR_PTR(
"GetTempFileName failed", procName, NULL);
2992 if ((fp = fopen(fname,
"wb")) == NULL)
2993 return (
char *)ERROR_PTR(
"file cannot be written to", procName, NULL);
3024 char *tail, *basename;
3025 l_int32 len, nret, num;
3027 PROCNAME(
"extractNumberFromFilename");
3030 return ERROR_INT(
"fname not defined", procName, -1);
3036 len = strlen(basename);
3037 if (numpre + numpost > len - 1) {
3038 LEPT_FREE(basename);
3039 return ERROR_INT(
"numpre + numpost too big", procName, -1);
3042 basename[len - numpost] =
'\0';
3043 nret = sscanf(basename + numpre,
"%d", &num);
3044 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_int32 l_dnaGetCount(L_DNA *da)
l_dnaGetCount()
l_int32 stringLength(const char *src, size_t size)
stringLength()
l_int32 stringCopy(char *dest, const char *src, l_int32 n)
stringCopy()
l_int32 stringReplace(char **pdest, const char *src)
stringReplace()
char * genPathname(const char *dir, const char *fname)
genPathname()
l_int32 l_dnaAddNumber(L_DNA *da, l_float64 val)
l_dnaAddNumber()
l_int32 stringSplitOnToken(char *cstr, const char *seps, char **phead, char **ptail)
stringSplitOnToken()
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_int32 fileCopy(const char *srcfile, const char *newfile)
fileCopy()
char * stringNew(const char *src)
stringNew()
l_int32 modifyTrailingSlash(char *path, size_t nbytes, l_int32 flag)
modifyTrailingSlash()
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_int32 splitPathAtDirectory(const char *pathname, char **pdir, char **ptail)
splitPathAtDirectory()
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()
void * reallocNew(void **pindata, l_int32 oldsize, l_int32 newsize)
reallocNew()
l_int32 lept_fclose(FILE *fp)
lept_fclose()
l_int32 stringCat(char *dest, size_t size, const char *src)
stringCat()
l_uint8 * l_binaryRead(const char *filename, size_t *pnbytes)
l_binaryRead()
l_int32 splitPathAtExtension(const char *pathname, char **pbasename, char **pextension)
splitPathAtExtension()
l_int32 stringJoinIP(char **psrc1, const char *src2)
stringJoinIP()
l_int32 fileAppendString(const char *filename, const char *str)
fileAppendString()
l_int32 l_byteaAppendString(L_BYTEA *ba, char *str)
l_byteaAppendString()
void lept_free(void *ptr)
lept_free()
size_t fnbytesInFile(FILE *fp)
fnbytesInFile()
l_int32 bbufferExtendArray(L_BBUFFER *bb, l_int32 nbytes)
bbufferExtendArray()
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()
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)
l_makeTempFilename()
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 stringCheckForChars(const char *src, const char *chars, l_int32 *pfound)
stringCheckForChars()
l_int32 makeTempDirname(char *result, size_t nbytes, const char *subdir)
makeTempDirname()
l_int32 sarrayGetCount(SARRAY *sa)
sarrayGetCount()
FILE * lept_fopen(const char *filename, const char *mode)
lept_fopen()
l_int32 l_binaryWrite(const char *filename, const char *operation, void *data, size_t nbytes)
l_binaryWrite()
l_int32 convertSepCharsInPath(char *path, l_int32 type)
convertSepCharsInPath()
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()
char * strtokSafe(char *cstr, const char *seps, char **psaveptr)
strtokSafe()
void l_byteaDestroy(L_BYTEA **pba)
l_byteaDestroy()
l_int32 fileConcatenate(const char *srcfile, const char *destfile)
fileConcatenate()
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_BBUFFER * bbufferCreate(l_uint8 *indata, l_int32 nalloc)
bbufferCreate()
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_int32 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()