[1.5] Convert from DOS eol to UNIX eol
authorMathieu Malaterre <mathieu.malaterre@gmail.com>
Fri, 2 Mar 2012 09:50:30 +0000 (09:50 +0000)
committerMathieu Malaterre <mathieu.malaterre@gmail.com>
Fri, 2 Mar 2012 09:50:30 +0000 (09:50 +0000)
applications/codec/index.c
applications/codec/index.h
applications/codec/windirent.h

index 95cddb9167c69f72c103339ccdb98f5e28412935..22389c71cad62605e3a16c92c4c8d282ac528287 100644 (file)
-/*\r
- * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium\r
- * Copyright (c) 2002-2007, Professor Benoit Macq\r
- * Copyright (c) 2003-2007, Francois-Olivier Devaux \r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include <stdio.h>\r
-#include <math.h>\r
-#include <string.h>\r
-#include "openjpeg.h"\r
-#include "index.h"\r
-\r
-/* ------------------------------------------------------------------------------------ */\r
-\r
-/**\r
-Write a structured index to a file\r
-@param cstr_info Codestream information \r
-@param index Index filename\r
-@return Returns 0 if successful, returns 1 otherwise\r
-*/\r
-int write_index_file(opj_codestream_info_t *cstr_info, char *index) {\r
-       int tileno, compno, layno, resno, precno, pack_nb, x, y;\r
-       FILE *stream = NULL;\r
-       double total_disto = 0;\r
-/* UniPG>> */\r
-       int tilepartno;\r
-       char disto_on, numpix_on;\r
-\r
-#ifdef USE_JPWL\r
-       if (!strcmp(index, JPWL_PRIVATEINDEX_NAME))\r
-               return 0;\r
-#endif /* USE_JPWL */\r
-/* <<UniPG */\r
-\r
-       if (!cstr_info)         \r
-               return 1;\r
-\r
-       stream = fopen(index, "w");\r
-       if (!stream) {\r
-               fprintf(stderr, "failed to open index file [%s] for writing\n", index);\r
-               return 1;\r
-       }\r
-       \r
-       if (cstr_info->tile[0].distotile)\r
-               disto_on = 1;\r
-       else \r
-               disto_on = 0;\r
-\r
-       if (cstr_info->tile[0].numpix)\r
-               numpix_on = 1;\r
-       else \r
-               numpix_on = 0;\r
-\r
-       fprintf(stream, "%d %d\n", cstr_info->image_w, cstr_info->image_h);\r
-       fprintf(stream, "%d\n", cstr_info->prog);\r
-       fprintf(stream, "%d %d\n", cstr_info->tile_x, cstr_info->tile_y);\r
-       fprintf(stream, "%d %d\n", cstr_info->tw, cstr_info->th);\r
-       fprintf(stream, "%d\n", cstr_info->numcomps);\r
-       fprintf(stream, "%d\n", cstr_info->numlayers);\r
-       fprintf(stream, "%d\n", cstr_info->numdecompos[0]); /* based on component 0 */\r
-\r
-       for (resno = cstr_info->numdecompos[0]; resno >= 0; resno--) {\r
-               fprintf(stream, "[%d,%d] ", \r
-                       (1 << cstr_info->tile[0].pdx[resno]), (1 << cstr_info->tile[0].pdx[resno]));    /* based on tile 0 and component 0 */\r
-       }\r
-\r
-       fprintf(stream, "\n");\r
-/* UniPG>> */\r
-       fprintf(stream, "%d\n", cstr_info->main_head_start);\r
-/* <<UniPG */\r
-       fprintf(stream, "%d\n", cstr_info->main_head_end);\r
-       fprintf(stream, "%d\n", cstr_info->codestream_size);\r
-       \r
-       fprintf(stream, "\nINFO ON TILES\n");\r
-       fprintf(stream, "tileno start_pos  end_hd  end_tile   nbparts");\r
-       if (disto_on)\r
-               fprintf(stream,"         disto");\r
-       if (numpix_on)\r
-               fprintf(stream,"     nbpix");\r
-       if (disto_on && numpix_on)\r
-               fprintf(stream,"  disto/nbpix");\r
-       fprintf(stream, "\n");\r
-\r
-       for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {\r
-               fprintf(stream, "%4d %9d %9d %9d %9d", \r
-                       cstr_info->tile[tileno].tileno,\r
-                       cstr_info->tile[tileno].start_pos,\r
-                       cstr_info->tile[tileno].end_header,\r
-                       cstr_info->tile[tileno].end_pos,\r
-                       cstr_info->tile[tileno].num_tps);\r
-               if (disto_on)\r
-                       fprintf(stream," %9e", cstr_info->tile[tileno].distotile);\r
-               if (numpix_on)\r
-                       fprintf(stream," %9d", cstr_info->tile[tileno].numpix);\r
-               if (disto_on && numpix_on)\r
-                       fprintf(stream," %9e", cstr_info->tile[tileno].distotile / cstr_info->tile[tileno].numpix);\r
-               fprintf(stream, "\n");\r
-       }\r
-               \r
-       for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {\r
-               int start_pos, end_ph_pos, end_pos;\r
-               double disto = 0;\r
-               int max_numdecompos = 0;\r
-               pack_nb = 0;\r
-\r
-               for (compno = 0; compno < cstr_info->numcomps; compno++) {\r
-                       if (max_numdecompos < cstr_info->numdecompos[compno])\r
-                               max_numdecompos = cstr_info->numdecompos[compno];\r
-               }       \r
-\r
-               fprintf(stream, "\nTILE %d DETAILS\n", tileno); \r
-               fprintf(stream, "part_nb tileno  start_pack num_packs  start_pos end_tph_pos   end_pos\n");\r
-               for (tilepartno = 0; tilepartno < cstr_info->tile[tileno].num_tps; tilepartno++)\r
-                       fprintf(stream, "%4d %9d   %9d %9d  %9d %11d %9d\n",\r
-                               tilepartno, tileno,\r
-                               cstr_info->tile[tileno].tp[tilepartno].tp_start_pack,\r
-                               cstr_info->tile[tileno].tp[tilepartno].tp_numpacks,\r
-                               cstr_info->tile[tileno].tp[tilepartno].tp_start_pos,\r
-                               cstr_info->tile[tileno].tp[tilepartno].tp_end_header,\r
-                               cstr_info->tile[tileno].tp[tilepartno].tp_end_pos\r
-                               );\r
-\r
-               if (cstr_info->prog == LRCP) {  /* LRCP */\r
-                       fprintf(stream, "LRCP\npack_nb tileno layno resno compno precno start_pos end_ph_pos end_pos");\r
-                       if (disto_on)\r
-                               fprintf(stream, " disto");\r
-                       fprintf(stream,"\n");\r
-\r
-                       for (layno = 0; layno < cstr_info->numlayers; layno++) {\r
-                               for (resno = 0; resno < max_numdecompos + 1; resno++) {\r
-                                       for (compno = 0; compno < cstr_info->numcomps; compno++) {\r
-                                               int prec_max;\r
-                                               if (resno > cstr_info->numdecompos[compno])\r
-                                                       break;\r
-                                               prec_max = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];\r
-                                               for (precno = 0; precno < prec_max; precno++) {\r
-                                                       start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;\r
-                                                       end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos;\r
-                                                       end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;\r
-                                                       disto = cstr_info->tile[tileno].packet[pack_nb].disto;\r
-                                                       fprintf(stream, "%4d %6d %7d %5d %6d  %6d    %6d     %6d %7d",\r
-                                                               pack_nb, tileno, layno, resno, compno, precno, start_pos, end_ph_pos, end_pos);\r
-                                                       if (disto_on)\r
-                                                               fprintf(stream, " %8e", disto);\r
-                                                       fprintf(stream, "\n");\r
-                                                       total_disto += disto;\r
-                                                       pack_nb++;\r
-                                               }\r
-                                       }\r
-                               }\r
-                       }\r
-               } /* LRCP */\r
-\r
-               else if (cstr_info->prog == RLCP) {     /* RLCP */                      \r
-                       fprintf(stream, "RLCP\npack_nb tileno resno layno compno precno start_pos end_ph_pos end_pos\n");\r
-                       if (disto_on)\r
-                               fprintf(stream, " disto");\r
-                       fprintf(stream,"\n");\r
-\r
-                       for (resno = 0; resno < max_numdecompos + 1; resno++) {\r
-                               for (layno = 0; layno < cstr_info->numlayers; layno++) {\r
-                                       for (compno = 0; compno < cstr_info->numcomps; compno++) {\r
-                                               int prec_max; \r
-                                               if (resno > cstr_info->numdecompos[compno])\r
-                                                       break;\r
-                                               prec_max = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];\r
-                                               for (precno = 0; precno < prec_max; precno++) {\r
-                                                       start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;\r
-                                                       end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos;\r
-                                                       end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;\r
-                                                       disto = cstr_info->tile[tileno].packet[pack_nb].disto;\r
-                                                       fprintf(stream, "%4d %6d %5d %7d %6d %6d %9d   %9d %7d",\r
-                                                               pack_nb, tileno, resno, layno, compno, precno, start_pos, end_ph_pos, end_pos);\r
-                                                       if (disto_on)\r
-                                                               fprintf(stream, " %8e", disto);\r
-                                                       fprintf(stream, "\n");\r
-                                                       total_disto += disto;\r
-                                                       pack_nb++;\r
-                                               }\r
-                                       }\r
-                               }\r
-                       }\r
-               } /* RLCP */\r
-\r
-               else if (cstr_info->prog == RPCL) {     /* RPCL */\r
-\r
-                       fprintf(stream, "RPCL\npack_nb tileno resno precno compno layno start_pos end_ph_pos end_pos"); \r
-                       if (disto_on)\r
-                               fprintf(stream, " disto");\r
-                       fprintf(stream,"\n");\r
-\r
-                       for (resno = 0; resno < max_numdecompos + 1; resno++) {\r
-                               int numprec = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];\r
-                               for (precno = 0; precno < numprec; precno++) {                                                          \r
-                                       /* I suppose components have same XRsiz, YRsiz */\r
-                                       int x0 = cstr_info->tile_Ox + tileno - (int)floor((float)tileno/(float)cstr_info->tw ) * cstr_info->tw * cstr_info->tile_x;\r
-                                       int y0 = cstr_info->tile_Ox + (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tile_y;\r
-                                       int x1 = x0 + cstr_info->tile_x;\r
-                                       int y1 = y0 + cstr_info->tile_y;\r
-                                       for (compno = 0; compno < cstr_info->numcomps; compno++) {                                      \r
-                                               int pcnx = cstr_info->tile[tileno].pw[resno];\r
-                                               int pcx = (int) pow( 2, cstr_info->tile[tileno].pdx[resno] + cstr_info->numdecompos[compno] - resno );\r
-                                               int pcy = (int) pow( 2, cstr_info->tile[tileno].pdy[resno] + cstr_info->numdecompos[compno] - resno );\r
-                                               int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;\r
-                                               int precno_y = (int) floor( (float)precno/(float)pcnx );\r
-                                               if (resno > cstr_info->numdecompos[compno])\r
-                                                       break;\r
-                                               for(y = y0; y < y1; y++) {                                                      \r
-                                                       if (precno_y*pcy == y ) {\r
-                                                               for (x = x0; x < x1; x++) {                                                                     \r
-                                                                       if (precno_x*pcx == x ) {\r
-                                                                               for (layno = 0; layno < cstr_info->numlayers; layno++) {\r
-                                                                                       start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;\r
-                                                                                       end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos;\r
-                                                                                       end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;\r
-                                                                                       disto = cstr_info->tile[tileno].packet[pack_nb].disto;\r
-                                                                                       fprintf(stream, "%4d %6d %5d %6d %6d %7d %9d   %9d %7d",\r
-                                                                                               pack_nb, tileno, resno, precno, compno, layno, start_pos, end_ph_pos, end_pos); \r
-                                                                                       if (disto_on)\r
-                                                                                               fprintf(stream, " %8e", disto);\r
-                                                                                       fprintf(stream, "\n");\r
-                                                                                       total_disto += disto;\r
-                                                                                       pack_nb++; \r
-                                                                               }\r
-                                                                       }\r
-                                                               }/* x = x0..x1 */\r
-                                                       } \r
-                                               }  /* y = y0..y1 */\r
-                                       } /* precno */\r
-                               } /* compno */\r
-                       } /* resno */\r
-               } /* RPCL */\r
-\r
-               else if (cstr_info->prog == PCRL) {     /* PCRL */\r
-                       /* I suppose components have same XRsiz, YRsiz */\r
-                       int x0 = cstr_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tw * cstr_info->tile_x;\r
-                       int y0 = cstr_info->tile_Ox + (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tile_y;\r
-                       int x1 = x0 + cstr_info->tile_x;\r
-                       int y1 = y0 + cstr_info->tile_y;\r
-\r
-                       /* Count the maximum number of precincts */\r
-                       int max_numprec = 0;\r
-                       for (resno = 0; resno < max_numdecompos + 1; resno++) {\r
-                               int numprec = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];\r
-                               if (numprec > max_numprec)\r
-                                       max_numprec = numprec;\r
-                       }\r
-\r
-                       fprintf(stream, "PCRL\npack_nb tileno precno compno resno layno start_pos end_ph_pos end_pos"); \r
-                       if (disto_on)\r
-                               fprintf(stream, " disto");\r
-                       fprintf(stream,"\n");\r
-\r
-                       for (precno = 0; precno < max_numprec; precno++) {\r
-                               for (compno = 0; compno < cstr_info->numcomps; compno++) {\r
-                                       for (resno = 0; resno < cstr_info->numdecompos[compno] + 1; resno++) {\r
-                                               int numprec = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];\r
-                                               int pcnx = cstr_info->tile[tileno].pw[resno];\r
-                                               int pcx = (int) pow( 2, cstr_info->tile[tileno].pdx[resno] + cstr_info->numdecompos[compno] - resno );\r
-                                               int pcy = (int) pow( 2, cstr_info->tile[tileno].pdy[resno] + cstr_info->numdecompos[compno] - resno );\r
-                                               int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;\r
-                                               int precno_y = (int) floor( (float)precno/(float)pcnx );\r
-                                               if (precno >= numprec)\r
-                                                       continue;\r
-                                               for(y = y0; y < y1; y++) {                                                      \r
-                                                       if (precno_y*pcy == y ) {\r
-                                                               for (x = x0; x < x1; x++) {                                                                     \r
-                                                                       if (precno_x*pcx == x ) {\r
-                                                                               for (layno = 0; layno < cstr_info->numlayers; layno++) {\r
-                                                                                       start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;\r
-                                                                                       end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos;\r
-                                                                                       end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;\r
-                                                                                       disto = cstr_info->tile[tileno].packet[pack_nb].disto;\r
-                                                                                       fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d   %9d %7d",\r
-                                                                                               pack_nb, tileno, precno, compno, resno, layno, start_pos, end_ph_pos, end_pos); \r
-                                                                                       if (disto_on)\r
-                                                                                               fprintf(stream, " %8e", disto);\r
-                                                                                       fprintf(stream, "\n");\r
-                                                                                       total_disto += disto;\r
-                                                                                       pack_nb++; \r
-                                                                               }\r
-                                                                       }\r
-                                                               }/* x = x0..x1 */\r
-                                                       } \r
-                                               }  /* y = y0..y1 */\r
-                                       } /* resno */\r
-                               } /* compno */\r
-                       } /* precno */\r
-               } /* PCRL */\r
-\r
-               else {  /* CPRL */\r
-                       /* Count the maximum number of precincts */\r
-                       int max_numprec = 0;\r
-                       for (resno = 0; resno < max_numdecompos + 1; resno++) {\r
-                               int numprec = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];\r
-                               if (numprec > max_numprec)\r
-                                       max_numprec = numprec;\r
-                       }\r
-\r
-                       fprintf(stream, "CPRL\npack_nb tileno compno precno resno layno start_pos end_ph_pos end_pos"); \r
-                       if (disto_on)\r
-                               fprintf(stream, " disto");\r
-                       fprintf(stream,"\n");\r
-\r
-                       for (compno = 0; compno < cstr_info->numcomps; compno++) {\r
-                               /* I suppose components have same XRsiz, YRsiz */\r
-                               int x0 = cstr_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tw * cstr_info->tile_x;\r
-                               int y0 = cstr_info->tile_Ox + (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tile_y;\r
-                               int x1 = x0 + cstr_info->tile_x;\r
-                               int y1 = y0 + cstr_info->tile_y;\r
-\r
-                               for (precno = 0; precno < max_numprec; precno++) {\r
-                                       for (resno = 0; resno < cstr_info->numdecompos[compno] + 1; resno++) {\r
-                                               int numprec = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];\r
-                                               int pcnx = cstr_info->tile[tileno].pw[resno];\r
-                                               int pcx = (int) pow( 2, cstr_info->tile[tileno].pdx[resno] + cstr_info->numdecompos[compno] - resno );\r
-                                               int pcy = (int) pow( 2, cstr_info->tile[tileno].pdy[resno] + cstr_info->numdecompos[compno] - resno );\r
-                                               int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;\r
-                                               int precno_y = (int) floor( (float)precno/(float)pcnx );\r
-                                               if (precno >= numprec)\r
-                                                       continue;\r
-\r
-                                               for(y = y0; y < y1; y++) {\r
-                                                       if (precno_y*pcy == y ) {\r
-                                                               for (x = x0; x < x1; x++) {\r
-                                                                       if (precno_x*pcx == x ) {\r
-                                                                               for (layno = 0; layno < cstr_info->numlayers; layno++) {\r
-                                                                                       start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;\r
-                                                                                       end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos;\r
-                                                                                       end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;\r
-                                                                                       disto = cstr_info->tile[tileno].packet[pack_nb].disto;\r
-                                                                                       fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d   %9d %7d",\r
-                                                                                               pack_nb, tileno, compno, precno, resno, layno, start_pos, end_ph_pos, end_pos); \r
-                                                                                       if (disto_on)\r
-                                                                                               fprintf(stream, " %8e", disto);\r
-                                                                                       fprintf(stream, "\n");\r
-                                                                                       total_disto += disto;\r
-                                                                                       pack_nb++; \r
-                                                                               }\r
-                                                                       }\r
-                                                               }/* x = x0..x1 */\r
-                                                       }\r
-                                               } /* y = y0..y1 */\r
-                                       } /* resno */\r
-                               } /* precno */\r
-                       } /* compno */\r
-               } /* CPRL */   \r
-       } /* tileno */\r
-       \r
-       if (disto_on) {\r
-               fprintf(stream, "%8e\n", cstr_info->D_max); /* SE max */        \r
-               fprintf(stream, "%.8e\n", total_disto); /* SE totale */\r
-       }\r
-/* UniPG>> */\r
-       /* print the markers' list */\r
-       if (cstr_info->marknum) {\r
-               fprintf(stream, "\nMARKER LIST\n");\r
-               fprintf(stream, "%d\n", cstr_info->marknum);\r
-               fprintf(stream, "type\tstart_pos    length\n");\r
-               for (x = 0; x < cstr_info->marknum; x++)\r
-                       fprintf(stream, "%X\t%9d %9d\n", cstr_info->marker[x].type, cstr_info->marker[x].pos, cstr_info->marker[x].len);\r
-       }\r
-/* <<UniPG */\r
-       fclose(stream);\r
-\r
-       fprintf(stderr,"Generated index file %s\n", index);\r
-\r
-       return 0;\r
-}\r
+/*
+ * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2007, Professor Benoit Macq
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux 
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <math.h>
+#include <string.h>
+#include "openjpeg.h"
+#include "index.h"
+
+/* ------------------------------------------------------------------------------------ */
+
+/**
+Write a structured index to a file
+@param cstr_info Codestream information 
+@param index Index filename
+@return Returns 0 if successful, returns 1 otherwise
+*/
+int write_index_file(opj_codestream_info_t *cstr_info, char *index) {
+       int tileno, compno, layno, resno, precno, pack_nb, x, y;
+       FILE *stream = NULL;
+       double total_disto = 0;
+/* UniPG>> */
+       int tilepartno;
+       char disto_on, numpix_on;
+
+#ifdef USE_JPWL
+       if (!strcmp(index, JPWL_PRIVATEINDEX_NAME))
+               return 0;
+#endif /* USE_JPWL */
+/* <<UniPG */
+
+       if (!cstr_info)         
+               return 1;
+
+       stream = fopen(index, "w");
+       if (!stream) {
+               fprintf(stderr, "failed to open index file [%s] for writing\n", index);
+               return 1;
+       }
+       
+       if (cstr_info->tile[0].distotile)
+               disto_on = 1;
+       else 
+               disto_on = 0;
+
+       if (cstr_info->tile[0].numpix)
+               numpix_on = 1;
+       else 
+               numpix_on = 0;
+
+       fprintf(stream, "%d %d\n", cstr_info->image_w, cstr_info->image_h);
+       fprintf(stream, "%d\n", cstr_info->prog);
+       fprintf(stream, "%d %d\n", cstr_info->tile_x, cstr_info->tile_y);
+       fprintf(stream, "%d %d\n", cstr_info->tw, cstr_info->th);
+       fprintf(stream, "%d\n", cstr_info->numcomps);
+       fprintf(stream, "%d\n", cstr_info->numlayers);
+       fprintf(stream, "%d\n", cstr_info->numdecompos[0]); /* based on component 0 */
+
+       for (resno = cstr_info->numdecompos[0]; resno >= 0; resno--) {
+               fprintf(stream, "[%d,%d] ", 
+                       (1 << cstr_info->tile[0].pdx[resno]), (1 << cstr_info->tile[0].pdx[resno]));    /* based on tile 0 and component 0 */
+       }
+
+       fprintf(stream, "\n");
+/* UniPG>> */
+       fprintf(stream, "%d\n", cstr_info->main_head_start);
+/* <<UniPG */
+       fprintf(stream, "%d\n", cstr_info->main_head_end);
+       fprintf(stream, "%d\n", cstr_info->codestream_size);
+       
+       fprintf(stream, "\nINFO ON TILES\n");
+       fprintf(stream, "tileno start_pos  end_hd  end_tile   nbparts");
+       if (disto_on)
+               fprintf(stream,"         disto");
+       if (numpix_on)
+               fprintf(stream,"     nbpix");
+       if (disto_on && numpix_on)
+               fprintf(stream,"  disto/nbpix");
+       fprintf(stream, "\n");
+
+       for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
+               fprintf(stream, "%4d %9d %9d %9d %9d", 
+                       cstr_info->tile[tileno].tileno,
+                       cstr_info->tile[tileno].start_pos,
+                       cstr_info->tile[tileno].end_header,
+                       cstr_info->tile[tileno].end_pos,
+                       cstr_info->tile[tileno].num_tps);
+               if (disto_on)
+                       fprintf(stream," %9e", cstr_info->tile[tileno].distotile);
+               if (numpix_on)
+                       fprintf(stream," %9d", cstr_info->tile[tileno].numpix);
+               if (disto_on && numpix_on)
+                       fprintf(stream," %9e", cstr_info->tile[tileno].distotile / cstr_info->tile[tileno].numpix);
+               fprintf(stream, "\n");
+       }
+               
+       for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
+               int start_pos, end_ph_pos, end_pos;
+               double disto = 0;
+               int max_numdecompos = 0;
+               pack_nb = 0;
+
+               for (compno = 0; compno < cstr_info->numcomps; compno++) {
+                       if (max_numdecompos < cstr_info->numdecompos[compno])
+                               max_numdecompos = cstr_info->numdecompos[compno];
+               }       
+
+               fprintf(stream, "\nTILE %d DETAILS\n", tileno); 
+               fprintf(stream, "part_nb tileno  start_pack num_packs  start_pos end_tph_pos   end_pos\n");
+               for (tilepartno = 0; tilepartno < cstr_info->tile[tileno].num_tps; tilepartno++)
+                       fprintf(stream, "%4d %9d   %9d %9d  %9d %11d %9d\n",
+                               tilepartno, tileno,
+                               cstr_info->tile[tileno].tp[tilepartno].tp_start_pack,
+                               cstr_info->tile[tileno].tp[tilepartno].tp_numpacks,
+                               cstr_info->tile[tileno].tp[tilepartno].tp_start_pos,
+                               cstr_info->tile[tileno].tp[tilepartno].tp_end_header,
+                               cstr_info->tile[tileno].tp[tilepartno].tp_end_pos
+                               );
+
+               if (cstr_info->prog == LRCP) {  /* LRCP */
+                       fprintf(stream, "LRCP\npack_nb tileno layno resno compno precno start_pos end_ph_pos end_pos");
+                       if (disto_on)
+                               fprintf(stream, " disto");
+                       fprintf(stream,"\n");
+
+                       for (layno = 0; layno < cstr_info->numlayers; layno++) {
+                               for (resno = 0; resno < max_numdecompos + 1; resno++) {
+                                       for (compno = 0; compno < cstr_info->numcomps; compno++) {
+                                               int prec_max;
+                                               if (resno > cstr_info->numdecompos[compno])
+                                                       break;
+                                               prec_max = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];
+                                               for (precno = 0; precno < prec_max; precno++) {
+                                                       start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;
+                                                       end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos;
+                                                       end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;
+                                                       disto = cstr_info->tile[tileno].packet[pack_nb].disto;
+                                                       fprintf(stream, "%4d %6d %7d %5d %6d  %6d    %6d     %6d %7d",
+                                                               pack_nb, tileno, layno, resno, compno, precno, start_pos, end_ph_pos, end_pos);
+                                                       if (disto_on)
+                                                               fprintf(stream, " %8e", disto);
+                                                       fprintf(stream, "\n");
+                                                       total_disto += disto;
+                                                       pack_nb++;
+                                               }
+                                       }
+                               }
+                       }
+               } /* LRCP */
+
+               else if (cstr_info->prog == RLCP) {     /* RLCP */                      
+                       fprintf(stream, "RLCP\npack_nb tileno resno layno compno precno start_pos end_ph_pos end_pos\n");
+                       if (disto_on)
+                               fprintf(stream, " disto");
+                       fprintf(stream,"\n");
+
+                       for (resno = 0; resno < max_numdecompos + 1; resno++) {
+                               for (layno = 0; layno < cstr_info->numlayers; layno++) {
+                                       for (compno = 0; compno < cstr_info->numcomps; compno++) {
+                                               int prec_max; 
+                                               if (resno > cstr_info->numdecompos[compno])
+                                                       break;
+                                               prec_max = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];
+                                               for (precno = 0; precno < prec_max; precno++) {
+                                                       start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;
+                                                       end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos;
+                                                       end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;
+                                                       disto = cstr_info->tile[tileno].packet[pack_nb].disto;
+                                                       fprintf(stream, "%4d %6d %5d %7d %6d %6d %9d   %9d %7d",
+                                                               pack_nb, tileno, resno, layno, compno, precno, start_pos, end_ph_pos, end_pos);
+                                                       if (disto_on)
+                                                               fprintf(stream, " %8e", disto);
+                                                       fprintf(stream, "\n");
+                                                       total_disto += disto;
+                                                       pack_nb++;
+                                               }
+                                       }
+                               }
+                       }
+               } /* RLCP */
+
+               else if (cstr_info->prog == RPCL) {     /* RPCL */
+
+                       fprintf(stream, "RPCL\npack_nb tileno resno precno compno layno start_pos end_ph_pos end_pos"); 
+                       if (disto_on)
+                               fprintf(stream, " disto");
+                       fprintf(stream,"\n");
+
+                       for (resno = 0; resno < max_numdecompos + 1; resno++) {
+                               int numprec = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];
+                               for (precno = 0; precno < numprec; precno++) {                                                          
+                                       /* I suppose components have same XRsiz, YRsiz */
+                                       int x0 = cstr_info->tile_Ox + tileno - (int)floor((float)tileno/(float)cstr_info->tw ) * cstr_info->tw * cstr_info->tile_x;
+                                       int y0 = cstr_info->tile_Ox + (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tile_y;
+                                       int x1 = x0 + cstr_info->tile_x;
+                                       int y1 = y0 + cstr_info->tile_y;
+                                       for (compno = 0; compno < cstr_info->numcomps; compno++) {                                      
+                                               int pcnx = cstr_info->tile[tileno].pw[resno];
+                                               int pcx = (int) pow( 2, cstr_info->tile[tileno].pdx[resno] + cstr_info->numdecompos[compno] - resno );
+                                               int pcy = (int) pow( 2, cstr_info->tile[tileno].pdy[resno] + cstr_info->numdecompos[compno] - resno );
+                                               int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
+                                               int precno_y = (int) floor( (float)precno/(float)pcnx );
+                                               if (resno > cstr_info->numdecompos[compno])
+                                                       break;
+                                               for(y = y0; y < y1; y++) {                                                      
+                                                       if (precno_y*pcy == y ) {
+                                                               for (x = x0; x < x1; x++) {                                                                     
+                                                                       if (precno_x*pcx == x ) {
+                                                                               for (layno = 0; layno < cstr_info->numlayers; layno++) {
+                                                                                       start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;
+                                                                                       end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos;
+                                                                                       end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;
+                                                                                       disto = cstr_info->tile[tileno].packet[pack_nb].disto;
+                                                                                       fprintf(stream, "%4d %6d %5d %6d %6d %7d %9d   %9d %7d",
+                                                                                               pack_nb, tileno, resno, precno, compno, layno, start_pos, end_ph_pos, end_pos); 
+                                                                                       if (disto_on)
+                                                                                               fprintf(stream, " %8e", disto);
+                                                                                       fprintf(stream, "\n");
+                                                                                       total_disto += disto;
+                                                                                       pack_nb++; 
+                                                                               }
+                                                                       }
+                                                               }/* x = x0..x1 */
+                                                       } 
+                                               }  /* y = y0..y1 */
+                                       } /* precno */
+                               } /* compno */
+                       } /* resno */
+               } /* RPCL */
+
+               else if (cstr_info->prog == PCRL) {     /* PCRL */
+                       /* I suppose components have same XRsiz, YRsiz */
+                       int x0 = cstr_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tw * cstr_info->tile_x;
+                       int y0 = cstr_info->tile_Ox + (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tile_y;
+                       int x1 = x0 + cstr_info->tile_x;
+                       int y1 = y0 + cstr_info->tile_y;
+
+                       /* Count the maximum number of precincts */
+                       int max_numprec = 0;
+                       for (resno = 0; resno < max_numdecompos + 1; resno++) {
+                               int numprec = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];
+                               if (numprec > max_numprec)
+                                       max_numprec = numprec;
+                       }
+
+                       fprintf(stream, "PCRL\npack_nb tileno precno compno resno layno start_pos end_ph_pos end_pos"); 
+                       if (disto_on)
+                               fprintf(stream, " disto");
+                       fprintf(stream,"\n");
+
+                       for (precno = 0; precno < max_numprec; precno++) {
+                               for (compno = 0; compno < cstr_info->numcomps; compno++) {
+                                       for (resno = 0; resno < cstr_info->numdecompos[compno] + 1; resno++) {
+                                               int numprec = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];
+                                               int pcnx = cstr_info->tile[tileno].pw[resno];
+                                               int pcx = (int) pow( 2, cstr_info->tile[tileno].pdx[resno] + cstr_info->numdecompos[compno] - resno );
+                                               int pcy = (int) pow( 2, cstr_info->tile[tileno].pdy[resno] + cstr_info->numdecompos[compno] - resno );
+                                               int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
+                                               int precno_y = (int) floor( (float)precno/(float)pcnx );
+                                               if (precno >= numprec)
+                                                       continue;
+                                               for(y = y0; y < y1; y++) {                                                      
+                                                       if (precno_y*pcy == y ) {
+                                                               for (x = x0; x < x1; x++) {                                                                     
+                                                                       if (precno_x*pcx == x ) {
+                                                                               for (layno = 0; layno < cstr_info->numlayers; layno++) {
+                                                                                       start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;
+                                                                                       end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos;
+                                                                                       end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;
+                                                                                       disto = cstr_info->tile[tileno].packet[pack_nb].disto;
+                                                                                       fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d   %9d %7d",
+                                                                                               pack_nb, tileno, precno, compno, resno, layno, start_pos, end_ph_pos, end_pos); 
+                                                                                       if (disto_on)
+                                                                                               fprintf(stream, " %8e", disto);
+                                                                                       fprintf(stream, "\n");
+                                                                                       total_disto += disto;
+                                                                                       pack_nb++; 
+                                                                               }
+                                                                       }
+                                                               }/* x = x0..x1 */
+                                                       } 
+                                               }  /* y = y0..y1 */
+                                       } /* resno */
+                               } /* compno */
+                       } /* precno */
+               } /* PCRL */
+
+               else {  /* CPRL */
+                       /* Count the maximum number of precincts */
+                       int max_numprec = 0;
+                       for (resno = 0; resno < max_numdecompos + 1; resno++) {
+                               int numprec = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];
+                               if (numprec > max_numprec)
+                                       max_numprec = numprec;
+                       }
+
+                       fprintf(stream, "CPRL\npack_nb tileno compno precno resno layno start_pos end_ph_pos end_pos"); 
+                       if (disto_on)
+                               fprintf(stream, " disto");
+                       fprintf(stream,"\n");
+
+                       for (compno = 0; compno < cstr_info->numcomps; compno++) {
+                               /* I suppose components have same XRsiz, YRsiz */
+                               int x0 = cstr_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tw * cstr_info->tile_x;
+                               int y0 = cstr_info->tile_Ox + (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tile_y;
+                               int x1 = x0 + cstr_info->tile_x;
+                               int y1 = y0 + cstr_info->tile_y;
+
+                               for (precno = 0; precno < max_numprec; precno++) {
+                                       for (resno = 0; resno < cstr_info->numdecompos[compno] + 1; resno++) {
+                                               int numprec = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];
+                                               int pcnx = cstr_info->tile[tileno].pw[resno];
+                                               int pcx = (int) pow( 2, cstr_info->tile[tileno].pdx[resno] + cstr_info->numdecompos[compno] - resno );
+                                               int pcy = (int) pow( 2, cstr_info->tile[tileno].pdy[resno] + cstr_info->numdecompos[compno] - resno );
+                                               int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
+                                               int precno_y = (int) floor( (float)precno/(float)pcnx );
+                                               if (precno >= numprec)
+                                                       continue;
+
+                                               for(y = y0; y < y1; y++) {
+                                                       if (precno_y*pcy == y ) {
+                                                               for (x = x0; x < x1; x++) {
+                                                                       if (precno_x*pcx == x ) {
+                                                                               for (layno = 0; layno < cstr_info->numlayers; layno++) {
+                                                                                       start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;
+                                                                                       end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos;
+                                                                                       end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;
+                                                                                       disto = cstr_info->tile[tileno].packet[pack_nb].disto;
+                                                                                       fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d   %9d %7d",
+                                                                                               pack_nb, tileno, compno, precno, resno, layno, start_pos, end_ph_pos, end_pos); 
+                                                                                       if (disto_on)
+                                                                                               fprintf(stream, " %8e", disto);
+                                                                                       fprintf(stream, "\n");
+                                                                                       total_disto += disto;
+                                                                                       pack_nb++; 
+                                                                               }
+                                                                       }
+                                                               }/* x = x0..x1 */
+                                                       }
+                                               } /* y = y0..y1 */
+                                       } /* resno */
+                               } /* precno */
+                       } /* compno */
+               } /* CPRL */   
+       } /* tileno */
+       
+       if (disto_on) {
+               fprintf(stream, "%8e\n", cstr_info->D_max); /* SE max */        
+               fprintf(stream, "%.8e\n", total_disto); /* SE totale */
+       }
+/* UniPG>> */
+       /* print the markers' list */
+       if (cstr_info->marknum) {
+               fprintf(stream, "\nMARKER LIST\n");
+               fprintf(stream, "%d\n", cstr_info->marknum);
+               fprintf(stream, "type\tstart_pos    length\n");
+               for (x = 0; x < cstr_info->marknum; x++)
+                       fprintf(stream, "%X\t%9d %9d\n", cstr_info->marker[x].type, cstr_info->marker[x].pos, cstr_info->marker[x].len);
+       }
+/* <<UniPG */
+       fclose(stream);
+
+       fprintf(stderr,"Generated index file %s\n", index);
+
+       return 0;
+}
index 29f673a10452e8159ec2e446d72265f7db2f3092..6c2231dd904dbffbbe84c6734fa970cea651e096 100644 (file)
@@ -1,49 +1,49 @@
-/*\r
- * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium\r
- * Copyright (c) 2002-2007, Professor Benoit Macq\r
- * Copyright (c) 2003-2007, Francois-Olivier Devaux\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#ifndef __J2K_INDEX_H\r
-#define __J2K_INDEX_H\r
-\r
-#ifdef __cplusplus\r
-extern "C" {\r
-#endif\r
-\r
-/**\r
-Write a structured index to a file\r
-@param cstr_info Codestream information \r
-@param index Index filename\r
-@return Returns 0 if successful, returns 1 otherwise\r
-*/\r
-int write_index_file(opj_codestream_info_t *cstr_info, char *index);\r
-\r
-#ifdef __cplusplus\r
-}\r
-#endif\r
-\r
-#endif /* __J2K_INDEX_H */\r
-\r
+/*
+ * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2007, Professor Benoit Macq
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __J2K_INDEX_H
+#define __J2K_INDEX_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+Write a structured index to a file
+@param cstr_info Codestream information 
+@param index Index filename
+@return Returns 0 if successful, returns 1 otherwise
+*/
+int write_index_file(opj_codestream_info_t *cstr_info, char *index);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __J2K_INDEX_H */
+
index 7dd7fef343acefe20683efa8ada16a06a6b3248a..944b7d719b9c848f7f3b2d41b2c7a3ea740bed4d 100644 (file)
-\r
-/*\r
- * uce-dirent.h - operating system independent dirent implementation\r
- * \r
- * Copyright (C) 1998-2002  Toni Ronkko\r
- * \r
- * Permission is hereby granted, free of charge, to any person obtaining\r
- * a copy of this software and associated documentation files (the\r
- * ``Software''), to deal in the Software without restriction, including\r
- * without limitation the rights to use, copy, modify, merge, publish,\r
- * distribute, sublicense, and/or sell copies of the Software, and to\r
- * permit persons to whom the Software is furnished to do so, subject to\r
- * the following conditions:\r
- * \r
- * The above copyright notice and this permission notice shall be included\r
- * in all copies or substantial portions of the Software.\r
- * \r
- * THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS\r
- * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\r
- * IN NO EVENT SHALL TONI RONKKO BE LIABLE FOR ANY CLAIM, DAMAGES OR\r
- * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,\r
- * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR\r
- * OTHER DEALINGS IN THE SOFTWARE.\r
- * \r
- * \r
- * May 28 1998, Toni Ronkko <tronkko@messi.uku.fi>\r
- *\r
- * $Id: uce-dirent.h,v 1.7 2002/05/13 10:48:35 tr Exp $\r
- *\r
- * $Log: uce-dirent.h,v $\r
- * Revision 1.7  2002/05/13 10:48:35  tr\r
- * embedded some source code directly to the header so that no source\r
- * modules need to be included in the MS Visual C project using the\r
- * interface, removed all the dependencies to other headers of the `uce'\r
- * library so that the header can be made public\r
- *\r
- * Revision 1.6  2002/04/12 16:22:04  tr\r
- * Unified Compiling Environment (UCE) replaced `std' library\r
- *\r
- * Revision 1.5  2001/07/20 16:33:40  tr\r
- * moved to `std' library and re-named defines accordingly\r
- *\r
- * Revision 1.4  2001/07/10 16:47:18  tronkko\r
- * revised comments\r
- *\r
- * Revision 1.3  2001/01/11 13:16:43  tr\r
- * using ``uce-machine.h'' for finding out defines such as `FREEBSD'\r
- *\r
- * Revision 1.2  2000/10/08 16:00:41  tr\r
- * copy of FreeBSD man page\r
- *\r
- * Revision 1.1  2000/07/10 05:53:16  tr\r
- * Initial revision\r
- *\r
- * Revision 1.2  1998/07/19 18:29:14  tr\r
- * Added error reporting capabilities and some asserts.\r
- *\r
- * Revision 1.1  1998/07/04 16:27:51  tr\r
- * Initial revision\r
- *\r
- * \r
- * MSVC 1.0 scans automatic dependencies incorrectly when your project\r
- * contains this very header.  The problem is that MSVC cannot handle\r
- * include directives inside #if..#endif block those are never entered.\r
- * Since this header ought to compile in many different operating systems,\r
- * there had to be several conditional blocks that are compiled only in\r
- * operating systems for what they were designed for.  MSVC 1.0 cannot\r
- * handle inclusion of sys/dir.h in a part that is compiled only in Apollo\r
- * operating system.  To fix the problem you need to insert DIR.H into\r
- * SYSINCL.DAT located in MSVC\BIN directory and restart visual C++.\r
- * Consult manuals for more informaton about the problem.\r
- *\r
- * Since many UNIX systems have dirent.h we assume to have one also.\r
- * However, if your UNIX system does not have dirent.h you can download one\r
- * for example at: http://ftp.uni-mannheim.de/ftp/GNU/dirent/dirent.tar.gz.\r
- * You can also see if you have one of dirent.h, direct.h, dir.h, ndir.h,\r
- * sys/dir.h and sys/ndir.h somewhere.  Try defining HAVE_DIRENT_H,\r
- * HAVE_DIRECT_H, HAVE_DIR_H, HAVE_NDIR_H, HAVE_SYS_DIR_H and\r
- * HAVE_SYS_NDIR_H according to the files found.\r
- */\r
-#ifndef DIRENT_H\r
-#define DIRENT_H\r
-#define DIRENT_H_INCLUDED\r
-\r
-/* find out platform */\r
-#if defined(MSDOS)                             /* MS-DOS */\r
-#elif defined(__MSDOS__)                       /* Turbo C/Borland */\r
-# define MSDOS\r
-#elif defined(__DOS__)                         /* Watcom */\r
-# define MSDOS\r
-#endif\r
-\r
-#if defined(WIN32)                             /* MS-Windows */\r
-#elif defined(__NT__)                          /* Watcom */\r
-# define WIN32\r
-#elif defined(_WIN32)                          /* Microsoft */\r
-# define WIN32\r
-#elif defined(__WIN32__)                       /* Borland */\r
-# define WIN32\r
-#endif\r
-\r
-/*\r
- * See what kind of dirent interface we have unless autoconf has already\r
- * determinated that.\r
- */\r
-#if !defined(HAVE_DIRENT_H) && !defined(HAVE_DIRECT_H) && !defined(HAVE_SYS_DIR_H) && !defined(HAVE_NDIR_H) && !defined(HAVE_SYS_NDIR_H) && !defined(HAVE_DIR_H)\r
-# if defined(_MSC_VER)                         /* Microsoft C/C++ */\r
-    /* no dirent.h */\r
-# elif defined(__MINGW32__)                    /* MinGW */\r
-    /* no dirent.h */\r
-# elif defined(__BORLANDC__)                   /* Borland C/C++ */\r
-#   define HAVE_DIRENT_H\r
-#   define VOID_CLOSEDIR\r
-# elif defined(__TURBOC__)                     /* Borland Turbo C */\r
-    /* no dirent.h */\r
-# elif defined(__WATCOMC__)                    /* Watcom C/C++ */\r
-#   define HAVE_DIRECT_H\r
-# elif defined(__apollo)                       /* Apollo */\r
-#   define HAVE_SYS_DIR_H\r
-# elif defined(__hpux)                         /* HP-UX */\r
-#   define HAVE_DIRENT_H\r
-# elif (defined(__alpha) || defined(__alpha__)) && !defined(__linux__)  /* Alpha OSF1 */\r
-#   error "not implemented"\r
-# elif defined(__sgi)                          /* Silicon Graphics */\r
-#   define HAVE_DIRENT_H\r
-# elif defined(sun) || defined(__sun)           /* Sun Solaris */\r
-#   define HAVE_DIRENT_H\r
-# elif defined(__FreeBSD__)                    /* FreeBSD */\r
-#   define HAVE_DIRENT_H\r
-# elif defined(__linux__)                      /* Linux */\r
-#   define HAVE_DIRENT_H\r
-# elif defined(__GNUC__)                       /* GNU C/C++ */\r
-#   define HAVE_DIRENT_H\r
-# else\r
-#   error "not implemented"\r
-# endif\r
-#endif\r
-\r
-/* include proper interface headers */\r
-#if defined(HAVE_DIRENT_H)\r
-# include <dirent.h>\r
-# ifdef FREEBSD\r
-#   define NAMLEN(dp) ((int)((dp)->d_namlen))\r
-# else\r
-#   define NAMLEN(dp) ((int)(strlen((dp)->d_name)))\r
-# endif\r
-\r
-#elif defined(HAVE_NDIR_H)\r
-# include <ndir.h>\r
-# define NAMLEN(dp) ((int)((dp)->d_namlen))\r
-\r
-#elif defined(HAVE_SYS_NDIR_H)\r
-# include <sys/ndir.h>\r
-# define NAMLEN(dp) ((int)((dp)->d_namlen))\r
-\r
-#elif defined(HAVE_DIRECT_H)\r
-# include <direct.h>\r
-# define NAMLEN(dp) ((int)((dp)->d_namlen))\r
-\r
-#elif defined(HAVE_DIR_H)\r
-# include <dir.h>\r
-# define NAMLEN(dp) ((int)((dp)->d_namlen))\r
-\r
-#elif defined(HAVE_SYS_DIR_H)\r
-# include <sys/types.h>\r
-# include <sys/dir.h>\r
-# ifndef dirent\r
-#   define dirent direct\r
-# endif\r
-# define NAMLEN(dp) ((int)((dp)->d_namlen))\r
-\r
-#elif defined(MSDOS) || defined(WIN32)\r
-\r
-  /* figure out type of underlaying directory interface to be used */\r
-# if defined(WIN32)\r
-#   define DIRENT_WIN32_INTERFACE\r
-# elif defined(MSDOS)\r
-#   define DIRENT_MSDOS_INTERFACE\r
-# else\r
-#   error "missing native dirent interface"\r
-# endif\r
-\r
-  /*** WIN32 specifics ***/\r
-# if defined(DIRENT_WIN32_INTERFACE)\r
-#   include <windows.h>\r
-#   if !defined(DIRENT_MAXNAMLEN)\r
-#     define DIRENT_MAXNAMLEN (MAX_PATH)\r
-#   endif\r
-\r
-\r
-  /*** MS-DOS specifics ***/\r
-# elif defined(DIRENT_MSDOS_INTERFACE)\r
-#   include <dos.h>\r
-\r
-    /* Borland defines file length macros in dir.h */\r
-#   if defined(__BORLANDC__)\r
-#     include <dir.h>\r
-#     if !defined(DIRENT_MAXNAMLEN)\r
-#       define DIRENT_MAXNAMLEN ((MAXFILE)+(MAXEXT))\r
-#     endif\r
-#     if !defined(_find_t)\r
-#       define _find_t find_t\r
-#     endif\r
-\r
-    /* Turbo C defines ffblk structure in dir.h */\r
-#   elif defined(__TURBOC__)\r
-#     include <dir.h>\r
-#     if !defined(DIRENT_MAXNAMLEN)\r
-#       define DIRENT_MAXNAMLEN ((MAXFILE)+(MAXEXT))\r
-#     endif\r
-#     define DIRENT_USE_FFBLK\r
-\r
-    /* MSVC */\r
-#   elif defined(_MSC_VER)\r
-#     if !defined(DIRENT_MAXNAMLEN)\r
-#       define DIRENT_MAXNAMLEN (12)\r
-#     endif\r
-\r
-    /* Watcom */\r
-#   elif defined(__WATCOMC__)\r
-#     if !defined(DIRENT_MAXNAMLEN)\r
-#       if defined(__OS2__) || defined(__NT__)\r
-#         define DIRENT_MAXNAMLEN (255)\r
-#       else\r
-#         define DIRENT_MAXNAMLEN (12)\r
-#       endif\r
-#     endif\r
-\r
-#   endif\r
-# endif\r
-\r
-  /*** generic MS-DOS and MS-Windows stuff ***/\r
-# if !defined(NAME_MAX) && defined(DIRENT_MAXNAMLEN)\r
-#   define NAME_MAX DIRENT_MAXNAMLEN\r
-# endif\r
-# if NAME_MAX < DIRENT_MAXNAMLEN\r
-#   error "assertion failed: NAME_MAX >= DIRENT_MAXNAMLEN"\r
-# endif\r
-\r
-\r
-  /*\r
-   * Substitute for real dirent structure.  Note that `d_name' field is a\r
-   * true character array although we have it copied in the implementation\r
-   * dependent data.  We could save some memory if we had declared `d_name'\r
-   * as a pointer refering the name within implementation dependent data.\r
-   * We have not done that since some code may rely on sizeof(d_name) to be\r
-   * something other than four.  Besides, directory entries are typically so\r
-   * small that it takes virtually no time to copy them from place to place.\r
-   */\r
-  typedef struct dirent {\r
-    char d_name[NAME_MAX + 1];\r
-\r
-    /*** Operating system specific part ***/\r
-# if defined(DIRENT_WIN32_INTERFACE)       /*WIN32*/\r
-    WIN32_FIND_DATA data;\r
-# elif defined(DIRENT_MSDOS_INTERFACE)     /*MSDOS*/\r
-#   if defined(DIRENT_USE_FFBLK)\r
-    struct ffblk data;\r
-#   else\r
-    struct _find_t data;\r
-#   endif\r
-# endif\r
-  } dirent;\r
-\r
-  /* DIR substitute structure containing directory name.  The name is\r
-   * essential for the operation of ``rewinndir'' function. */\r
-  typedef struct DIR {\r
-    char          *dirname;                    /* directory being scanned */\r
-    dirent        current;                     /* current entry */\r
-    int           dirent_filled;               /* is current un-processed? */\r
-\r
-  /*** Operating system specific part ***/\r
-#  if defined(DIRENT_WIN32_INTERFACE)\r
-    HANDLE        search_handle;\r
-#  elif defined(DIRENT_MSDOS_INTERFACE)\r
-#  endif\r
-  } DIR;\r
-\r
-# ifdef __cplusplus\r
-extern "C" {\r
-# endif\r
-\r
-/* supply prototypes for dirent functions */\r
-static DIR *opendir (const char *dirname);\r
-static struct dirent *readdir (DIR *dirp);\r
-static int closedir (DIR *dirp);\r
-static void rewinddir (DIR *dirp);\r
-\r
-/*\r
- * Implement dirent interface as static functions so that the user does not\r
- * need to change his project in any way to use dirent function.  With this\r
- * it is sufficient to include this very header from source modules using\r
- * dirent functions and the functions will be pulled in automatically.\r
- */\r
-#include <stdio.h>\r
-#include <stdlib.h>\r
-#include <string.h>\r
-#include <assert.h>\r
-#include <errno.h>\r
-\r
-/* use ffblk instead of _find_t if requested */\r
-#if defined(DIRENT_USE_FFBLK)\r
-# define _A_ARCH   (FA_ARCH)\r
-# define _A_HIDDEN (FA_HIDDEN)\r
-# define _A_NORMAL (0)\r
-# define _A_RDONLY (FA_RDONLY)\r
-# define _A_SUBDIR (FA_DIREC)\r
-# define _A_SYSTEM (FA_SYSTEM)\r
-# define _A_VOLID  (FA_LABEL)\r
-# define _dos_findnext(dest) findnext(dest)\r
-# define _dos_findfirst(name,flags,dest) findfirst(name,dest,flags)\r
-#endif\r
-\r
-static int _initdir (DIR *p);\r
-static const char *_getdirname (const struct dirent *dp);\r
-static void _setdirname (struct DIR *dirp);\r
-\r
-/*\r
- * <function name="opendir">\r
- * <intro>open directory stream for reading\r
- * <syntax>DIR *opendir (const char *dirname);\r
- *\r
- * <desc>Open named directory stream for read and return pointer to the\r
- * internal working area that is used for retrieving individual directory\r
- * entries.  The internal working area has no fields of your interest.\r
- *\r
- * <ret>Returns a pointer to the internal working area or NULL in case the \r
- * directory stream could not be opened.  Global `errno' variable will set\r
- * in case of error as follows:\r
- *\r
- * <table>\r
- * [EACESS  |Permission denied.\r
- * [EMFILE  |Too many open files used by the process.\r
- * [ENFILE  |Too many open files in system.\r
- * [ENOENT  |Directory does not exist.\r
- * [ENOMEM  |Insufficient memory.\r
- * [ENOTDIR |dirname does not refer to directory.  This value is not\r
- *           reliable on MS-DOS and MS-Windows platforms.  Many\r
- *           implementations return ENOENT even when the name refers to a\r
- *           file.]\r
- * </table>\r
- * </function>\r
- */\r
-static DIR *opendir(const char *dirname)\r
-{\r
-  DIR *dirp;\r
-  assert (dirname != NULL);\r
-  \r
-  dirp = (DIR*)malloc (sizeof (struct DIR));\r
-  if (dirp != NULL) {\r
-    char *p;\r
-    \r
-    /* allocate room for directory name */\r
-    dirp->dirname = (char*) malloc (strlen (dirname) + 1 + strlen ("\\*.*"));\r
-    if (dirp->dirname == NULL) {\r
-      /* failed to duplicate directory name.  errno set by malloc() */\r
-      free (dirp);\r
-      return NULL;\r
-    }\r
-    /* Copy directory name while appending directory separator and "*.*".\r
-     * Directory separator is not appended if the name already ends with\r
-     * drive or directory separator.  Directory separator is assumed to be\r
-     * '/' or '\' and drive separator is assumed to be ':'. */\r
-    strcpy (dirp->dirname, dirname);\r
-    p = strchr (dirp->dirname, '\0');\r
-    if (dirp->dirname < p  &&\r
-        *(p - 1) != '\\'  &&  *(p - 1) != '/'  &&  *(p - 1) != ':')\r
-    {\r
-      strcpy (p++, "\\");\r
-    }\r
-# ifdef DIRENT_WIN32_INTERFACE\r
-    strcpy (p, "*"); /*scan files with and without extension in win32*/\r
-# else\r
-    strcpy (p, "*.*"); /*scan files with and without extension in DOS*/\r
-# endif\r
-\r
-    /* open stream */\r
-    if (_initdir (dirp) == 0) {\r
-      /* initialization failed */\r
-      free (dirp->dirname);\r
-      free (dirp);\r
-      return NULL;\r
-    }\r
-  }\r
-  return dirp;\r
-}\r
-\r
-\r
-/*\r
- * <function name="readdir">\r
- * <intro>read a directory entry\r
- * <syntax>struct dirent *readdir (DIR *dirp);\r
- *\r
- * <desc>Read individual directory entry and return pointer to a structure\r
- * containing the name of the entry.  Individual directory entries returned\r
- * include normal files, sub-directories, pseudo-directories "." and ".."\r
- * and also volume labels, hidden files and system files in MS-DOS and\r
- * MS-Windows.   You might want to use stat(2) function to determinate which\r
- * one are you dealing with.  Many dirent implementations already contain\r
- * equivalent information in dirent structure but you cannot depend on\r
- * this.\r
- *\r
- * The dirent structure contains several system dependent fields that\r
- * generally have no interest to you.  The only interesting one is char\r
- * d_name[] that is also portable across different systems.  The d_name\r
- * field contains the name of the directory entry without leading path.\r
- * While d_name is portable across different systems the actual storage\r
- * capacity of d_name varies from system to system and there is no portable\r
- * way to find out it at compile time as different systems define the\r
- * capacity of d_name with different macros and some systems do not define\r
- * capacity at all (besides actual declaration of the field). If you really\r
- * need to find out storage capacity of d_name then you might want to try\r
- * NAME_MAX macro. The NAME_MAX is defined in POSIX standard althought\r
- * there are many MS-DOS and MS-Windows implementations those do not define\r
- * it.  There are also systems that declare d_name as "char d_name[1]" and\r
- * then allocate suitable amount of memory at run-time.  Thanks to Alain\r
- * Decamps (Alain.Decamps@advalvas.be) for pointing it out to me.\r
- *\r
- * This all leads to the fact that it is difficult to allocate space\r
- * for the directory names when the very same program is being compiled on\r
- * number of operating systems.  Therefore I suggest that you always\r
- * allocate space for directory names dynamically.\r
- *\r
- * <ret>\r
- * Returns a pointer to a structure containing name of the directory entry\r
- * in `d_name' field or NULL if there was an error.  In case of an error the\r
- * global `errno' variable will set as follows:\r
- *\r
- * <table>\r
- * [EBADF  |dir parameter refers to an invalid directory stream.  This value\r
- *          is not set reliably on all implementations.]\r
- * </table>\r
- * </function>\r
- */\r
-static struct dirent *\r
-readdir (DIR *dirp)\r
-{\r
-  assert(dirp != NULL);\r
-  if (dirp == NULL) {\r
-    errno = EBADF;\r
-    return NULL;\r
-  }\r
-\r
-#if defined(DIRENT_WIN32_INTERFACE)\r
-  if (dirp->search_handle == INVALID_HANDLE_VALUE) {\r
-    /* directory stream was opened/rewound incorrectly or it ended normally */\r
-    errno = EBADF;\r
-    return NULL;\r
-  }\r
-#endif\r
-\r
-  if (dirp->dirent_filled != 0) {\r
-    /*\r
-     * Directory entry has already been retrieved and there is no need to\r
-     * retrieve a new one.  Directory entry will be retrieved in advance\r
-     * when the user calls readdir function for the first time.  This is so\r
-     * because real dirent has separate functions for opening and reading\r
-     * the stream whereas Win32 and DOS dirents open the stream\r
-     * automatically when we retrieve the first file.  Therefore, we have to\r
-     * save the first file when opening the stream and later we have to\r
-     * return the saved entry when the user tries to read the first entry.\r
-     */\r
-    dirp->dirent_filled = 0;\r
-  } else {\r
-    /* fill in entry and return that */\r
-#if defined(DIRENT_WIN32_INTERFACE)\r
-    if (FindNextFile (dirp->search_handle, &dirp->current.data) == FALSE) {\r
-      /* Last file has been processed or an error occured */\r
-      FindClose (dirp->search_handle);\r
-      dirp->search_handle = INVALID_HANDLE_VALUE;\r
-      errno = ENOENT;\r
-      return NULL;\r
-    }\r
-\r
-# elif defined(DIRENT_MSDOS_INTERFACE)\r
-    if (_dos_findnext (&dirp->current.data) != 0) {\r
-      /* _dos_findnext and findnext will set errno to ENOENT when no\r
-       * more entries could be retrieved. */\r
-      return NULL;\r
-    }\r
-# endif\r
-\r
-    _setdirname (dirp);\r
-    assert (dirp->dirent_filled == 0);\r
-  }\r
-  return &dirp->current;\r
-}\r
-\r
-\r
-/*\r
- * <function name="closedir">\r
- * <intro>close directory stream.\r
- * <syntax>int closedir (DIR *dirp);\r
- *\r
- * <desc>Close directory stream opened by the `opendir' function.  Close of\r
- * directory stream invalidates the DIR structure as well as previously read\r
- * dirent entry.\r
- *\r
- * <ret>The function typically returns 0 on success and -1 on failure but\r
- * the function may be declared to return void on same systems.  At least\r
- * Borland C/C++ and some UNIX implementations use void as a return type.\r
- * The dirent wrapper tries to define VOID_CLOSEDIR whenever closedir is\r
- * known to return nothing.  The very same definition is made by the GNU\r
- * autoconf if you happen to use it.\r
- *\r
- * The global `errno' variable will set to EBADF in case of error.\r
- * </function>\r
- */\r
-static int\r
-closedir (DIR *dirp)\r
-{   \r
-  int retcode = 0;\r
-\r
-  /* make sure that dirp points to legal structure */\r
-  assert (dirp != NULL);\r
-  if (dirp == NULL) {\r
-    errno = EBADF;\r
-    return -1;\r
-  }\r
\r
-  /* free directory name and search handles */\r
-  if (dirp->dirname != NULL) free (dirp->dirname);\r
-\r
-#if defined(DIRENT_WIN32_INTERFACE)\r
-  if (dirp->search_handle != INVALID_HANDLE_VALUE) {\r
-    if (FindClose (dirp->search_handle) == FALSE) {\r
-      /* Unknown error */\r
-      retcode = -1;\r
-      errno = EBADF;\r
-    }\r
-  }\r
-#endif                     \r
-\r
-  /* clear dirp structure to make sure that it cannot be used anymore*/\r
-  memset (dirp, 0, sizeof (*dirp));\r
-# if defined(DIRENT_WIN32_INTERFACE)\r
-  dirp->search_handle = INVALID_HANDLE_VALUE;\r
-# endif\r
-\r
-  free (dirp);\r
-  return retcode;\r
-}\r
-\r
-\r
-/*\r
- * <function name="rewinddir">\r
- * <intro>rewind directory stream to the beginning\r
- * <syntax>void rewinddir (DIR *dirp);\r
- *\r
- * <desc>Rewind directory stream to the beginning so that the next call of\r
- * readdir() returns the very first directory entry again.  However, note\r
- * that next call of readdir() may not return the same directory entry as it\r
- * did in first time.  The directory stream may have been affected by newly\r
- * created files.\r
- *\r
- * Almost every dirent implementation ensure that rewinddir will update\r
- * the directory stream to reflect any changes made to the directory entries\r
- * since the previous ``opendir'' or ``rewinddir'' call.  Keep an eye on\r
- * this if your program depends on the feature.  I know at least one dirent\r
- * implementation where you are required to close and re-open the stream to\r
- * see the changes.\r
- *\r
- * <ret>Returns nothing.  If something went wrong while rewinding, you will\r
- * notice it later when you try to retrieve the first directory entry.\r
- */\r
-static void\r
-rewinddir (DIR *dirp)\r
-{   \r
-  /* make sure that dirp is legal */\r
-  assert (dirp != NULL);\r
-  if (dirp == NULL) {\r
-    errno = EBADF;\r
-    return;\r
-  }\r
-  assert (dirp->dirname != NULL);\r
-  \r
-  /* close previous stream */\r
-#if defined(DIRENT_WIN32_INTERFACE)\r
-  if (dirp->search_handle != INVALID_HANDLE_VALUE) {\r
-    if (FindClose (dirp->search_handle) == FALSE) {\r
-      /* Unknown error */\r
-      errno = EBADF;\r
-    }\r
-  }\r
-#endif\r
-\r
-  /* re-open previous stream */\r
-  if (_initdir (dirp) == 0) {\r
-    /* initialization failed but we cannot deal with error.  User will notice\r
-     * error later when she tries to retrieve first directory enty. */\r
-    /*EMPTY*/;\r
-  }\r
-}\r
-\r
-\r
-/*\r
- * Open native directory stream object and retrieve first file.\r
- * Be sure to close previous stream before opening new one.\r
- */\r
-static int\r
-_initdir (DIR *dirp)\r
-{ \r
-  assert (dirp != NULL);\r
-  assert (dirp->dirname != NULL);\r
-  dirp->dirent_filled = 0;\r
-\r
-# if defined(DIRENT_WIN32_INTERFACE)\r
-  /* Open stream and retrieve first file */\r
-  dirp->search_handle = FindFirstFile (dirp->dirname, &dirp->current.data);\r
-  if (dirp->search_handle == INVALID_HANDLE_VALUE) {\r
-    /* something went wrong but we don't know what.  GetLastError() could\r
-     * give us more information about the error, but then we should map\r
-     * the error code into errno. */\r
-    errno = ENOENT;\r
-    return 0;\r
-  }\r
-\r
-# elif defined(DIRENT_MSDOS_INTERFACE)\r
-  if (_dos_findfirst (dirp->dirname,\r
-          _A_SUBDIR | _A_RDONLY | _A_ARCH | _A_SYSTEM | _A_HIDDEN,\r
-          &dirp->current.data) != 0)\r
-  {\r
-    /* _dos_findfirst and findfirst will set errno to ENOENT when no \r
-     * more entries could be retrieved. */\r
-    return 0;\r
-  }\r
-# endif\r
-\r
-  /* initialize DIR and it's first entry */\r
-  _setdirname (dirp);\r
-  dirp->dirent_filled = 1;\r
-  return 1;\r
-}\r
-\r
-\r
-/*\r
- * Return implementation dependent name of the current directory entry.\r
- */\r
-static const char *\r
-_getdirname (const struct dirent *dp)\r
-{\r
-#if defined(DIRENT_WIN32_INTERFACE)\r
-  return dp->data.cFileName;\r
-  \r
-#elif defined(DIRENT_USE_FFBLK)\r
-  return dp->data.ff_name;\r
-  \r
-#else\r
-  return dp->data.name;\r
-#endif  \r
-}\r
-\r
-\r
-/*\r
- * Copy name of implementation dependent directory entry to the d_name field.\r
- */\r
-static void\r
-_setdirname (struct DIR *dirp) {\r
-  /* make sure that d_name is long enough */\r
-  assert (strlen (_getdirname (&dirp->current)) <= NAME_MAX);\r
-  \r
-  strncpy (dirp->current.d_name,\r
-      _getdirname (&dirp->current),\r
-      NAME_MAX);\r
-  dirp->current.d_name[NAME_MAX] = '\0'; /*char d_name[NAME_MAX+1]*/\r
-}\r
-  \r
-# ifdef __cplusplus\r
-}\r
-# endif\r
-# define NAMLEN(dp) ((int)(strlen((dp)->d_name)))\r
-\r
-#else\r
-# error "missing dirent interface"\r
-#endif\r
-\r
-\r
-#endif /*DIRENT_H*/\r
+
+/*
+ * uce-dirent.h - operating system independent dirent implementation
+ * 
+ * Copyright (C) 1998-2002  Toni Ronkko
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * ``Software''), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL TONI RONKKO BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ * 
+ * 
+ * May 28 1998, Toni Ronkko <tronkko@messi.uku.fi>
+ *
+ * $Id: uce-dirent.h,v 1.7 2002/05/13 10:48:35 tr Exp $
+ *
+ * $Log: uce-dirent.h,v $
+ * Revision 1.7  2002/05/13 10:48:35  tr
+ * embedded some source code directly to the header so that no source
+ * modules need to be included in the MS Visual C project using the
+ * interface, removed all the dependencies to other headers of the `uce'
+ * library so that the header can be made public
+ *
+ * Revision 1.6  2002/04/12 16:22:04  tr
+ * Unified Compiling Environment (UCE) replaced `std' library
+ *
+ * Revision 1.5  2001/07/20 16:33:40  tr
+ * moved to `std' library and re-named defines accordingly
+ *
+ * Revision 1.4  2001/07/10 16:47:18  tronkko
+ * revised comments
+ *
+ * Revision 1.3  2001/01/11 13:16:43  tr
+ * using ``uce-machine.h'' for finding out defines such as `FREEBSD'
+ *
+ * Revision 1.2  2000/10/08 16:00:41  tr
+ * copy of FreeBSD man page
+ *
+ * Revision 1.1  2000/07/10 05:53:16  tr
+ * Initial revision
+ *
+ * Revision 1.2  1998/07/19 18:29:14  tr
+ * Added error reporting capabilities and some asserts.
+ *
+ * Revision 1.1  1998/07/04 16:27:51  tr
+ * Initial revision
+ *
+ * 
+ * MSVC 1.0 scans automatic dependencies incorrectly when your project
+ * contains this very header.  The problem is that MSVC cannot handle
+ * include directives inside #if..#endif block those are never entered.
+ * Since this header ought to compile in many different operating systems,
+ * there had to be several conditional blocks that are compiled only in
+ * operating systems for what they were designed for.  MSVC 1.0 cannot
+ * handle inclusion of sys/dir.h in a part that is compiled only in Apollo
+ * operating system.  To fix the problem you need to insert DIR.H into
+ * SYSINCL.DAT located in MSVC\BIN directory and restart visual C++.
+ * Consult manuals for more informaton about the problem.
+ *
+ * Since many UNIX systems have dirent.h we assume to have one also.
+ * However, if your UNIX system does not have dirent.h you can download one
+ * for example at: http://ftp.uni-mannheim.de/ftp/GNU/dirent/dirent.tar.gz.
+ * You can also see if you have one of dirent.h, direct.h, dir.h, ndir.h,
+ * sys/dir.h and sys/ndir.h somewhere.  Try defining HAVE_DIRENT_H,
+ * HAVE_DIRECT_H, HAVE_DIR_H, HAVE_NDIR_H, HAVE_SYS_DIR_H and
+ * HAVE_SYS_NDIR_H according to the files found.
+ */
+#ifndef DIRENT_H
+#define DIRENT_H
+#define DIRENT_H_INCLUDED
+
+/* find out platform */
+#if defined(MSDOS)                             /* MS-DOS */
+#elif defined(__MSDOS__)                       /* Turbo C/Borland */
+# define MSDOS
+#elif defined(__DOS__)                         /* Watcom */
+# define MSDOS
+#endif
+
+#if defined(WIN32)                             /* MS-Windows */
+#elif defined(__NT__)                          /* Watcom */
+# define WIN32
+#elif defined(_WIN32)                          /* Microsoft */
+# define WIN32
+#elif defined(__WIN32__)                       /* Borland */
+# define WIN32
+#endif
+
+/*
+ * See what kind of dirent interface we have unless autoconf has already
+ * determinated that.
+ */
+#if !defined(HAVE_DIRENT_H) && !defined(HAVE_DIRECT_H) && !defined(HAVE_SYS_DIR_H) && !defined(HAVE_NDIR_H) && !defined(HAVE_SYS_NDIR_H) && !defined(HAVE_DIR_H)
+# if defined(_MSC_VER)                         /* Microsoft C/C++ */
+    /* no dirent.h */
+# elif defined(__MINGW32__)                    /* MinGW */
+    /* no dirent.h */
+# elif defined(__BORLANDC__)                   /* Borland C/C++ */
+#   define HAVE_DIRENT_H
+#   define VOID_CLOSEDIR
+# elif defined(__TURBOC__)                     /* Borland Turbo C */
+    /* no dirent.h */
+# elif defined(__WATCOMC__)                    /* Watcom C/C++ */
+#   define HAVE_DIRECT_H
+# elif defined(__apollo)                       /* Apollo */
+#   define HAVE_SYS_DIR_H
+# elif defined(__hpux)                         /* HP-UX */
+#   define HAVE_DIRENT_H
+# elif (defined(__alpha) || defined(__alpha__)) && !defined(__linux__)  /* Alpha OSF1 */
+#   error "not implemented"
+# elif defined(__sgi)                          /* Silicon Graphics */
+#   define HAVE_DIRENT_H
+# elif defined(sun) || defined(__sun)           /* Sun Solaris */
+#   define HAVE_DIRENT_H
+# elif defined(__FreeBSD__)                    /* FreeBSD */
+#   define HAVE_DIRENT_H
+# elif defined(__linux__)                      /* Linux */
+#   define HAVE_DIRENT_H
+# elif defined(__GNUC__)                       /* GNU C/C++ */
+#   define HAVE_DIRENT_H
+# else
+#   error "not implemented"
+# endif
+#endif
+
+/* include proper interface headers */
+#if defined(HAVE_DIRENT_H)
+# include <dirent.h>
+# ifdef FREEBSD
+#   define NAMLEN(dp) ((int)((dp)->d_namlen))
+# else
+#   define NAMLEN(dp) ((int)(strlen((dp)->d_name)))
+# endif
+
+#elif defined(HAVE_NDIR_H)
+# include <ndir.h>
+# define NAMLEN(dp) ((int)((dp)->d_namlen))
+
+#elif defined(HAVE_SYS_NDIR_H)
+# include <sys/ndir.h>
+# define NAMLEN(dp) ((int)((dp)->d_namlen))
+
+#elif defined(HAVE_DIRECT_H)
+# include <direct.h>
+# define NAMLEN(dp) ((int)((dp)->d_namlen))
+
+#elif defined(HAVE_DIR_H)
+# include <dir.h>
+# define NAMLEN(dp) ((int)((dp)->d_namlen))
+
+#elif defined(HAVE_SYS_DIR_H)
+# include <sys/types.h>
+# include <sys/dir.h>
+# ifndef dirent
+#   define dirent direct
+# endif
+# define NAMLEN(dp) ((int)((dp)->d_namlen))
+
+#elif defined(MSDOS) || defined(WIN32)
+
+  /* figure out type of underlaying directory interface to be used */
+# if defined(WIN32)
+#   define DIRENT_WIN32_INTERFACE
+# elif defined(MSDOS)
+#   define DIRENT_MSDOS_INTERFACE
+# else
+#   error "missing native dirent interface"
+# endif
+
+  /*** WIN32 specifics ***/
+# if defined(DIRENT_WIN32_INTERFACE)
+#   include <windows.h>
+#   if !defined(DIRENT_MAXNAMLEN)
+#     define DIRENT_MAXNAMLEN (MAX_PATH)
+#   endif
+
+
+  /*** MS-DOS specifics ***/
+# elif defined(DIRENT_MSDOS_INTERFACE)
+#   include <dos.h>
+
+    /* Borland defines file length macros in dir.h */
+#   if defined(__BORLANDC__)
+#     include <dir.h>
+#     if !defined(DIRENT_MAXNAMLEN)
+#       define DIRENT_MAXNAMLEN ((MAXFILE)+(MAXEXT))
+#     endif
+#     if !defined(_find_t)
+#       define _find_t find_t
+#     endif
+
+    /* Turbo C defines ffblk structure in dir.h */
+#   elif defined(__TURBOC__)
+#     include <dir.h>
+#     if !defined(DIRENT_MAXNAMLEN)
+#       define DIRENT_MAXNAMLEN ((MAXFILE)+(MAXEXT))
+#     endif
+#     define DIRENT_USE_FFBLK
+
+    /* MSVC */
+#   elif defined(_MSC_VER)
+#     if !defined(DIRENT_MAXNAMLEN)
+#       define DIRENT_MAXNAMLEN (12)
+#     endif
+
+    /* Watcom */
+#   elif defined(__WATCOMC__)
+#     if !defined(DIRENT_MAXNAMLEN)
+#       if defined(__OS2__) || defined(__NT__)
+#         define DIRENT_MAXNAMLEN (255)
+#       else
+#         define DIRENT_MAXNAMLEN (12)
+#       endif
+#     endif
+
+#   endif
+# endif
+
+  /*** generic MS-DOS and MS-Windows stuff ***/
+# if !defined(NAME_MAX) && defined(DIRENT_MAXNAMLEN)
+#   define NAME_MAX DIRENT_MAXNAMLEN
+# endif
+# if NAME_MAX < DIRENT_MAXNAMLEN
+#   error "assertion failed: NAME_MAX >= DIRENT_MAXNAMLEN"
+# endif
+
+
+  /*
+   * Substitute for real dirent structure.  Note that `d_name' field is a
+   * true character array although we have it copied in the implementation
+   * dependent data.  We could save some memory if we had declared `d_name'
+   * as a pointer refering the name within implementation dependent data.
+   * We have not done that since some code may rely on sizeof(d_name) to be
+   * something other than four.  Besides, directory entries are typically so
+   * small that it takes virtually no time to copy them from place to place.
+   */
+  typedef struct dirent {
+    char d_name[NAME_MAX + 1];
+
+    /*** Operating system specific part ***/
+# if defined(DIRENT_WIN32_INTERFACE)       /*WIN32*/
+    WIN32_FIND_DATA data;
+# elif defined(DIRENT_MSDOS_INTERFACE)     /*MSDOS*/
+#   if defined(DIRENT_USE_FFBLK)
+    struct ffblk data;
+#   else
+    struct _find_t data;
+#   endif
+# endif
+  } dirent;
+
+  /* DIR substitute structure containing directory name.  The name is
+   * essential for the operation of ``rewinndir'' function. */
+  typedef struct DIR {
+    char          *dirname;                    /* directory being scanned */
+    dirent        current;                     /* current entry */
+    int           dirent_filled;               /* is current un-processed? */
+
+  /*** Operating system specific part ***/
+#  if defined(DIRENT_WIN32_INTERFACE)
+    HANDLE        search_handle;
+#  elif defined(DIRENT_MSDOS_INTERFACE)
+#  endif
+  } DIR;
+
+# ifdef __cplusplus
+extern "C" {
+# endif
+
+/* supply prototypes for dirent functions */
+static DIR *opendir (const char *dirname);
+static struct dirent *readdir (DIR *dirp);
+static int closedir (DIR *dirp);
+static void rewinddir (DIR *dirp);
+
+/*
+ * Implement dirent interface as static functions so that the user does not
+ * need to change his project in any way to use dirent function.  With this
+ * it is sufficient to include this very header from source modules using
+ * dirent functions and the functions will be pulled in automatically.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <errno.h>
+
+/* use ffblk instead of _find_t if requested */
+#if defined(DIRENT_USE_FFBLK)
+# define _A_ARCH   (FA_ARCH)
+# define _A_HIDDEN (FA_HIDDEN)
+# define _A_NORMAL (0)
+# define _A_RDONLY (FA_RDONLY)
+# define _A_SUBDIR (FA_DIREC)
+# define _A_SYSTEM (FA_SYSTEM)
+# define _A_VOLID  (FA_LABEL)
+# define _dos_findnext(dest) findnext(dest)
+# define _dos_findfirst(name,flags,dest) findfirst(name,dest,flags)
+#endif
+
+static int _initdir (DIR *p);
+static const char *_getdirname (const struct dirent *dp);
+static void _setdirname (struct DIR *dirp);
+
+/*
+ * <function name="opendir">
+ * <intro>open directory stream for reading
+ * <syntax>DIR *opendir (const char *dirname);
+ *
+ * <desc>Open named directory stream for read and return pointer to the
+ * internal working area that is used for retrieving individual directory
+ * entries.  The internal working area has no fields of your interest.
+ *
+ * <ret>Returns a pointer to the internal working area or NULL in case the 
+ * directory stream could not be opened.  Global `errno' variable will set
+ * in case of error as follows:
+ *
+ * <table>
+ * [EACESS  |Permission denied.
+ * [EMFILE  |Too many open files used by the process.
+ * [ENFILE  |Too many open files in system.
+ * [ENOENT  |Directory does not exist.
+ * [ENOMEM  |Insufficient memory.
+ * [ENOTDIR |dirname does not refer to directory.  This value is not
+ *           reliable on MS-DOS and MS-Windows platforms.  Many
+ *           implementations return ENOENT even when the name refers to a
+ *           file.]
+ * </table>
+ * </function>
+ */
+static DIR *opendir(const char *dirname)
+{
+  DIR *dirp;
+  assert (dirname != NULL);
+  
+  dirp = (DIR*)malloc (sizeof (struct DIR));
+  if (dirp != NULL) {
+    char *p;
+    
+    /* allocate room for directory name */
+    dirp->dirname = (char*) malloc (strlen (dirname) + 1 + strlen ("\\*.*"));
+    if (dirp->dirname == NULL) {
+      /* failed to duplicate directory name.  errno set by malloc() */
+      free (dirp);
+      return NULL;
+    }
+    /* Copy directory name while appending directory separator and "*.*".
+     * Directory separator is not appended if the name already ends with
+     * drive or directory separator.  Directory separator is assumed to be
+     * '/' or '\' and drive separator is assumed to be ':'. */
+    strcpy (dirp->dirname, dirname);
+    p = strchr (dirp->dirname, '\0');
+    if (dirp->dirname < p  &&
+        *(p - 1) != '\\'  &&  *(p - 1) != '/'  &&  *(p - 1) != ':')
+    {
+      strcpy (p++, "\\");
+    }
+# ifdef DIRENT_WIN32_INTERFACE
+    strcpy (p, "*"); /*scan files with and without extension in win32*/
+# else
+    strcpy (p, "*.*"); /*scan files with and without extension in DOS*/
+# endif
+
+    /* open stream */
+    if (_initdir (dirp) == 0) {
+      /* initialization failed */
+      free (dirp->dirname);
+      free (dirp);
+      return NULL;
+    }
+  }
+  return dirp;
+}
+
+
+/*
+ * <function name="readdir">
+ * <intro>read a directory entry
+ * <syntax>struct dirent *readdir (DIR *dirp);
+ *
+ * <desc>Read individual directory entry and return pointer to a structure
+ * containing the name of the entry.  Individual directory entries returned
+ * include normal files, sub-directories, pseudo-directories "." and ".."
+ * and also volume labels, hidden files and system files in MS-DOS and
+ * MS-Windows.   You might want to use stat(2) function to determinate which
+ * one are you dealing with.  Many dirent implementations already contain
+ * equivalent information in dirent structure but you cannot depend on
+ * this.
+ *
+ * The dirent structure contains several system dependent fields that
+ * generally have no interest to you.  The only interesting one is char
+ * d_name[] that is also portable across different systems.  The d_name
+ * field contains the name of the directory entry without leading path.
+ * While d_name is portable across different systems the actual storage
+ * capacity of d_name varies from system to system and there is no portable
+ * way to find out it at compile time as different systems define the
+ * capacity of d_name with different macros and some systems do not define
+ * capacity at all (besides actual declaration of the field). If you really
+ * need to find out storage capacity of d_name then you might want to try
+ * NAME_MAX macro. The NAME_MAX is defined in POSIX standard althought
+ * there are many MS-DOS and MS-Windows implementations those do not define
+ * it.  There are also systems that declare d_name as "char d_name[1]" and
+ * then allocate suitable amount of memory at run-time.  Thanks to Alain
+ * Decamps (Alain.Decamps@advalvas.be) for pointing it out to me.
+ *
+ * This all leads to the fact that it is difficult to allocate space
+ * for the directory names when the very same program is being compiled on
+ * number of operating systems.  Therefore I suggest that you always
+ * allocate space for directory names dynamically.
+ *
+ * <ret>
+ * Returns a pointer to a structure containing name of the directory entry
+ * in `d_name' field or NULL if there was an error.  In case of an error the
+ * global `errno' variable will set as follows:
+ *
+ * <table>
+ * [EBADF  |dir parameter refers to an invalid directory stream.  This value
+ *          is not set reliably on all implementations.]
+ * </table>
+ * </function>
+ */
+static struct dirent *
+readdir (DIR *dirp)
+{
+  assert(dirp != NULL);
+  if (dirp == NULL) {
+    errno = EBADF;
+    return NULL;
+  }
+
+#if defined(DIRENT_WIN32_INTERFACE)
+  if (dirp->search_handle == INVALID_HANDLE_VALUE) {
+    /* directory stream was opened/rewound incorrectly or it ended normally */
+    errno = EBADF;
+    return NULL;
+  }
+#endif
+
+  if (dirp->dirent_filled != 0) {
+    /*
+     * Directory entry has already been retrieved and there is no need to
+     * retrieve a new one.  Directory entry will be retrieved in advance
+     * when the user calls readdir function for the first time.  This is so
+     * because real dirent has separate functions for opening and reading
+     * the stream whereas Win32 and DOS dirents open the stream
+     * automatically when we retrieve the first file.  Therefore, we have to
+     * save the first file when opening the stream and later we have to
+     * return the saved entry when the user tries to read the first entry.
+     */
+    dirp->dirent_filled = 0;
+  } else {
+    /* fill in entry and return that */
+#if defined(DIRENT_WIN32_INTERFACE)
+    if (FindNextFile (dirp->search_handle, &dirp->current.data) == FALSE) {
+      /* Last file has been processed or an error occured */
+      FindClose (dirp->search_handle);
+      dirp->search_handle = INVALID_HANDLE_VALUE;
+      errno = ENOENT;
+      return NULL;
+    }
+
+# elif defined(DIRENT_MSDOS_INTERFACE)
+    if (_dos_findnext (&dirp->current.data) != 0) {
+      /* _dos_findnext and findnext will set errno to ENOENT when no
+       * more entries could be retrieved. */
+      return NULL;
+    }
+# endif
+
+    _setdirname (dirp);
+    assert (dirp->dirent_filled == 0);
+  }
+  return &dirp->current;
+}
+
+
+/*
+ * <function name="closedir">
+ * <intro>close directory stream.
+ * <syntax>int closedir (DIR *dirp);
+ *
+ * <desc>Close directory stream opened by the `opendir' function.  Close of
+ * directory stream invalidates the DIR structure as well as previously read
+ * dirent entry.
+ *
+ * <ret>The function typically returns 0 on success and -1 on failure but
+ * the function may be declared to return void on same systems.  At least
+ * Borland C/C++ and some UNIX implementations use void as a return type.
+ * The dirent wrapper tries to define VOID_CLOSEDIR whenever closedir is
+ * known to return nothing.  The very same definition is made by the GNU
+ * autoconf if you happen to use it.
+ *
+ * The global `errno' variable will set to EBADF in case of error.
+ * </function>
+ */
+static int
+closedir (DIR *dirp)
+{   
+  int retcode = 0;
+
+  /* make sure that dirp points to legal structure */
+  assert (dirp != NULL);
+  if (dirp == NULL) {
+    errno = EBADF;
+    return -1;
+  }
+  /* free directory name and search handles */
+  if (dirp->dirname != NULL) free (dirp->dirname);
+
+#if defined(DIRENT_WIN32_INTERFACE)
+  if (dirp->search_handle != INVALID_HANDLE_VALUE) {
+    if (FindClose (dirp->search_handle) == FALSE) {
+      /* Unknown error */
+      retcode = -1;
+      errno = EBADF;
+    }
+  }
+#endif                     
+
+  /* clear dirp structure to make sure that it cannot be used anymore*/
+  memset (dirp, 0, sizeof (*dirp));
+# if defined(DIRENT_WIN32_INTERFACE)
+  dirp->search_handle = INVALID_HANDLE_VALUE;
+# endif
+
+  free (dirp);
+  return retcode;
+}
+
+
+/*
+ * <function name="rewinddir">
+ * <intro>rewind directory stream to the beginning
+ * <syntax>void rewinddir (DIR *dirp);
+ *
+ * <desc>Rewind directory stream to the beginning so that the next call of
+ * readdir() returns the very first directory entry again.  However, note
+ * that next call of readdir() may not return the same directory entry as it
+ * did in first time.  The directory stream may have been affected by newly
+ * created files.
+ *
+ * Almost every dirent implementation ensure that rewinddir will update
+ * the directory stream to reflect any changes made to the directory entries
+ * since the previous ``opendir'' or ``rewinddir'' call.  Keep an eye on
+ * this if your program depends on the feature.  I know at least one dirent
+ * implementation where you are required to close and re-open the stream to
+ * see the changes.
+ *
+ * <ret>Returns nothing.  If something went wrong while rewinding, you will
+ * notice it later when you try to retrieve the first directory entry.
+ */
+static void
+rewinddir (DIR *dirp)
+{   
+  /* make sure that dirp is legal */
+  assert (dirp != NULL);
+  if (dirp == NULL) {
+    errno = EBADF;
+    return;
+  }
+  assert (dirp->dirname != NULL);
+  
+  /* close previous stream */
+#if defined(DIRENT_WIN32_INTERFACE)
+  if (dirp->search_handle != INVALID_HANDLE_VALUE) {
+    if (FindClose (dirp->search_handle) == FALSE) {
+      /* Unknown error */
+      errno = EBADF;
+    }
+  }
+#endif
+
+  /* re-open previous stream */
+  if (_initdir (dirp) == 0) {
+    /* initialization failed but we cannot deal with error.  User will notice
+     * error later when she tries to retrieve first directory enty. */
+    /*EMPTY*/;
+  }
+}
+
+
+/*
+ * Open native directory stream object and retrieve first file.
+ * Be sure to close previous stream before opening new one.
+ */
+static int
+_initdir (DIR *dirp)
+{ 
+  assert (dirp != NULL);
+  assert (dirp->dirname != NULL);
+  dirp->dirent_filled = 0;
+
+# if defined(DIRENT_WIN32_INTERFACE)
+  /* Open stream and retrieve first file */
+  dirp->search_handle = FindFirstFile (dirp->dirname, &dirp->current.data);
+  if (dirp->search_handle == INVALID_HANDLE_VALUE) {
+    /* something went wrong but we don't know what.  GetLastError() could
+     * give us more information about the error, but then we should map
+     * the error code into errno. */
+    errno = ENOENT;
+    return 0;
+  }
+
+# elif defined(DIRENT_MSDOS_INTERFACE)
+  if (_dos_findfirst (dirp->dirname,
+          _A_SUBDIR | _A_RDONLY | _A_ARCH | _A_SYSTEM | _A_HIDDEN,
+          &dirp->current.data) != 0)
+  {
+    /* _dos_findfirst and findfirst will set errno to ENOENT when no 
+     * more entries could be retrieved. */
+    return 0;
+  }
+# endif
+
+  /* initialize DIR and it's first entry */
+  _setdirname (dirp);
+  dirp->dirent_filled = 1;
+  return 1;
+}
+
+
+/*
+ * Return implementation dependent name of the current directory entry.
+ */
+static const char *
+_getdirname (const struct dirent *dp)
+{
+#if defined(DIRENT_WIN32_INTERFACE)
+  return dp->data.cFileName;
+  
+#elif defined(DIRENT_USE_FFBLK)
+  return dp->data.ff_name;
+  
+#else
+  return dp->data.name;
+#endif  
+}
+
+
+/*
+ * Copy name of implementation dependent directory entry to the d_name field.
+ */
+static void
+_setdirname (struct DIR *dirp) {
+  /* make sure that d_name is long enough */
+  assert (strlen (_getdirname (&dirp->current)) <= NAME_MAX);
+  
+  strncpy (dirp->current.d_name,
+      _getdirname (&dirp->current),
+      NAME_MAX);
+  dirp->current.d_name[NAME_MAX] = '\0'; /*char d_name[NAME_MAX+1]*/
+}
+  
+# ifdef __cplusplus
+}
+# endif
+# define NAMLEN(dp) ((int)(strlen((dp)->d_name)))
+
+#else
+# error "missing dirent interface"
+#endif
+
+
+#endif /*DIRENT_H*/