135 #include <sys/stat.h> 137 #include "allheaders.h" 139 static const l_int32 INITIAL_PTR_ARRAYSIZE = 50;
161 PROCNAME(
"sarrayCreate");
164 n = INITIAL_PTR_ARRAYSIZE;
167 if ((sa->
array = (
char **)LEPT_CALLOC(n,
sizeof(
char *))) == NULL) {
169 return (
SARRAY *)ERROR_PTR(
"ptr array not made", procName, NULL);
193 PROCNAME(
"sarrayCreateInitialized");
196 return (
SARRAY *)ERROR_PTR(
"n must be > 0", procName, NULL);
198 return (
SARRAY *)ERROR_PTR(
"initstr not defined", procName, NULL);
201 for (i = 0; i < n; i++)
222 char separators[] =
" \n\t";
223 l_int32 i, nsub, size, inword;
226 PROCNAME(
"sarrayCreateWordsFromString");
229 return (
SARRAY *)ERROR_PTR(
"textstr not defined", procName, NULL);
232 size = strlen(
string);
235 for (i = 0; i < size; i++) {
236 if (inword == FALSE &&
237 (
string[i] !=
' ' &&
string[i] !=
'\t' &&
string[i] !=
'\n')) {
240 }
else if (inword == TRUE &&
241 (
string[i] ==
' ' ||
string[i] ==
'\t' ||
string[i] ==
'\n')) {
247 return (
SARRAY *)ERROR_PTR(
"sa not made", procName, NULL);
248 sarraySplitString(sa,
string, separators);
273 l_int32 i, nsub, size, startptr;
274 char *cstring, *substring;
277 PROCNAME(
"sarrayCreateLinesFromString");
280 return (
SARRAY *)ERROR_PTR(
"textstr not defined", procName, NULL);
283 size = strlen(
string);
285 for (i = 0; i < size; i++) {
286 if (
string[i] ==
'\n')
291 return (
SARRAY *)ERROR_PTR(
"sa not made", procName, NULL);
295 if ((cstring =
stringNew(
string)) == NULL) {
297 return (
SARRAY *)ERROR_PTR(
"cstring not made", procName, NULL);
301 for (i = 0; i < size; i++) {
302 if (cstring[i] ==
'\n') {
304 if (i > 0 && cstring[i - 1] ==
'\r')
305 cstring[i - 1] =
'\0';
306 if ((substring =
stringNew(cstring + startptr)) == NULL) {
309 return (
SARRAY *)ERROR_PTR(
"substring not made",
317 if (startptr < size) {
318 if ((substring =
stringNew(cstring + startptr)) == NULL) {
321 return (
SARRAY *)ERROR_PTR(
"substring not made",
329 sarraySplitString(sa,
string,
"\r\n");
354 PROCNAME(
"sarrayDestroy");
357 L_WARNING(
"ptr address is NULL!\n", procName);
360 if ((sa = *psa) == NULL)
366 for (i = 0; i < sa->
n; i++) {
368 LEPT_FREE(sa->
array[i]);
370 LEPT_FREE(sa->
array);
392 PROCNAME(
"sarrayCopy");
395 return (
SARRAY *)ERROR_PTR(
"sa not defined", procName, NULL);
398 return (
SARRAY *)ERROR_PTR(
"csa not made", procName, NULL);
400 for (i = 0; i < sa->
n; i++)
416 PROCNAME(
"sarrayClone");
419 return (
SARRAY *)ERROR_PTR(
"sa not defined", procName, NULL);
445 PROCNAME(
"sarrayAddString");
448 return ERROR_INT(
"sa not defined", procName, 1);
450 return ERROR_INT(
"string not defined", procName, 1);
452 return ERROR_INT(
"invalid copyflag", procName, 1);
459 sa->
array[n] = string;
477 PROCNAME(
"sarrayExtendArray");
480 return ERROR_INT(
"sa not defined", procName, 1);
483 sizeof(
char *) * sa->
nalloc,
484 2 *
sizeof(
char *) * sa->
nalloc)) == NULL)
485 return ERROR_INT(
"new ptr array not returned", procName, 1);
505 l_int32 i, n, nalloc;
507 PROCNAME(
"sarrayRemoveString");
510 return (
char *)ERROR_PTR(
"sa not defined", procName, NULL);
513 return (
char *)ERROR_PTR(
"array not returned", procName, NULL);
515 if (index < 0 || index >= n)
516 return (
char *)ERROR_PTR(
"array index out of bounds", procName, NULL);
518 string = array[
index];
524 for (i =
index; i < n - 1; i++)
525 array[i] = array[i + 1];
559 PROCNAME(
"sarrayReplaceString");
562 return ERROR_INT(
"sa not defined", procName, 1);
564 if (index < 0 || index >= n)
565 return ERROR_INT(
"array index out of bounds", procName, 1);
567 return ERROR_INT(
"newstr not defined", procName, 1);
569 return ERROR_INT(
"invalid copyflag", procName, 1);
592 PROCNAME(
"sarrayClear");
595 return ERROR_INT(
"sa not defined", procName, 1);
596 for (i = 0; i < sa->
n; i++) {
597 LEPT_FREE(sa->
array[i]);
617 PROCNAME(
"sarrayGetCount");
620 return ERROR_INT(
"sa not defined", procName, 0);
646 PROCNAME(
"sarrayGetArray");
649 return (
char **)ERROR_PTR(
"sa not defined", procName, NULL);
652 if (pnalloc) *pnalloc = sa->
nalloc;
679 PROCNAME(
"sarrayGetString");
682 return (
char *)ERROR_PTR(
"sa not defined", procName, NULL);
683 if (index < 0 || index >= sa->
n)
684 return (
char *)ERROR_PTR(
"index not valid", procName, NULL);
686 return (
char *)ERROR_PTR(
"invalid copyflag", procName, NULL);
704 PROCNAME(
"sarrayGetRefcount");
707 return ERROR_INT(
"sa not defined", procName, UNDEF);
723 PROCNAME(
"sarrayChangeRefcount");
726 return ERROR_INT(
"sa not defined", procName, UNDEF);
760 PROCNAME(
"sarrayToString");
763 return (
char *)ERROR_PTR(
"sa not defined", procName, NULL);
797 char *dest, *src, *str;
798 l_int32 n, i, last, size,
index, len;
800 PROCNAME(
"sarrayToStringRange");
803 return (
char *)ERROR_PTR(
"sa not defined", procName, NULL);
804 if (addnlflag != 0 && addnlflag != 1 && addnlflag != 2)
805 return (
char *)ERROR_PTR(
"invalid addnlflag", procName, NULL);
819 return (
char *)ERROR_PTR(
"first not valid", procName, NULL);
823 if (first < 0 || first >= n)
824 return (
char *)ERROR_PTR(
"first not valid", procName, NULL);
825 if (nstrings == 0 || (nstrings > n - first))
826 nstrings = n - first;
827 last = first + nstrings - 1;
830 for (i = first; i <= last; i++) {
832 return (
char *)ERROR_PTR(
"str not found", procName, NULL);
833 size += strlen(str) + 2;
836 if ((dest = (
char *)LEPT_CALLOC(size + 1,
sizeof(
char))) == NULL)
837 return (
char *)ERROR_PTR(
"dest not made", procName, NULL);
840 for (i = first; i <= last; i++) {
843 memcpy(dest +
index, src, len);
845 if (addnlflag == 1) {
848 }
else if (addnlflag == 2) {
880 PROCNAME(
"sarrayJoin");
883 return ERROR_INT(
"sa1 not defined", procName, 1);
885 return ERROR_INT(
"sa2 not defined", procName, 1);
888 for (i = 0; i < n; i++) {
922 PROCNAME(
"sarrayAppendRange");
925 return ERROR_INT(
"sa1 not defined", procName, 1);
927 return ERROR_INT(
"sa2 not defined", procName, 1);
932 if (end < 0 || end >= n)
935 return ERROR_INT(
"start > end", procName, 1);
937 for (i = start; i <= end; i++) {
972 PROCNAME(
"sarrayPadToSameSize");
975 return ERROR_INT(
"both sa1 and sa2 not defined", procName, 1);
980 for (i = n1; i < n2; i++)
982 }
else if (n1 > n2) {
983 for (i = n2; i < n1; i++)
1027 char emptystring[] =
"";
1028 l_int32 n, i, len, totlen;
1031 PROCNAME(
"sarrayConvertWordsToLines");
1034 return (
SARRAY *)ERROR_PTR(
"sa not defined", procName, NULL);
1040 for (i = 0; i < n; i++) {
1053 }
else if (totlen == 0 && len + 1 > linesize) {
1055 }
else if (totlen + len + 1 > linesize) {
1092 sarraySplitString(
SARRAY *sa,
1094 const char *separators)
1096 char *cstr, *substr, *saveptr;
1098 PROCNAME(
"sarraySplitString");
1101 return ERROR_INT(
"sa not defined", procName, 1);
1103 return ERROR_INT(
"str not defined", procName, 1);
1105 return ERROR_INT(
"separators not defined", procName, 1);
1108 substr =
strtokSafe(cstr, separators, &saveptr);
1111 while ((substr =
strtokSafe(NULL, separators, &saveptr)))
1142 l_int32 n, i, offset, found;
1145 PROCNAME(
"sarraySelectBySubstring");
1148 return (
SARRAY *)ERROR_PTR(
"sain not defined", procName, NULL);
1151 if (!substr || n == 0)
1155 for (i = 0; i < n; i++) {
1158 strlen(substr), &offset, &found);
1192 PROCNAME(
"sarraySelectByRange");
1195 return (
SARRAY *)ERROR_PTR(
"sain not defined", procName, NULL);
1196 if (first < 0) first = 0;
1198 if (last <= 0) last = n - 1;
1200 L_WARNING(
"last > n - 1; setting to n - 1\n", procName);
1204 return (
SARRAY *)ERROR_PTR(
"first must be >= last", procName, NULL);
1207 for (i = first; i <= last; i++) {
1255 l_int32 *pactualstart,
1262 l_int32 n, i, offset, found;
1264 PROCNAME(
"sarrayParseRange");
1267 return ERROR_INT(
"sa not defined", procName, 1);
1268 if (!pactualstart || !pend || !pnewstart)
1269 return ERROR_INT(
"not all range addresses defined", procName, 1);
1271 *pactualstart = *pend = *pnewstart = n;
1273 return ERROR_INT(
"substr not defined", procName, 1);
1276 if (start < 0 || start >= n)
1278 for (i = start; i < n; i++) {
1281 strlen(substr), &offset, &found);
1285 if (!found || offset != loc)
break;
1293 *pactualstart = start;
1294 for (i = start + 1; i < n; i++) {
1297 strlen(substr), &offset, &found);
1301 if (found && offset == loc)
break;
1311 for (i = start; i < n; i++) {
1314 strlen(substr), &offset, &found);
1318 if (!found || offset != loc)
break;
1343 PROCNAME(
"sarrayRead");
1346 return (
SARRAY *)ERROR_PTR(
"filename not defined", procName, NULL);
1349 return (
SARRAY *)ERROR_PTR(
"stream not opened", procName, NULL);
1353 return (
SARRAY *)ERROR_PTR(
"sa not read", procName, NULL);
1378 l_int32 i, n, size,
index, bufsize, version, ignore,
success;
1381 PROCNAME(
"sarrayReadStream");
1384 return (
SARRAY *)ERROR_PTR(
"stream not defined", procName, NULL);
1386 if (fscanf(
fp,
"\nSarray Version %d\n", &version) != 1)
1387 return (
SARRAY *)ERROR_PTR(
"not an sarray file", procName, NULL);
1389 return (
SARRAY *)ERROR_PTR(
"invalid sarray version", procName, NULL);
1390 if (fscanf(
fp,
"Number of strings = %d\n", &n) != 1)
1391 return (
SARRAY *)ERROR_PTR(
"error on # strings", procName, NULL);
1393 return (
SARRAY *)ERROR_PTR(
"more than 2^24 strings!", procName, NULL);
1397 return (
SARRAY *)ERROR_PTR(
"sa not made", procName, NULL);
1399 stringbuf = (
char *)LEPT_CALLOC(bufsize,
sizeof(
char));
1401 for (i = 0; i < n; i++) {
1403 if ((fscanf(
fp,
"%d[%d]:", &
index, &size) != 2) || (size > (1 << 30))) {
1405 L_ERROR(
"error on string size\n", procName);
1409 if (size > bufsize - 5) {
1410 LEPT_FREE(stringbuf);
1411 bufsize = (l_int32)(1.5 * size);
1412 stringbuf = (
char *)LEPT_CALLOC(bufsize,
sizeof(
char));
1415 if (fread(stringbuf, 1, size + 3,
fp) != size + 3) {
1417 L_ERROR(
"error reading string\n", procName);
1421 stringbuf[size + 2] =
'\0';
1425 ignore = fscanf(
fp,
"\n");
1428 LEPT_FREE(stringbuf);
1448 PROCNAME(
"sarrayReadMem");
1451 return (
SARRAY *)ERROR_PTR(
"data not defined", procName, NULL);
1453 return (
SARRAY *)ERROR_PTR(
"stream not opened", procName, NULL);
1457 if (!sa) L_ERROR(
"sarray not read\n", procName);
1476 PROCNAME(
"sarrayWrite");
1479 return ERROR_INT(
"filename not defined", procName, 1);
1481 return ERROR_INT(
"sa not defined", procName, 1);
1484 return ERROR_INT(
"stream not opened", procName, 1);
1488 return ERROR_INT(
"sa not written to stream", procName, 1);
1512 PROCNAME(
"sarrayWriteStream");
1515 return ERROR_INT(
"stream not defined", procName, 1);
1517 return ERROR_INT(
"sa not defined", procName, 1);
1521 fprintf(
fp,
"Number of strings = %d\n", n);
1522 for (i = 0; i < n; i++) {
1523 len = strlen(sa->
array[i]);
1524 fprintf(
fp,
" %d[%d]: %s\n", i, len, sa->
array[i]);
1553 PROCNAME(
"sarrayWriteMem");
1555 if (pdata) *pdata = NULL;
1556 if (psize) *psize = 0;
1558 return ERROR_INT(
"&data not defined", procName, 1);
1560 return ERROR_INT(
"&size not defined", procName, 1);
1562 return ERROR_INT(
"sa not defined", procName, 1);
1565 if ((
fp = open_memstream((
char **)pdata, psize)) == NULL)
1566 return ERROR_INT(
"stream not opened", procName, 1);
1569 L_INFO(
"work-around: writing to a temp file\n", procName);
1572 return ERROR_INT(
"tmpfile stream not opened", procName, 1);
1574 if ((
fp = tmpfile()) == NULL)
1575 return ERROR_INT(
"tmpfile stream not opened", procName, 1);
1599 PROCNAME(
"sarrayAppend");
1602 return ERROR_INT(
"filename not defined", procName, 1);
1604 return ERROR_INT(
"sa not defined", procName, 1);
1607 return ERROR_INT(
"stream not opened", procName, 1);
1610 return ERROR_INT(
"sa not appended to stream", procName, 1);
1671 PROCNAME(
"getNumberedPathnamesInDirectory");
1674 return (
SARRAY *)ERROR_PTR(
"dirname not defined", procName, NULL);
1677 return (
SARRAY *)ERROR_PTR(
"sa not made", procName, NULL);
1715 char *fname, *fullname;
1717 SARRAY *sa, *safiles, *saout;
1719 PROCNAME(
"getSortedPathnamesInDirectory");
1722 return (
SARRAY *)ERROR_PTR(
"dirname not defined", procName, NULL);
1725 return (
SARRAY *)ERROR_PTR(
"sa not made", procName, NULL);
1730 L_WARNING(
"no files found\n", procName);
1736 first = L_MIN(L_MAX(first, 0), n - 1);
1739 last = L_MIN(first + nfiles - 1, n - 1);
1742 for (i = first; i <= last; i++) {
1744 fullname =
pathJoin(dirname, fname);
1776 l_int32 i, nfiles, num,
index;
1779 PROCNAME(
"convertSortedToNumberedPathnames");
1782 return (
SARRAY *)ERROR_PTR(
"sa not defined", procName, NULL);
1791 for (i = nfiles - 1; i >= 0; i--) {
1794 if (num < 0)
continue;
1795 num = L_MIN(num + 1, maxnum);
1805 for (i = 0; i < nfiles; i++) {
1808 if (index < 0 || index >= num)
continue;
1811 L_WARNING(
"\n Multiple files with same number: %d\n",
1853 char *realdir, *name;
1857 struct dirent *pdirentry;
1861 PROCNAME(
"getFilenamesInDirectory");
1864 return (
SARRAY *)ERROR_PTR(
"dirname not defined", procName, NULL);
1867 pdir = opendir(realdir);
1870 return (
SARRAY *)ERROR_PTR(
"pdir not opened", procName, NULL);
1873 while ((pdirentry = readdir(pdir))) {
1876 if ((0 == fstatat(dfd, pdirentry->d_name, &st, 0))
1877 && S_ISDIR(st.st_mode)) {
1882 name = pdirentry->d_name;
1884 if (len == 1 && name[len - 1] ==
'.')
continue;
1885 if (len == 2 && name[len - 1] ==
'.' && name[len - 2] ==
'.')
continue;
1896 #include <windows.h> 1903 HANDLE hFind = INVALID_HANDLE_VALUE;
1905 WIN32_FIND_DATAA ffd;
1907 PROCNAME(
"getFilenamesInDirectory");
1910 return (
SARRAY *)ERROR_PTR(
"dirname not defined", procName, NULL);
1916 if (strlen(pszDir) + 1 > MAX_PATH) {
1918 return (
SARRAY *)ERROR_PTR(
"dirname is too long", procName, NULL);
1923 return (
SARRAY *)ERROR_PTR(
"safiles not made", procName, NULL);
1926 hFind = FindFirstFileA(pszDir, &ffd);
1927 if (INVALID_HANDLE_VALUE == hFind) {
1930 return (
SARRAY *)ERROR_PTR(
"hFind not opened", procName, NULL);
1933 while (FindNextFileA(hFind, &ffd) != 0) {
1934 if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
l_int32 sarrayReplaceString(SARRAY *sa, l_int32 index, char *newstr, l_int32 copyflag)
sarrayReplaceString()
char * sarrayToString(SARRAY *sa, l_int32 addnlflag)
sarrayToString()
SARRAY * sarrayCopy(SARRAY *sa)
sarrayCopy()
char * genPathname(const char *dir, const char *fname)
genPathname()
#define SARRAY_VERSION_NUMBER
char * stringNew(const char *src)
stringNew()
char * sarrayRemoveString(SARRAY *sa, l_int32 index)
sarrayRemoveString()
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 sarrayWriteStream(FILE *fp, SARRAY *sa)
sarrayWriteStream()
l_int32 sarrayAppendRange(SARRAY *sa1, SARRAY *sa2, l_int32 start, l_int32 end)
sarrayAppendRange()
l_int32 sarrayWrite(const char *filename, SARRAY *sa)
sarrayWrite()
void * reallocNew(void **pindata, l_int32 oldsize, l_int32 newsize)
reallocNew()
SARRAY * sarrayConvertWordsToLines(SARRAY *sa, l_int32 linesize)
sarrayConvertWordsToLines()
SARRAY * getNumberedPathnamesInDirectory(const char *dirname, const char *substr, l_int32 numpre, l_int32 numpost, l_int32 maxnum)
getNumberedPathnamesInDirectory()
static const l_int32 L_INSERT
l_int32 sarrayAppend(const char *filename, SARRAY *sa)
sarrayAppend()
l_int32 sarrayWriteMem(l_uint8 **pdata, size_t *psize, SARRAY *sa)
sarrayWriteMem()
l_int32 sarrayChangeRefcount(SARRAY *sa, l_int32 delta)
sarrayChangeRefCount()
FILE * fopenWriteWinTempfile()
fopenWriteWinTempfile()
char * sarrayGetString(SARRAY *sa, l_int32 index, l_int32 copyflag)
sarrayGetString()
l_int32 sarrayAddString(SARRAY *sa, char *string, l_int32 copyflag)
sarrayAddString()
char ** sarrayGetArray(SARRAY *sa, l_int32 *pnalloc, l_int32 *pn)
sarrayGetArray()
SARRAY * sarrayReadMem(const l_uint8 *data, size_t size)
sarrayReadMem()
SARRAY * sarrayCreateLinesFromString(const char *string, l_int32 blankflag)
sarrayCreateLinesFromString()
SARRAY * getSortedPathnamesInDirectory(const char *dirname, const char *substr, l_int32 first, l_int32 nfiles)
getSortedPathnamesInDirectory()
SARRAY * sarrayClone(SARRAY *sa)
sarrayClone()
l_int32 extractNumberFromFilename(const char *fname, l_int32 numpre, l_int32 numpost)
l_makeTempFilename()
SARRAY * sarraySelectByRange(SARRAY *sain, l_int32 first, l_int32 last)
sarraySelectByRange()
FILE * fopenWriteStream(const char *filename, const char *modestring)
fopenWriteStream()
FILE * fopenReadStream(const char *filename)
fopenReadStream()
static l_int32 sarrayExtendArray(SARRAY *sa)
sarrayExtendArray()
l_uint8 * l_binaryReadStream(FILE *fp, size_t *pnbytes)
l_binaryReadStream()
l_int32 sarrayGetCount(SARRAY *sa)
sarrayGetCount()
l_int32 sarrayGetRefcount(SARRAY *sa)
sarrayGetRefCount()
SARRAY * convertSortedToNumberedPathnames(SARRAY *sa, l_int32 numpre, l_int32 numpost, l_int32 maxnum)
convertSortedToNumberedPathnames()
SARRAY * sarrayRead(const char *filename)
sarrayRead()
SARRAY * sarraySort(SARRAY *saout, SARRAY *sain, l_int32 sortorder)
sarraySort()
l_int32 convertSepCharsInPath(char *path, l_int32 type)
convertSepCharsInPath()
char * pathJoin(const char *dir, const char *fname)
pathJoin()
char * stringJoin(const char *src1, const char *src2)
stringJoin()
l_int32 sarrayClear(SARRAY *sa)
sarrayClear()
char * strtokSafe(char *cstr, const char *seps, char **psaveptr)
strtokSafe()
l_int32 sarrayPadToSameSize(SARRAY *sa1, SARRAY *sa2, char *padstring)
sarrayPadToSameSize()
SARRAY * sarrayCreateInitialized(l_int32 n, char *initstr)
sarrayCreateInitialized()
l_int32 sarrayParseRange(SARRAY *sa, l_int32 start, l_int32 *pactualstart, l_int32 *pend, l_int32 *pnewstart, const char *substr, l_int32 loc)
sarrayParseRange()
l_int32 arrayFindSequence(const l_uint8 *data, size_t datalen, const l_uint8 *sequence, size_t seqlen, l_int32 *poffset, l_int32 *pfound)
arrayFindSequence()
SARRAY * sarrayReadStream(FILE *fp)
sarrayReadStream()
l_int32 sarrayJoin(SARRAY *sa1, SARRAY *sa2)
sarrayJoin()
SARRAY * sarraySelectBySubstring(SARRAY *sain, const char *substr)
sarraySelectBySubstring()
SARRAY * sarrayCreateWordsFromString(const char *string)
sarrayCreateWordsFromString()
static const l_int32 L_BUF_SIZE
void sarrayDestroy(SARRAY **psa)
sarrayDestroy()