Leptonica  1.73
Image processing and image analysis suite
sarray1.c
Go to the documentation of this file.
1 /*====================================================================*
2  - Copyright (C) 2001 Leptonica. All rights reserved.
3  -
4  - Redistribution and use in source and binary forms, with or without
5  - modification, are permitted provided that the following conditions
6  - are met:
7  - 1. Redistributions of source code must retain the above copyright
8  - notice, this list of conditions and the following disclaimer.
9  - 2. Redistributions in binary form must reproduce the above
10  - copyright notice, this list of conditions and the following
11  - disclaimer in the documentation and/or other materials
12  - provided with the distribution.
13  -
14  - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15  - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16  - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17  - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANY
18  - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  - OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23  - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *====================================================================*/
26 
132 #include <string.h>
133 #ifndef _WIN32
134 #include <dirent.h> /* unix only */
135 #include <sys/stat.h>
136 #endif /* ! _WIN32 */
137 #include "allheaders.h"
138 
139 static const l_int32 INITIAL_PTR_ARRAYSIZE = 50; /* n'importe quoi */
140 static const l_int32 L_BUF_SIZE = 512;
141 
142  /* Static functions */
143 static l_int32 sarrayExtendArray(SARRAY *sa);
144 
145 
146 /*--------------------------------------------------------------------------*
147  * String array create/destroy/copy/extend *
148  *--------------------------------------------------------------------------*/
156 SARRAY *
157 sarrayCreate(l_int32 n)
158 {
159 SARRAY *sa;
160 
161  PROCNAME("sarrayCreate");
162 
163  if (n <= 0)
164  n = INITIAL_PTR_ARRAYSIZE;
165 
166  sa = (SARRAY *)LEPT_CALLOC(1, sizeof(SARRAY));
167  if ((sa->array = (char **)LEPT_CALLOC(n, sizeof(char *))) == NULL) {
168  sarrayDestroy(&sa);
169  return (SARRAY *)ERROR_PTR("ptr array not made", procName, NULL);
170  }
171 
172  sa->nalloc = n;
173  sa->n = 0;
174  sa->refcount = 1;
175  return sa;
176 }
177 
178 
186 SARRAY *
188  char *initstr)
189 {
190 l_int32 i;
191 SARRAY *sa;
192 
193  PROCNAME("sarrayCreateInitialized");
194 
195  if (n <= 0)
196  return (SARRAY *)ERROR_PTR("n must be > 0", procName, NULL);
197  if (!initstr)
198  return (SARRAY *)ERROR_PTR("initstr not defined", procName, NULL);
199 
200  sa = sarrayCreate(n);
201  for (i = 0; i < n; i++)
202  sarrayAddString(sa, initstr, L_COPY);
203  return sa;
204 }
205 
206 
219 SARRAY *
220 sarrayCreateWordsFromString(const char *string)
221 {
222 char separators[] = " \n\t";
223 l_int32 i, nsub, size, inword;
224 SARRAY *sa;
225 
226  PROCNAME("sarrayCreateWordsFromString");
227 
228  if (!string)
229  return (SARRAY *)ERROR_PTR("textstr not defined", procName, NULL);
230 
231  /* Find the number of words */
232  size = strlen(string);
233  nsub = 0;
234  inword = FALSE;
235  for (i = 0; i < size; i++) {
236  if (inword == FALSE &&
237  (string[i] != ' ' && string[i] != '\t' && string[i] != '\n')) {
238  inword = TRUE;
239  nsub++;
240  } else if (inword == TRUE &&
241  (string[i] == ' ' || string[i] == '\t' || string[i] == '\n')) {
242  inword = FALSE;
243  }
244  }
245 
246  if ((sa = sarrayCreate(nsub)) == NULL)
247  return (SARRAY *)ERROR_PTR("sa not made", procName, NULL);
248  sarraySplitString(sa, string, separators);
249 
250  return sa;
251 }
252 
253 
269 SARRAY *
270 sarrayCreateLinesFromString(const char *string,
271  l_int32 blankflag)
272 {
273 l_int32 i, nsub, size, startptr;
274 char *cstring, *substring;
275 SARRAY *sa;
276 
277  PROCNAME("sarrayCreateLinesFromString");
278 
279  if (!string)
280  return (SARRAY *)ERROR_PTR("textstr not defined", procName, NULL);
281 
282  /* Find the number of lines */
283  size = strlen(string);
284  nsub = 0;
285  for (i = 0; i < size; i++) {
286  if (string[i] == '\n')
287  nsub++;
288  }
289 
290  if ((sa = sarrayCreate(nsub)) == NULL)
291  return (SARRAY *)ERROR_PTR("sa not made", procName, NULL);
292 
293  if (blankflag) { /* keep blank lines as null strings */
294  /* Make a copy for munging */
295  if ((cstring = stringNew(string)) == NULL) {
296  sarrayDestroy(&sa);
297  return (SARRAY *)ERROR_PTR("cstring not made", procName, NULL);
298  }
299  /* We'll insert nulls like strtok */
300  startptr = 0;
301  for (i = 0; i < size; i++) {
302  if (cstring[i] == '\n') {
303  cstring[i] = '\0';
304  if (i > 0 && cstring[i - 1] == '\r')
305  cstring[i - 1] = '\0'; /* also remove Windows CR */
306  if ((substring = stringNew(cstring + startptr)) == NULL) {
307  sarrayDestroy(&sa);
308  LEPT_FREE(cstring);
309  return (SARRAY *)ERROR_PTR("substring not made",
310  procName, NULL);
311  }
312  sarrayAddString(sa, substring, L_INSERT);
313 /* fprintf(stderr, "substring = %s\n", substring); */
314  startptr = i + 1;
315  }
316  }
317  if (startptr < size) { /* no newline at end of last line */
318  if ((substring = stringNew(cstring + startptr)) == NULL) {
319  sarrayDestroy(&sa);
320  LEPT_FREE(cstring);
321  return (SARRAY *)ERROR_PTR("substring not made",
322  procName, NULL);
323  }
324  sarrayAddString(sa, substring, L_INSERT);
325 /* fprintf(stderr, "substring = %s\n", substring); */
326  }
327  LEPT_FREE(cstring);
328  } else { /* remove blank lines; use strtok */
329  sarraySplitString(sa, string, "\r\n");
330  }
331 
332  return sa;
333 }
334 
335 
348 void
350 {
351 l_int32 i;
352 SARRAY *sa;
353 
354  PROCNAME("sarrayDestroy");
355 
356  if (psa == NULL) {
357  L_WARNING("ptr address is NULL!\n", procName);
358  return;
359  }
360  if ((sa = *psa) == NULL)
361  return;
362 
363  sarrayChangeRefcount(sa, -1);
364  if (sarrayGetRefcount(sa) <= 0) {
365  if (sa->array) {
366  for (i = 0; i < sa->n; i++) {
367  if (sa->array[i])
368  LEPT_FREE(sa->array[i]);
369  }
370  LEPT_FREE(sa->array);
371  }
372  LEPT_FREE(sa);
373  }
374 
375  *psa = NULL;
376  return;
377 }
378 
379 
386 SARRAY *
388 {
389 l_int32 i;
390 SARRAY *csa;
391 
392  PROCNAME("sarrayCopy");
393 
394  if (!sa)
395  return (SARRAY *)ERROR_PTR("sa not defined", procName, NULL);
396 
397  if ((csa = sarrayCreate(sa->nalloc)) == NULL)
398  return (SARRAY *)ERROR_PTR("csa not made", procName, NULL);
399 
400  for (i = 0; i < sa->n; i++)
401  sarrayAddString(csa, sa->array[i], L_COPY);
402 
403  return csa;
404 }
405 
406 
413 SARRAY *
415 {
416  PROCNAME("sarrayClone");
417 
418  if (!sa)
419  return (SARRAY *)ERROR_PTR("sa not defined", procName, NULL);
420  sarrayChangeRefcount(sa, 1);
421  return sa;
422 }
423 
424 
438 l_int32
440  char *string,
441  l_int32 copyflag)
442 {
443 l_int32 n;
444 
445  PROCNAME("sarrayAddString");
446 
447  if (!sa)
448  return ERROR_INT("sa not defined", procName, 1);
449  if (!string)
450  return ERROR_INT("string not defined", procName, 1);
451  if (copyflag != L_INSERT && copyflag != L_COPY)
452  return ERROR_INT("invalid copyflag", procName, 1);
453 
454  n = sarrayGetCount(sa);
455  if (n >= sa->nalloc)
456  sarrayExtendArray(sa);
457 
458  if (copyflag == L_INSERT)
459  sa->array[n] = string;
460  else /* L_COPY */
461  sa->array[n] = stringNew(string);
462  sa->n++;
463 
464  return 0;
465 }
466 
467 
474 static l_int32
476 {
477  PROCNAME("sarrayExtendArray");
478 
479  if (!sa)
480  return ERROR_INT("sa not defined", procName, 1);
481 
482  if ((sa->array = (char **)reallocNew((void **)&sa->array,
483  sizeof(char *) * sa->nalloc,
484  2 * sizeof(char *) * sa->nalloc)) == NULL)
485  return ERROR_INT("new ptr array not returned", procName, 1);
486 
487  sa->nalloc *= 2;
488  return 0;
489 }
490 
491 
499 char *
501  l_int32 index)
502 {
503 char *string;
504 char **array;
505 l_int32 i, n, nalloc;
506 
507  PROCNAME("sarrayRemoveString");
508 
509  if (!sa)
510  return (char *)ERROR_PTR("sa not defined", procName, NULL);
511 
512  if ((array = sarrayGetArray(sa, &nalloc, &n)) == NULL)
513  return (char *)ERROR_PTR("array not returned", procName, NULL);
514 
515  if (index < 0 || index >= n)
516  return (char *)ERROR_PTR("array index out of bounds", procName, NULL);
517 
518  string = array[index];
519 
520  /* If removed string is not at end of array, shift
521  * to fill in, maintaining original ordering.
522  * Note: if we didn't care about the order, we could
523  * put the last string array[n - 1] directly into the hole. */
524  for (i = index; i < n - 1; i++)
525  array[i] = array[i + 1];
526 
527  sa->n--;
528  return string;
529 }
530 
531 
550 l_int32
552  l_int32 index,
553  char *newstr,
554  l_int32 copyflag)
555 {
556 char *str;
557 l_int32 n;
558 
559  PROCNAME("sarrayReplaceString");
560 
561  if (!sa)
562  return ERROR_INT("sa not defined", procName, 1);
563  n = sarrayGetCount(sa);
564  if (index < 0 || index >= n)
565  return ERROR_INT("array index out of bounds", procName, 1);
566  if (!newstr)
567  return ERROR_INT("newstr not defined", procName, 1);
568  if (copyflag != L_INSERT && copyflag != L_COPY)
569  return ERROR_INT("invalid copyflag", procName, 1);
570 
571  LEPT_FREE(sa->array[index]);
572  if (copyflag == L_INSERT)
573  str = newstr;
574  else /* L_COPY */
575  str = stringNew(newstr);
576  sa->array[index] = str;
577  return 0;
578 }
579 
580 
587 l_int32
589 {
590 l_int32 i;
591 
592  PROCNAME("sarrayClear");
593 
594  if (!sa)
595  return ERROR_INT("sa not defined", procName, 1);
596  for (i = 0; i < sa->n; i++) { /* free strings and null ptrs */
597  LEPT_FREE(sa->array[i]);
598  sa->array[i] = NULL;
599  }
600  sa->n = 0;
601  return 0;
602 }
603 
604 
605 /*----------------------------------------------------------------------*
606  * Accessors *
607  *----------------------------------------------------------------------*/
614 l_int32
616 {
617  PROCNAME("sarrayGetCount");
618 
619  if (!sa)
620  return ERROR_INT("sa not defined", procName, 0);
621  return sa->n;
622 }
623 
624 
639 char **
641  l_int32 *pnalloc,
642  l_int32 *pn)
643 {
644 char **array;
645 
646  PROCNAME("sarrayGetArray");
647 
648  if (!sa)
649  return (char **)ERROR_PTR("sa not defined", procName, NULL);
650 
651  array = sa->array;
652  if (pnalloc) *pnalloc = sa->nalloc;
653  if (pn) *pn = sa->n;
654 
655  return array;
656 }
657 
658 
674 char *
676  l_int32 index,
677  l_int32 copyflag)
678 {
679  PROCNAME("sarrayGetString");
680 
681  if (!sa)
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);
685  if (copyflag != L_NOCOPY && copyflag != L_COPY)
686  return (char *)ERROR_PTR("invalid copyflag", procName, NULL);
687 
688  if (copyflag == L_NOCOPY)
689  return sa->array[index];
690  else /* L_COPY */
691  return stringNew(sa->array[index]);
692 }
693 
694 
701 l_int32
703 {
704  PROCNAME("sarrayGetRefcount");
705 
706  if (!sa)
707  return ERROR_INT("sa not defined", procName, UNDEF);
708  return sa->refcount;
709 }
710 
711 
719 l_int32
721  l_int32 delta)
722 {
723  PROCNAME("sarrayChangeRefcount");
724 
725  if (!sa)
726  return ERROR_INT("sa not defined", procName, UNDEF);
727  sa->refcount += delta;
728  return 0;
729 }
730 
731 
732 /*----------------------------------------------------------------------*
733  * Conversion to string *
734  *----------------------------------------------------------------------*/
756 char *
758  l_int32 addnlflag)
759 {
760  PROCNAME("sarrayToString");
761 
762  if (!sa)
763  return (char *)ERROR_PTR("sa not defined", procName, NULL);
764 
765  return sarrayToStringRange(sa, 0, 0, addnlflag);
766 }
767 
768 
791 char *
793  l_int32 first,
794  l_int32 nstrings,
795  l_int32 addnlflag)
796 {
797 char *dest, *src, *str;
798 l_int32 n, i, last, size, index, len;
799 
800  PROCNAME("sarrayToStringRange");
801 
802  if (!sa)
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);
806 
807  n = sarrayGetCount(sa);
808 
809  /* Empty sa; return char corresponding to addnlflag only */
810  if (n == 0) {
811  if (first == 0) {
812  if (addnlflag == 0)
813  return stringNew("");
814  if (addnlflag == 1)
815  return stringNew("\n");
816  else /* addnlflag == 2) */
817  return stringNew(" ");
818  } else {
819  return (char *)ERROR_PTR("first not valid", procName, NULL);
820  }
821  }
822 
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; /* no overflow */
827  last = first + nstrings - 1;
828 
829  size = 0;
830  for (i = first; i <= last; i++) {
831  if ((str = sarrayGetString(sa, i, L_NOCOPY)) == NULL)
832  return (char *)ERROR_PTR("str not found", procName, NULL);
833  size += strlen(str) + 2;
834  }
835 
836  if ((dest = (char *)LEPT_CALLOC(size + 1, sizeof(char))) == NULL)
837  return (char *)ERROR_PTR("dest not made", procName, NULL);
838 
839  index = 0;
840  for (i = first; i <= last; i++) {
841  src = sarrayGetString(sa, i, L_NOCOPY);
842  len = strlen(src);
843  memcpy(dest + index, src, len);
844  index += len;
845  if (addnlflag == 1) {
846  dest[index] = '\n';
847  index++;
848  } else if (addnlflag == 2) {
849  dest[index] = ' ';
850  index++;
851  }
852  }
853 
854  return dest;
855 }
856 
857 
858 /*----------------------------------------------------------------------*
859  * Join 2 sarrays *
860  *----------------------------------------------------------------------*/
873 l_int32
875  SARRAY *sa2)
876 {
877 char *str;
878 l_int32 n, i;
879 
880  PROCNAME("sarrayJoin");
881 
882  if (!sa1)
883  return ERROR_INT("sa1 not defined", procName, 1);
884  if (!sa2)
885  return ERROR_INT("sa2 not defined", procName, 1);
886 
887  n = sarrayGetCount(sa2);
888  for (i = 0; i < n; i++) {
889  str = sarrayGetString(sa2, i, L_NOCOPY);
890  sarrayAddString(sa1, str, L_COPY);
891  }
892 
893  return 0;
894 }
895 
896 
913 l_int32
915  SARRAY *sa2,
916  l_int32 start,
917  l_int32 end)
918 {
919 char *str;
920 l_int32 n, i;
921 
922  PROCNAME("sarrayAppendRange");
923 
924  if (!sa1)
925  return ERROR_INT("sa1 not defined", procName, 1);
926  if (!sa2)
927  return ERROR_INT("sa2 not defined", procName, 1);
928 
929  if (start < 0)
930  start = 0;
931  n = sarrayGetCount(sa2);
932  if (end < 0 || end >= n)
933  end = n - 1;
934  if (start > end)
935  return ERROR_INT("start > end", procName, 1);
936 
937  for (i = start; i <= end; i++) {
938  str = sarrayGetString(sa2, i, L_NOCOPY);
939  sarrayAddString(sa1, str, L_COPY);
940  }
941 
942  return 0;
943 }
944 
945 
946 /*----------------------------------------------------------------------*
947  * Pad an sarray to be the same size as another sarray *
948  *----------------------------------------------------------------------*/
965 l_int32
967  SARRAY *sa2,
968  char *padstring)
969 {
970 l_int32 i, n1, n2;
971 
972  PROCNAME("sarrayPadToSameSize");
973 
974  if (!sa1 || !sa2)
975  return ERROR_INT("both sa1 and sa2 not defined", procName, 1);
976 
977  n1 = sarrayGetCount(sa1);
978  n2 = sarrayGetCount(sa2);
979  if (n1 < n2) {
980  for (i = n1; i < n2; i++)
981  sarrayAddString(sa1, padstring, L_COPY);
982  } else if (n1 > n2) {
983  for (i = n2; i < n1; i++)
984  sarrayAddString(sa2, padstring, L_COPY);
985  }
986 
987  return 0;
988 }
989 
990 
991 /*----------------------------------------------------------------------*
992  * Convert word sarray to line sarray *
993  *----------------------------------------------------------------------*/
1022 SARRAY *
1024  l_int32 linesize)
1025 {
1026 char *wd, *strl;
1027 char emptystring[] = "";
1028 l_int32 n, i, len, totlen;
1029 SARRAY *sal, *saout;
1030 
1031  PROCNAME("sarrayConvertWordsToLines");
1032 
1033  if (!sa)
1034  return (SARRAY *)ERROR_PTR("sa not defined", procName, NULL);
1035 
1036  saout = sarrayCreate(0);
1037  n = sarrayGetCount(sa);
1038  totlen = 0;
1039  sal = NULL;
1040  for (i = 0; i < n; i++) {
1041  if (!sal)
1042  sal = sarrayCreate(0);
1043  wd = sarrayGetString(sa, i, L_NOCOPY);
1044  len = strlen(wd);
1045  if (len == 0) { /* end of paragraph: end line & insert blank line */
1046  if (totlen > 0) {
1047  strl = sarrayToString(sal, 2);
1048  sarrayAddString(saout, strl, L_INSERT);
1049  }
1050  sarrayAddString(saout, emptystring, L_COPY);
1051  sarrayDestroy(&sal);
1052  totlen = 0;
1053  } else if (totlen == 0 && len + 1 > linesize) { /* long word! */
1054  sarrayAddString(saout, wd, L_COPY); /* copy to one line */
1055  } else if (totlen + len + 1 > linesize) { /* end line & start new */
1056  strl = sarrayToString(sal, 2);
1057  sarrayAddString(saout, strl, L_INSERT);
1058  sarrayDestroy(&sal);
1059  sal = sarrayCreate(0);
1060  sarrayAddString(sal, wd, L_COPY);
1061  totlen = len + 1;
1062  } else { /* add to current line */
1063  sarrayAddString(sal, wd, L_COPY);
1064  totlen += len + 1;
1065  }
1066  }
1067  if (totlen > 0) { /* didn't end with blank line; output last line */
1068  strl = sarrayToString(sal, 2);
1069  sarrayAddString(saout, strl, L_INSERT);
1070  sarrayDestroy(&sal);
1071  }
1072 
1073  return saout;
1074 }
1075 
1076 
1077 /*----------------------------------------------------------------------*
1078  * Split string on separator list *
1079  *----------------------------------------------------------------------*/
1080 /*
1081  * sarraySplitString()
1082  *
1083  * Input: sa (to append to; typically empty initially)
1084  * str (string to split; not changed)
1085  * separators (characters that split input string)
1086  * Return: 0 if OK, 1 on error.
1087  *
1088  * Notes:
1089  * (1) This uses strtokSafe(). See the notes there in utils.c.
1090  */
1091 l_int32
1092 sarraySplitString(SARRAY *sa,
1093  const char *str,
1094  const char *separators)
1095 {
1096 char *cstr, *substr, *saveptr;
1097 
1098  PROCNAME("sarraySplitString");
1099 
1100  if (!sa)
1101  return ERROR_INT("sa not defined", procName, 1);
1102  if (!str)
1103  return ERROR_INT("str not defined", procName, 1);
1104  if (!separators)
1105  return ERROR_INT("separators not defined", procName, 1);
1106 
1107  cstr = stringNew(str); /* preserves const-ness of input str */
1108  substr = strtokSafe(cstr, separators, &saveptr);
1109  if (substr)
1110  sarrayAddString(sa, substr, L_INSERT);
1111  while ((substr = strtokSafe(NULL, separators, &saveptr)))
1112  sarrayAddString(sa, substr, L_INSERT);
1113  LEPT_FREE(cstr);
1114 
1115  return 0;
1116 }
1117 
1118 
1119 /*----------------------------------------------------------------------*
1120  * Filter sarray *
1121  *----------------------------------------------------------------------*/
1137 SARRAY *
1139  const char *substr)
1140 {
1141 char *str;
1142 l_int32 n, i, offset, found;
1143 SARRAY *saout;
1144 
1145  PROCNAME("sarraySelectBySubstring");
1146 
1147  if (!sain)
1148  return (SARRAY *)ERROR_PTR("sain not defined", procName, NULL);
1149 
1150  n = sarrayGetCount(sain);
1151  if (!substr || n == 0)
1152  return sarrayCopy(sain);
1153 
1154  saout = sarrayCreate(n);
1155  for (i = 0; i < n; i++) {
1156  str = sarrayGetString(sain, i, L_NOCOPY);
1157  arrayFindSequence((l_uint8 *)str, strlen(str), (l_uint8 *)substr,
1158  strlen(substr), &offset, &found);
1159  if (found)
1160  sarrayAddString(saout, str, L_COPY);
1161  }
1162 
1163  return saout;
1164 }
1165 
1166 
1183 SARRAY *
1185  l_int32 first,
1186  l_int32 last)
1187 {
1188 char *str;
1189 l_int32 n, i;
1190 SARRAY *saout;
1191 
1192  PROCNAME("sarraySelectByRange");
1193 
1194  if (!sain)
1195  return (SARRAY *)ERROR_PTR("sain not defined", procName, NULL);
1196  if (first < 0) first = 0;
1197  n = sarrayGetCount(sain);
1198  if (last <= 0) last = n - 1;
1199  if (last >= n) {
1200  L_WARNING("last > n - 1; setting to n - 1\n", procName);
1201  last = n - 1;
1202  }
1203  if (first > last)
1204  return (SARRAY *)ERROR_PTR("first must be >= last", procName, NULL);
1205 
1206  saout = sarrayCreate(0);
1207  for (i = first; i <= last; i++) {
1208  str = sarrayGetString(sain, i, L_COPY);
1209  sarrayAddString(saout, str, L_INSERT);
1210  }
1211 
1212  return saout;
1213 }
1214 
1215 
1252 l_int32
1254  l_int32 start,
1255  l_int32 *pactualstart,
1256  l_int32 *pend,
1257  l_int32 *pnewstart,
1258  const char *substr,
1259  l_int32 loc)
1260 {
1261 char *str;
1262 l_int32 n, i, offset, found;
1263 
1264  PROCNAME("sarrayParseRange");
1265 
1266  if (!sa)
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);
1270  n = sarrayGetCount(sa);
1271  *pactualstart = *pend = *pnewstart = n;
1272  if (!substr)
1273  return ERROR_INT("substr not defined", procName, 1);
1274 
1275  /* Look for the first string without the marker */
1276  if (start < 0 || start >= n)
1277  return 1;
1278  for (i = start; i < n; i++) {
1279  str = sarrayGetString(sa, i, L_NOCOPY);
1280  arrayFindSequence((l_uint8 *)str, strlen(str), (l_uint8 *)substr,
1281  strlen(substr), &offset, &found);
1282  if (loc < 0) {
1283  if (!found) break;
1284  } else {
1285  if (!found || offset != loc) break;
1286  }
1287  }
1288  start = i;
1289  if (i == n) /* couldn't get started */
1290  return 1;
1291 
1292  /* Look for the last string without the marker */
1293  *pactualstart = start;
1294  for (i = start + 1; i < n; i++) {
1295  str = sarrayGetString(sa, i, L_NOCOPY);
1296  arrayFindSequence((l_uint8 *)str, strlen(str), (l_uint8 *)substr,
1297  strlen(substr), &offset, &found);
1298  if (loc < 0) {
1299  if (found) break;
1300  } else {
1301  if (found && offset == loc) break;
1302  }
1303  }
1304  *pend = i - 1;
1305  start = i;
1306  if (i == n) /* no further range */
1307  return 0;
1308 
1309  /* Look for the first string after *pend without the marker.
1310  * This will start the next run of strings, if it exists. */
1311  for (i = start; i < n; i++) {
1312  str = sarrayGetString(sa, i, L_NOCOPY);
1313  arrayFindSequence((l_uint8 *)str, strlen(str), (l_uint8 *)substr,
1314  strlen(substr), &offset, &found);
1315  if (loc < 0) {
1316  if (!found) break;
1317  } else {
1318  if (!found || offset != loc) break;
1319  }
1320  }
1321  if (i < n)
1322  *pnewstart = i;
1323 
1324  return 0;
1325 }
1326 
1327 
1328 /*----------------------------------------------------------------------*
1329  * Serialize for I/O *
1330  *----------------------------------------------------------------------*/
1337 SARRAY *
1338 sarrayRead(const char *filename)
1339 {
1340 FILE *fp;
1341 SARRAY *sa;
1342 
1343  PROCNAME("sarrayRead");
1344 
1345  if (!filename)
1346  return (SARRAY *)ERROR_PTR("filename not defined", procName, NULL);
1347 
1348  if ((fp = fopenReadStream(filename)) == NULL)
1349  return (SARRAY *)ERROR_PTR("stream not opened", procName, NULL);
1350  sa = sarrayReadStream(fp);
1351  fclose(fp);
1352  if (!sa)
1353  return (SARRAY *)ERROR_PTR("sa not read", procName, NULL);
1354  return sa;
1355 }
1356 
1357 
1374 SARRAY *
1376 {
1377 char *stringbuf;
1378 l_int32 i, n, size, index, bufsize, version, ignore, success;
1379 SARRAY *sa;
1380 
1381  PROCNAME("sarrayReadStream");
1382 
1383  if (!fp)
1384  return (SARRAY *)ERROR_PTR("stream not defined", procName, NULL);
1385 
1386  if (fscanf(fp, "\nSarray Version %d\n", &version) != 1)
1387  return (SARRAY *)ERROR_PTR("not an sarray file", procName, NULL);
1388  if (version != SARRAY_VERSION_NUMBER)
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);
1392  if (n > (1 << 24))
1393  return (SARRAY *)ERROR_PTR("more than 2^24 strings!", procName, NULL);
1394 
1395  success = TRUE;
1396  if ((sa = sarrayCreate(n)) == NULL)
1397  return (SARRAY *)ERROR_PTR("sa not made", procName, NULL);
1398  bufsize = L_BUF_SIZE + 1;
1399  stringbuf = (char *)LEPT_CALLOC(bufsize, sizeof(char));
1400 
1401  for (i = 0; i < n; i++) {
1402  /* Get the size of the stored string */
1403  if ((fscanf(fp, "%d[%d]:", &index, &size) != 2) || (size > (1 << 30))) {
1404  success = FALSE;
1405  L_ERROR("error on string size\n", procName);
1406  goto cleanup;
1407  }
1408  /* Expand the string buffer if necessary */
1409  if (size > bufsize - 5) {
1410  LEPT_FREE(stringbuf);
1411  bufsize = (l_int32)(1.5 * size);
1412  stringbuf = (char *)LEPT_CALLOC(bufsize, sizeof(char));
1413  }
1414  /* Read the stored string, plus leading spaces and trailing \n */
1415  if (fread(stringbuf, 1, size + 3, fp) != size + 3) {
1416  success = FALSE;
1417  L_ERROR("error reading string\n", procName);
1418  goto cleanup;
1419  }
1420  /* Remove the \n that was added by sarrayWriteStream() */
1421  stringbuf[size + 2] = '\0';
1422  /* Copy it in, skipping the 2 leading spaces */
1423  sarrayAddString(sa, stringbuf + 2, L_COPY);
1424  }
1425  ignore = fscanf(fp, "\n");
1426 
1427 cleanup:
1428  LEPT_FREE(stringbuf);
1429  if (!success) sarrayDestroy(&sa);
1430  return sa;
1431 }
1432 
1433 
1441 SARRAY *
1442 sarrayReadMem(const l_uint8 *data,
1443  size_t size)
1444 {
1445 FILE *fp;
1446 SARRAY *sa;
1447 
1448  PROCNAME("sarrayReadMem");
1449 
1450  if (!data)
1451  return (SARRAY *)ERROR_PTR("data not defined", procName, NULL);
1452  if ((fp = fopenReadFromMemory(data, size)) == NULL)
1453  return (SARRAY *)ERROR_PTR("stream not opened", procName, NULL);
1454 
1455  sa = sarrayReadStream(fp);
1456  fclose(fp);
1457  if (!sa) L_ERROR("sarray not read\n", procName);
1458  return sa;
1459 }
1460 
1461 
1469 l_int32
1470 sarrayWrite(const char *filename,
1471  SARRAY *sa)
1472 {
1473 l_int32 ret;
1474 FILE *fp;
1475 
1476  PROCNAME("sarrayWrite");
1477 
1478  if (!filename)
1479  return ERROR_INT("filename not defined", procName, 1);
1480  if (!sa)
1481  return ERROR_INT("sa not defined", procName, 1);
1482 
1483  if ((fp = fopenWriteStream(filename, "w")) == NULL)
1484  return ERROR_INT("stream not opened", procName, 1);
1485  ret = sarrayWriteStream(fp, sa);
1486  fclose(fp);
1487  if (ret)
1488  return ERROR_INT("sa not written to stream", procName, 1);
1489  return 0;
1490 }
1491 
1492 
1506 l_int32
1508  SARRAY *sa)
1509 {
1510 l_int32 i, n, len;
1511 
1512  PROCNAME("sarrayWriteStream");
1513 
1514  if (!fp)
1515  return ERROR_INT("stream not defined", procName, 1);
1516  if (!sa)
1517  return ERROR_INT("sa not defined", procName, 1);
1518 
1519  n = sarrayGetCount(sa);
1520  fprintf(fp, "\nSarray Version %d\n", SARRAY_VERSION_NUMBER);
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]);
1525  }
1526  fprintf(fp, "\n");
1527 
1528  return 0;
1529 }
1530 
1531 
1545 l_int32
1546 sarrayWriteMem(l_uint8 **pdata,
1547  size_t *psize,
1548  SARRAY *sa)
1549 {
1550 l_int32 ret;
1551 FILE *fp;
1552 
1553  PROCNAME("sarrayWriteMem");
1554 
1555  if (pdata) *pdata = NULL;
1556  if (psize) *psize = 0;
1557  if (!pdata)
1558  return ERROR_INT("&data not defined", procName, 1);
1559  if (!psize)
1560  return ERROR_INT("&size not defined", procName, 1);
1561  if (!sa)
1562  return ERROR_INT("sa not defined", procName, 1);
1563 
1564 #if HAVE_FMEMOPEN
1565  if ((fp = open_memstream((char **)pdata, psize)) == NULL)
1566  return ERROR_INT("stream not opened", procName, 1);
1567  ret = sarrayWriteStream(fp, sa);
1568 #else
1569  L_INFO("work-around: writing to a temp file\n", procName);
1570  #ifdef _WIN32
1571  if ((fp = fopenWriteWinTempfile()) == NULL)
1572  return ERROR_INT("tmpfile stream not opened", procName, 1);
1573  #else
1574  if ((fp = tmpfile()) == NULL)
1575  return ERROR_INT("tmpfile stream not opened", procName, 1);
1576  #endif /* _WIN32 */
1577  ret = sarrayWriteStream(fp, sa);
1578  rewind(fp);
1579  *pdata = l_binaryReadStream(fp, psize);
1580 #endif /* HAVE_FMEMOPEN */
1581  fclose(fp);
1582  return ret;
1583 }
1584 
1585 
1593 l_int32
1594 sarrayAppend(const char *filename,
1595  SARRAY *sa)
1596 {
1597 FILE *fp;
1598 
1599  PROCNAME("sarrayAppend");
1600 
1601  if (!filename)
1602  return ERROR_INT("filename not defined", procName, 1);
1603  if (!sa)
1604  return ERROR_INT("sa not defined", procName, 1);
1605 
1606  if ((fp = fopenWriteStream(filename, "a")) == NULL)
1607  return ERROR_INT("stream not opened", procName, 1);
1608  if (sarrayWriteStream(fp, sa)) {
1609  fclose(fp);
1610  return ERROR_INT("sa not appended to stream", procName, 1);
1611  }
1612 
1613  fclose(fp);
1614  return 0;
1615 }
1616 
1617 
1618 /*---------------------------------------------------------------------*
1619  * Directory filenames *
1620  *---------------------------------------------------------------------*/
1661 SARRAY *
1663  const char *substr,
1664  l_int32 numpre,
1665  l_int32 numpost,
1666  l_int32 maxnum)
1667 {
1668 l_int32 nfiles;
1669 SARRAY *sa, *saout;
1670 
1671  PROCNAME("getNumberedPathnamesInDirectory");
1672 
1673  if (!dirname)
1674  return (SARRAY *)ERROR_PTR("dirname not defined", procName, NULL);
1675 
1676  if ((sa = getSortedPathnamesInDirectory(dirname, substr, 0, 0)) == NULL)
1677  return (SARRAY *)ERROR_PTR("sa not made", procName, NULL);
1678  if ((nfiles = sarrayGetCount(sa)) == 0) {
1679  sarrayDestroy(&sa);
1680  return sarrayCreate(1);
1681  }
1682 
1683  saout = convertSortedToNumberedPathnames(sa, numpre, numpost, maxnum);
1684  sarrayDestroy(&sa);
1685  return saout;
1686 }
1687 
1688 
1709 SARRAY *
1710 getSortedPathnamesInDirectory(const char *dirname,
1711  const char *substr,
1712  l_int32 first,
1713  l_int32 nfiles)
1714 {
1715 char *fname, *fullname;
1716 l_int32 i, n, last;
1717 SARRAY *sa, *safiles, *saout;
1718 
1719  PROCNAME("getSortedPathnamesInDirectory");
1720 
1721  if (!dirname)
1722  return (SARRAY *)ERROR_PTR("dirname not defined", procName, NULL);
1723 
1724  if ((sa = getFilenamesInDirectory(dirname)) == NULL)
1725  return (SARRAY *)ERROR_PTR("sa not made", procName, NULL);
1726  safiles = sarraySelectBySubstring(sa, substr);
1727  sarrayDestroy(&sa);
1728  n = sarrayGetCount(safiles);
1729  if (n == 0) {
1730  L_WARNING("no files found\n", procName);
1731  return safiles;
1732  }
1733 
1734  sarraySort(safiles, safiles, L_SORT_INCREASING);
1735 
1736  first = L_MIN(L_MAX(first, 0), n - 1);
1737  if (nfiles == 0)
1738  nfiles = n - first;
1739  last = L_MIN(first + nfiles - 1, n - 1);
1740 
1741  saout = sarrayCreate(last - first + 1);
1742  for (i = first; i <= last; i++) {
1743  fname = sarrayGetString(safiles, i, L_NOCOPY);
1744  fullname = pathJoin(dirname, fname);
1745  sarrayAddString(saout, fullname, L_INSERT);
1746  }
1747 
1748  sarrayDestroy(&safiles);
1749  return saout;
1750 }
1751 
1752 
1769 SARRAY *
1771  l_int32 numpre,
1772  l_int32 numpost,
1773  l_int32 maxnum)
1774 {
1775 char *fname, *str;
1776 l_int32 i, nfiles, num, index;
1777 SARRAY *saout;
1778 
1779  PROCNAME("convertSortedToNumberedPathnames");
1780 
1781  if (!sa)
1782  return (SARRAY *)ERROR_PTR("sa not defined", procName, NULL);
1783  if ((nfiles = sarrayGetCount(sa)) == 0)
1784  return sarrayCreate(1);
1785 
1786  /* Find the last file in the sorted array that has a number
1787  * that (a) matches the count pattern and (b) does not
1788  * exceed %maxnum. %maxnum sets an upper limit on the size
1789  * of the sarray. */
1790  num = 0;
1791  for (i = nfiles - 1; i >= 0; i--) {
1792  fname = sarrayGetString(sa, i, L_NOCOPY);
1793  num = extractNumberFromFilename(fname, numpre, numpost);
1794  if (num < 0) continue;
1795  num = L_MIN(num + 1, maxnum);
1796  break;
1797  }
1798 
1799  if (num <= 0) /* none found */
1800  return sarrayCreate(1);
1801 
1802  /* Insert pathnames into the output sarray.
1803  * Ignore numbers that are out of the range of sarray. */
1804  saout = sarrayCreateInitialized(num, (char *)"");
1805  for (i = 0; i < nfiles; i++) {
1806  fname = sarrayGetString(sa, i, L_NOCOPY);
1807  index = extractNumberFromFilename(fname, numpre, numpost);
1808  if (index < 0 || index >= num) continue;
1809  str = sarrayGetString(saout, index, L_NOCOPY);
1810  if (str[0] != '\0')
1811  L_WARNING("\n Multiple files with same number: %d\n",
1812  procName, index);
1813  sarrayReplaceString(saout, index, fname, L_COPY);
1814  }
1815 
1816  return saout;
1817 }
1818 
1819 
1848 #ifndef _WIN32
1849 
1850 SARRAY *
1851 getFilenamesInDirectory(const char *dirname)
1852 {
1853 char *realdir, *name;
1854 l_int32 len;
1855 SARRAY *safiles;
1856 DIR *pdir;
1857 struct dirent *pdirentry;
1858 int dfd;
1859 struct stat st;
1860 
1861  PROCNAME("getFilenamesInDirectory");
1862 
1863  if (!dirname)
1864  return (SARRAY *)ERROR_PTR("dirname not defined", procName, NULL);
1865 
1866  realdir = genPathname(dirname, NULL);
1867  pdir = opendir(realdir);
1868  LEPT_FREE(realdir);
1869  if (!pdir)
1870  return (SARRAY *)ERROR_PTR("pdir not opened", procName, NULL);
1871  safiles = sarrayCreate(0);
1872  dfd = dirfd(pdir);
1873  while ((pdirentry = readdir(pdir))) {
1874 
1875  /* It's nice to ignore directories. */
1876  if ((0 == fstatat(dfd, pdirentry->d_name, &st, 0))
1877  && S_ISDIR(st.st_mode)) {
1878  continue;
1879  }
1880 
1881  /* Filter out "." and ".." if they're passed through */
1882  name = pdirentry->d_name;
1883  len = strlen(name);
1884  if (len == 1 && name[len - 1] == '.') continue;
1885  if (len == 2 && name[len - 1] == '.' && name[len - 2] == '.') continue;
1886  sarrayAddString(safiles, name, L_COPY);
1887  }
1888  closedir(pdir);
1889 
1890  return safiles;
1891 }
1892 
1893 #else /* _WIN32 */
1894 
1895  /* http://msdn2.microsoft.com/en-us/library/aa365200(VS.85).aspx */
1896 #include <windows.h>
1897 
1898 SARRAY *
1899 getFilenamesInDirectory(const char *dirname)
1900 {
1901 char *pszDir;
1902 char *realdir;
1903 HANDLE hFind = INVALID_HANDLE_VALUE;
1904 SARRAY *safiles;
1905 WIN32_FIND_DATAA ffd;
1906 
1907  PROCNAME("getFilenamesInDirectory");
1908 
1909  if (!dirname)
1910  return (SARRAY *)ERROR_PTR("dirname not defined", procName, NULL);
1911 
1912  realdir = genPathname(dirname, NULL);
1913  pszDir = stringJoin(realdir, "\\*");
1914  LEPT_FREE(realdir);
1915 
1916  if (strlen(pszDir) + 1 > MAX_PATH) {
1917  LEPT_FREE(pszDir);
1918  return (SARRAY *)ERROR_PTR("dirname is too long", procName, NULL);
1919  }
1920 
1921  if ((safiles = sarrayCreate(0)) == NULL) {
1922  LEPT_FREE(pszDir);
1923  return (SARRAY *)ERROR_PTR("safiles not made", procName, NULL);
1924  }
1925 
1926  hFind = FindFirstFileA(pszDir, &ffd);
1927  if (INVALID_HANDLE_VALUE == hFind) {
1928  sarrayDestroy(&safiles);
1929  LEPT_FREE(pszDir);
1930  return (SARRAY *)ERROR_PTR("hFind not opened", procName, NULL);
1931  }
1932 
1933  while (FindNextFileA(hFind, &ffd) != 0) {
1934  if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) /* skip dirs */
1935  continue;
1936  convertSepCharsInPath(ffd.cFileName, UNIX_PATH_SEPCHAR);
1937  sarrayAddString(safiles, ffd.cFileName, L_COPY);
1938  }
1939 
1940  FindClose(hFind);
1941  LEPT_FREE(pszDir);
1942  return safiles;
1943 }
1944 #endif /* _WIN32 */
l_int32 sarrayReplaceString(SARRAY *sa, l_int32 index, char *newstr, l_int32 copyflag)
sarrayReplaceString()
Definition: sarray1.c:551
char * sarrayToString(SARRAY *sa, l_int32 addnlflag)
sarrayToString()
Definition: sarray1.c:757
FILE * fp
Definition: regutils.h:119
SARRAY * sarrayCopy(SARRAY *sa)
sarrayCopy()
Definition: sarray1.c:387
char * genPathname(const char *dir, const char *fname)
genPathname()
Definition: utils2.c:2759
#define SARRAY_VERSION_NUMBER
Definition: array.h:113
Definition: pix.h:704
char * stringNew(const char *src)
stringNew()
Definition: utils2.c:202
char * sarrayRemoveString(SARRAY *sa, l_int32 index)
sarrayRemoveString()
Definition: sarray1.c:500
SARRAY * sarrayCreate(l_int32 n)
sarrayCreate()
Definition: sarray1.c:157
char * sarrayToStringRange(SARRAY *sa, l_int32 first, l_int32 nstrings, l_int32 addnlflag)
sarrayToStringRange()
Definition: sarray1.c:792
SARRAY * getFilenamesInDirectory(const char *dirname)
getFilenamesInDirectory()
Definition: sarray1.c:1851
FILE * fopenReadFromMemory(const l_uint8 *data, size_t size)
fopenReadFromMemory()
Definition: utils2.c:1670
l_int32 sarrayWriteStream(FILE *fp, SARRAY *sa)
sarrayWriteStream()
Definition: sarray1.c:1507
l_int32 sarrayAppendRange(SARRAY *sa1, SARRAY *sa2, l_int32 start, l_int32 end)
sarrayAppendRange()
Definition: sarray1.c:914
l_int32 sarrayWrite(const char *filename, SARRAY *sa)
sarrayWrite()
Definition: sarray1.c:1470
void * reallocNew(void **pindata, l_int32 oldsize, l_int32 newsize)
reallocNew()
Definition: utils2.c:1102
SARRAY * sarrayConvertWordsToLines(SARRAY *sa, l_int32 linesize)
sarrayConvertWordsToLines()
Definition: sarray1.c:1023
SARRAY * getNumberedPathnamesInDirectory(const char *dirname, const char *substr, l_int32 numpre, l_int32 numpost, l_int32 maxnum)
getNumberedPathnamesInDirectory()
Definition: sarray1.c:1662
Definition: array.h:116
static const l_int32 L_INSERT
Definition: pix.h:710
l_int32 n
Definition: array.h:119
l_int32 sarrayAppend(const char *filename, SARRAY *sa)
sarrayAppend()
Definition: sarray1.c:1594
l_int32 sarrayWriteMem(l_uint8 **pdata, size_t *psize, SARRAY *sa)
sarrayWriteMem()
Definition: sarray1.c:1546
l_int32 refcount
Definition: array.h:120
l_int32 sarrayChangeRefcount(SARRAY *sa, l_int32 delta)
sarrayChangeRefCount()
Definition: sarray1.c:720
FILE * fopenWriteWinTempfile()
fopenWriteWinTempfile()
Definition: utils2.c:1716
char * sarrayGetString(SARRAY *sa, l_int32 index, l_int32 copyflag)
sarrayGetString()
Definition: sarray1.c:675
l_int32 sarrayAddString(SARRAY *sa, char *string, l_int32 copyflag)
sarrayAddString()
Definition: sarray1.c:439
char ** sarrayGetArray(SARRAY *sa, l_int32 *pnalloc, l_int32 *pn)
sarrayGetArray()
Definition: sarray1.c:640
SARRAY * sarrayReadMem(const l_uint8 *data, size_t size)
sarrayReadMem()
Definition: sarray1.c:1442
SARRAY * sarrayCreateLinesFromString(const char *string, l_int32 blankflag)
sarrayCreateLinesFromString()
Definition: sarray1.c:270
SARRAY * getSortedPathnamesInDirectory(const char *dirname, const char *substr, l_int32 first, l_int32 nfiles)
getSortedPathnamesInDirectory()
Definition: sarray1.c:1710
l_int32 success
Definition: regutils.h:124
SARRAY * sarrayClone(SARRAY *sa)
sarrayClone()
Definition: sarray1.c:414
l_int32 extractNumberFromFilename(const char *fname, l_int32 numpre, l_int32 numpost)
l_makeTempFilename()
Definition: utils2.c:3020
SARRAY * sarraySelectByRange(SARRAY *sain, l_int32 first, l_int32 last)
sarraySelectByRange()
Definition: sarray1.c:1184
FILE * fopenWriteStream(const char *filename, const char *modestring)
fopenWriteStream()
Definition: utils2.c:1636
FILE * fopenReadStream(const char *filename)
fopenReadStream()
Definition: utils2.c:1593
static l_int32 sarrayExtendArray(SARRAY *sa)
sarrayExtendArray()
Definition: sarray1.c:475
l_uint8 * l_binaryReadStream(FILE *fp, size_t *pnbytes)
l_binaryReadStream()
Definition: utils2.c:1200
l_int32 sarrayGetCount(SARRAY *sa)
sarrayGetCount()
Definition: sarray1.c:615
l_int32 nalloc
Definition: array.h:118
l_int32 sarrayGetRefcount(SARRAY *sa)
sarrayGetRefCount()
Definition: sarray1.c:702
SARRAY * convertSortedToNumberedPathnames(SARRAY *sa, l_int32 numpre, l_int32 numpost, l_int32 maxnum)
convertSortedToNumberedPathnames()
Definition: sarray1.c:1770
SARRAY * sarrayRead(const char *filename)
sarrayRead()
Definition: sarray1.c:1338
SARRAY * sarraySort(SARRAY *saout, SARRAY *sain, l_int32 sortorder)
sarraySort()
Definition: sarray2.c:95
l_int32 convertSepCharsInPath(char *path, l_int32 type)
convertSepCharsInPath()
Definition: utils2.c:2696
char * pathJoin(const char *dir, const char *fname)
pathJoin()
Definition: utils2.c:2565
Definition: pix.h:705
char * stringJoin(const char *src1, const char *src2)
stringJoin()
Definition: utils2.c:451
l_int32 sarrayClear(SARRAY *sa)
sarrayClear()
Definition: sarray1.c:588
char * strtokSafe(char *cstr, const char *seps, char **psaveptr)
strtokSafe()
Definition: utils2.c:582
l_int32 sarrayPadToSameSize(SARRAY *sa1, SARRAY *sa2, char *padstring)
sarrayPadToSameSize()
Definition: sarray1.c:966
SARRAY * sarrayCreateInitialized(l_int32 n, char *initstr)
sarrayCreateInitialized()
Definition: sarray1.c:187
char ** array
Definition: array.h:121
l_int32 sarrayParseRange(SARRAY *sa, l_int32 start, l_int32 *pactualstart, l_int32 *pend, l_int32 *pnewstart, const char *substr, l_int32 loc)
sarrayParseRange()
Definition: sarray1.c:1253
l_int32 index
Definition: regutils.h:123
l_int32 arrayFindSequence(const l_uint8 *data, size_t datalen, const l_uint8 *sequence, size_t seqlen, l_int32 *poffset, l_int32 *pfound)
arrayFindSequence()
Definition: utils2.c:1033
SARRAY * sarrayReadStream(FILE *fp)
sarrayReadStream()
Definition: sarray1.c:1375
l_int32 sarrayJoin(SARRAY *sa1, SARRAY *sa2)
sarrayJoin()
Definition: sarray1.c:874
SARRAY * sarraySelectBySubstring(SARRAY *sain, const char *substr)
sarraySelectBySubstring()
Definition: sarray1.c:1138
SARRAY * sarrayCreateWordsFromString(const char *string)
sarrayCreateWordsFromString()
Definition: sarray1.c:220
static const l_int32 L_BUF_SIZE
Definition: classapp.c:55
void sarrayDestroy(SARRAY **psa)
sarrayDestroy()
Definition: sarray1.c:349