Initialize Tizen 2.3 2.3a_release submit/tizen_2.3/20140531.114716
authorSehong Na <sehong.na@samsung.com>
Sat, 31 May 2014 04:20:37 +0000 (13:20 +0900)
committerSehong Na <sehong.na@samsung.com>
Sat, 31 May 2014 04:20:37 +0000 (13:20 +0900)
41 files changed:
.gitignore [new file with mode: 0755]
CMakeLists.txt [new file with mode: 0644]
COPYING [new file with mode: 0755]
debian/changelog [new file with mode: 0644]
debian/compat [new file with mode: 0644]
debian/control [new file with mode: 0644]
debian/copyright [new file with mode: 0644]
debian/dirs [new file with mode: 0644]
debian/docs [new file with mode: 0644]
debian/libxdgmime-dev.install.in [new file with mode: 0644]
debian/libxdgmime.install.in [new file with mode: 0644]
debian/libxdgmime.postinst [new file with mode: 0644]
debian/rules [new file with mode: 0755]
packaging/xdgmime.spec [new file with mode: 0644]
test/CMakeLists.txt [new file with mode: 0644]
test/perf.h [new file with mode: 0755]
test/xdgmime_test.c [new file with mode: 0755]
xdgmime.manifest [new file with mode: 0644]
xdgmime.pc.in [new file with mode: 0644]
xdgmime/ChangeLog [new file with mode: 0644]
xdgmime/Makefile [new file with mode: 0644]
xdgmime/README [new file with mode: 0644]
xdgmime/src/.cvsignore [new file with mode: 0644]
xdgmime/src/Makefile [new file with mode: 0644]
xdgmime/src/xdgmime.c [new file with mode: 0755]
xdgmime/src/xdgmime.h [new file with mode: 0644]
xdgmime/src/xdgmimealias.c [new file with mode: 0644]
xdgmime/src/xdgmimealias.h [new file with mode: 0644]
xdgmime/src/xdgmimecache.c [new file with mode: 0644]
xdgmime/src/xdgmimecache.h [new file with mode: 0644]
xdgmime/src/xdgmimeglob.c [new file with mode: 0644]
xdgmime/src/xdgmimeglob.h [new file with mode: 0644]
xdgmime/src/xdgmimeglobs2.c [new file with mode: 0755]
xdgmime/src/xdgmimeicon.c [new file with mode: 0644]
xdgmime/src/xdgmimeicon.h [new file with mode: 0644]
xdgmime/src/xdgmimeint.c [new file with mode: 0644]
xdgmime/src/xdgmimeint.h [new file with mode: 0644]
xdgmime/src/xdgmimemagic.c [new file with mode: 0644]
xdgmime/src/xdgmimemagic.h [new file with mode: 0644]
xdgmime/src/xdgmimeparent.c [new file with mode: 0644]
xdgmime/src/xdgmimeparent.h [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100755 (executable)
index 0000000..6c53821
--- /dev/null
@@ -0,0 +1,12 @@
+build_log
+*.log
+*.pyc
+usr
+opt
+*.o
+*.os
+*.exe
+packages
+binaries
+*.ipk
+*~
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644 (file)
index 0000000..5090db6
--- /dev/null
@@ -0,0 +1,84 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+SET(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true)
+
+PROJECT(xdgmime C)
+SET(VERSION_MAJOR 1)
+SET(VERSION "${VERSION_MAJOR}.1.0")
+
+### Global setting ###
+
+SET(PREFIX ${CMAKE_INSTALL_PREFIX})
+SET(EXEC_PREFIX "\${prefix}")
+SET(LIBDIR "\${prefix}/lib")
+SET(INCLUDEDIR "\${prefix}/include")
+
+# Build type : Release
+IF("${CMAKE_BUILD_TYPE}" STREQUAL "")
+       SET(CMAKE_BUILD_TYPE "Release")
+ENDIF()
+MESSAGE("Build type: ${CMAKE_BUILD_TYPE}")
+
+
+# Set required packages
+INCLUDE(FindPkgConfig)
+
+#pkg_check_modules(libpkgs REQUIRED )
+#FOREACH(flag ${libpkgs_CFLAGS})
+#      SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
+#ENDFOREACH(flag)
+
+# Compiler flags
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR})
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -Wl,-zdefs" )
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden")
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -Wall -Wmissing-prototypes -Wno-sign-compare -g -DHAVE_MMAP")
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
+SET(CMAKE_C_FLAGS_DEBUG "-O0 -g")
+SET(CMAKE_C_FLAGS_RELEASE "-O2")
+
+SET(CMAKE_SKIP_BUILD_RPATH true)
+
+# Get uname value to set 'TARGET' definition 
+# TODO: Is this needed?
+FIND_PROGRAM(UNAME NAMES uname)
+EXEC_PROGRAM("${UNAME}" ARGS "-m" OUTPUT_VARIABLE "ARCH")
+IF("${ARCH}" STREQUAL "arm")
+       ADD_DEFINITIONS("-DTARGET")
+       MESSAGE("add -DTARGET")
+ENDIF("${ARCH}" STREQUAL "arm")
+
+ADD_DEFINITIONS("-DPREFIX=\"${CMAKE_INSTALL_PREFIX}\"")
+
+# Linker flags
+SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed")
+
+### Build ###
+
+# xdgmime
+add_library(xdgmime SHARED
+       xdgmime/src/xdgmime.c
+       xdgmime/src/xdgmimeglob.c
+       xdgmime/src/xdgmimeint.c
+       xdgmime/src/xdgmimemagic.c
+       xdgmime/src/xdgmimealias.c
+       xdgmime/src/xdgmimeparent.c
+       xdgmime/src/xdgmimecache.c
+       xdgmime/src/xdgmimeicon.c
+       xdgmime/src/xdgmimeglobs2.c
+       )
+target_link_libraries(xdgmime ${libpkgs_LDFLAGS})
+SET_TARGET_PROPERTIES(xdgmime PROPERTIES SOVERSION ${VERSION_MAJOR})
+SET_TARGET_PROPERTIES(xdgmime PROPERTIES VERSION ${VERSION})
+
+# pkgconfig file
+CONFIGURE_FILE(xdgmime.pc.in xdgmime.pc @ONLY)
+
+### Install ###
+INSTALL(TARGETS xdgmime DESTINATION lib COMPONENT RuntimeLibraries)
+
+INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/xdgmime/src/xdgmime.h DESTINATION include)
+INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/xdgmime.pc DESTINATION lib/pkgconfig)
+
+# test
+add_subdirectory(test)
+
diff --git a/COPYING b/COPYING
new file mode 100755 (executable)
index 0000000..68b6dfb
--- /dev/null
+++ b/COPYING
@@ -0,0 +1,552 @@
+                 GNU LESSER GENERAL PUBLIC LICENSE
+                      Version 2.1, February 1999
+
+ Copyright (C) 1991, 1999 Free Software Foundation, Inc.
+ 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+[This is the first released version of the Lesser GPL.  It also counts
+ as the successor of the GNU Library Public License, version 2, hence
+ the version number 2.1.]
+
+                           Preamble
+
+  The licenses for most software are designed to take away your
+freedom to share and change it.  By contrast, the GNU General Public
+Licenses are intended to guarantee your freedom to share and change
+free software--to make sure the software is free for all its users.
+
+  This license, the Lesser General Public License, applies to some
+specially designated software packages--typically libraries--of the
+Free Software Foundation and other authors who decide to use it.  You
+can use it too, but we suggest you first think carefully about whether
+this license or the ordinary General Public License is the better
+strategy to use in any particular case, based on the explanations below.
+
+  When we speak of free software, we are referring to freedom of use,
+not price.  Our General Public Licenses are designed to make sure that
+you have the freedom to distribute copies of free software (and charge
+for this service if you wish); that you receive source code or can get
+it if you want it; that you can change the software and use pieces of
+it in new free programs; and that you are informed that you can do
+these things.
+
+  To protect your rights, we need to make restrictions that forbid
+distributors to deny you these rights or to ask you to surrender these
+rights.  These restrictions translate to certain responsibilities for
+you if you distribute copies of the library or if you modify it.
+
+  For example, if you distribute copies of the library, whether gratis
+or for a fee, you must give the recipients all the rights that we gave
+you.  You must make sure that they, too, receive or can get the source
+code.  If you link other code with the library, you must provide
+complete object files to the recipients, so that they can relink them
+with the library after making changes to the library and recompiling
+it.  And you must show them these terms so they know their rights.
+
+  We protect your rights with a two-step method: (1) we copyright the
+library, and (2) we offer you this license, which gives you legal
+permission to copy, distribute and/or modify the library.
+
+  To protect each distributor, we want to make it very clear that
+there is no warranty for the free library.  Also, if the library is
+modified by someone else and passed on, the recipients should know
+that what they have is not the original version, so that the original
+author's reputation will not be affected by problems that might be
+introduced by others.
+
+  Finally, software patents pose a constant threat to the existence of
+any free program.  We wish to make sure that a company cannot
+effectively restrict the users of a free program by obtaining a
+restrictive license from a patent holder.  Therefore, we insist that
+any patent license obtained for a version of the library must be
+consistent with the full freedom of use specified in this license.
+
+  Most GNU software, including some libraries, is covered by the
+ordinary GNU General Public License.  This license, the GNU Lesser
+General Public License, applies to certain designated libraries, and
+is quite different from the ordinary General Public License.  We use
+this license for certain libraries in order to permit linking those
+libraries into non-free programs.
+
+  When a program is linked with a library, whether statically or using
+a shared library, the combination of the two is legally speaking a
+combined work, a derivative of the original library.  The ordinary
+General Public License therefore permits such linking only if the
+entire combination fits its criteria of freedom.  The Lesser General
+Public License permits more lax criteria for linking other code with
+the library.
+
+  We call this license the "Lesser" General Public License because it
+does Less to protect the user's freedom than the ordinary General
+Public License.  It also provides other free software developers Less
+of an advantage over competing non-free programs.  These disadvantages
+are the reason we use the ordinary General Public License for many
+libraries.  However, the Lesser license provides advantages in certain
+special circumstances.
+
+  For example, on rare occasions, there may be a special need to
+encourage the widest possible use of a certain library, so that it becomes
+a de-facto standard.  To achieve this, non-free programs must be
+allowed to use the library.  A more frequent case is that a free
+library does the same job as widely used non-free libraries.  In this
+case, there is little to gain by limiting the free library to free
+software only, so we use the Lesser General Public License.
+
+  In other cases, permission to use a particular library in non-free
+programs enables a greater number of people to use a large body of
+free software.  For example, permission to use the GNU C Library in
+non-free programs enables many more people to use the whole GNU
+operating system, as well as its variant, the GNU/Linux operating
+system.
+
+  Although the Lesser General Public License is Less protective of the
+users' freedom, it does ensure that the user of a program that is
+linked with the Library has the freedom and the wherewithal to run
+that program using a modified version of the Library.
+
+  The precise terms and conditions for copying, distribution and
+modification follow.  Pay close attention to the difference between a
+"work based on the library" and a "work that uses the library".  The
+former contains code derived from the library, whereas the latter must
+be combined with the library in order to run.
+
+                 GNU LESSER GENERAL PUBLIC LICENSE
+   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+  0. This License Agreement applies to any software library or other
+program which contains a notice placed by the copyright holder or
+other authorized party saying it may be distributed under the terms of
+this Lesser General Public License (also called "this License").
+Each licensee is addressed as "you".
+
+  A "library" means a collection of software functions and/or data
+prepared so as to be conveniently linked with application programs
+(which use some of those functions and data) to form executables.
+
+  The "Library", below, refers to any such software library or work
+which has been distributed under these terms.  A "work based on the
+Library" means either the Library or any derivative work under
+copyright law: that is to say, a work containing the Library or a
+portion of it, either verbatim or with modifications and/or translated
+straightforwardly into another language.  (Hereinafter, translation is
+included without limitation in the term "modification".)
+
+  "Source code" for a work means the preferred form of the work for
+making modifications to it.  For a library, complete source code means
+all the source code for all modules it contains, plus any associated
+interface definition files, plus the scripts used to control compilation
+and installation of the library.
+
+  Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope.  The act of
+running a program using the Library is not restricted, and output from
+such a program is covered only if its contents constitute a work based
+on the Library (independent of the use of the Library in a tool for
+writing it).  Whether that is true depends on what the Library does
+and what the program that uses the Library does.
+
+  1. You may copy and distribute verbatim copies of the Library's
+complete source code as you receive it, in any medium, provided that
+you conspicuously and appropriately publish on each copy an
+appropriate copyright notice and disclaimer of warranty; keep intact
+all the notices that refer to this License and to the absence of any
+warranty; and distribute a copy of this License along with the
+Library.
+
+  You may charge a fee for the physical act of transferring a copy,
+and you may at your option offer warranty protection in exchange for a
+fee.
+
+  2. You may modify your copy or copies of the Library or any portion
+of it, thus forming a work based on the Library, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+    a) The modified work must itself be a software library.
+
+    b) You must cause the files modified to carry prominent notices
+    stating that you changed the files and the date of any change.
+
+    c) You must cause the whole of the work to be licensed at no
+    charge to all third parties under the terms of this License.
+
+    d) If a facility in the modified Library refers to a function or a
+    table of data to be supplied by an application program that uses
+    the facility, other than as an argument passed when the facility
+    is invoked, then you must make a good faith effort to ensure that,
+    in the event an application does not supply such function or
+    table, the facility still operates, and performs whatever part of
+    its purpose remains meaningful.
+
+    (For example, a function in a library to compute square roots has
+    a purpose that is entirely well-defined independent of the
+    application.  Therefore, Subsection 2d requires that any
+    application-supplied function or table used by this function must
+    be optional: if the application does not supply it, the square
+    root function must still compute square roots.)
+
+These requirements apply to the modified work as a whole.  If
+identifiable sections of that work are not derived from the Library,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works.  But when you
+distribute the same sections as part of a whole which is a work based
+on the Library, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote
+it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Library.
+
+In addition, mere aggregation of another work not based on the Library
+with the Library (or with a work based on the Library) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+  3. You may opt to apply the terms of the ordinary GNU General Public
+License instead of this License to a given copy of the Library.  To do
+this, you must alter all the notices that refer to this License, so
+that they refer to the ordinary GNU General Public License, version 2,
+instead of to this License.  (If a newer version than version 2 of the
+ordinary GNU General Public License has appeared, then you can specify
+that version instead if you wish.)  Do not make any other change in
+these notices.
+
+  Once this change is made in a given copy, it is irreversible for
+that copy, so the ordinary GNU General Public License applies to all
+subsequent copies and derivative works made from that copy.
+
+  This option is useful when you wish to copy part of the code of
+the Library into a program that is not a library.
+
+  4. You may copy and distribute the Library (or a portion or
+derivative of it, under Section 2) in object code or executable form
+under the terms of Sections 1 and 2 above provided that you accompany
+it with the complete corresponding machine-readable source code, which
+must be distributed under the terms of Sections 1 and 2 above on a
+medium customarily used for software interchange.
+
+  If distribution of object code is made by offering access to copy
+from a designated place, then offering equivalent access to copy the
+source code from the same place satisfies the requirement to
+distribute the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+  5. A program that contains no derivative of any portion of the
+Library, but is designed to work with the Library by being compiled or
+linked with it, is called a "work that uses the Library".  Such a
+work, in isolation, is not a derivative work of the Library, and
+therefore falls outside the scope of this License.
+
+  However, linking a "work that uses the Library" with the Library
+creates an executable that is a derivative of the Library (because it
+contains portions of the Library), rather than a "work that uses the
+library".  The executable is therefore covered by this License.
+Section 6 states terms for distribution of such executables.
+
+  When a "work that uses the Library" uses material from a header file
+that is part of the Library, the object code for the work may be a
+derivative work of the Library even though the source code is not.
+Whether this is true is especially significant if the work can be
+linked without the Library, or if the work is itself a library.  The
+threshold for this to be true is not precisely defined by law.
+
+  If such an object file uses only numerical parameters, data
+structure layouts and accessors, and small macros and small inline
+functions (ten lines or less in length), then the use of the object
+file is unrestricted, regardless of whether it is legally a derivative
+work.  (Executables containing this object code plus portions of the
+Library will still fall under Section 6.)
+
+  Otherwise, if the work is a derivative of the Library, you may
+distribute the object code for the work under the terms of Section 6.
+Any executables containing that work also fall under Section 6,
+whether or not they are linked directly with the Library itself.
+
+  6. As an exception to the Sections above, you may also combine or
+link a "work that uses the Library" with the Library to produce a
+work containing portions of the Library, and distribute that work
+under terms of your choice, provided that the terms permit
+modification of the work for the customer's own use and reverse
+engineering for debugging such modifications.
+
+  You must give prominent notice with each copy of the work that the
+Library is used in it and that the Library and its use are covered by
+this License.  You must supply a copy of this License.  If the work
+during execution displays copyright notices, you must include the
+copyright notice for the Library among them, as well as a reference
+directing the user to the copy of this License.  Also, you must do one
+of these things:
+
+    a) Accompany the work with the complete corresponding
+    machine-readable source code for the Library including whatever
+    changes were used in the work (which must be distributed under
+    Sections 1 and 2 above); and, if the work is an executable linked
+    with the Library, with the complete machine-readable "work that
+    uses the Library", as object code and/or source code, so that the
+    user can modify the Library and then relink to produce a modified
+    executable containing the modified Library.  (It is understood
+    that the user who changes the contents of definitions files in the
+    Library will not necessarily be able to recompile the application
+    to use the modified definitions.)
+
+    b) Use a suitable shared library mechanism for linking with the
+    Library.  A suitable mechanism is one that (1) uses at run time a
+    copy of the library already present on the user's computer system,
+    rather than copying library functions into the executable, and (2)
+    will operate properly with a modified version of the library, if
+    the user installs one, as long as the modified version is
+    interface-compatible with the version that the work was made with.
+
+    c) Accompany the work with a written offer, valid for at
+    least three years, to give the same user the materials
+    specified in Subsection 6a, above, for a charge no more
+    than the cost of performing this distribution.
+
+    d) If distribution of the work is made by offering access to copy
+    from a designated place, offer equivalent access to copy the above
+    specified materials from the same place.
+
+    e) Verify that the user has already received a copy of these
+    materials or that you have already sent this user a copy.
+
+  For an executable, the required form of the "work that uses the
+Library" must include any data and utility programs needed for
+reproducing the executable from it.  However, as a special exception,
+the materials to be distributed need not include anything that is
+normally distributed (in either source or binary form) with the major
+components (compiler, kernel, and so on) of the operating system on
+which the executable runs, unless that component itself accompanies
+the executable.
+
+  It may happen that this requirement contradicts the license
+restrictions of other proprietary libraries that do not normally
+accompany the operating system.  Such a contradiction means you cannot
+use both them and the Library together in an executable that you
+distribute.
+
+  7. You may place library facilities that are a work based on the
+Library side-by-side in a single library together with other library
+facilities not covered by this License, and distribute such a combined
+library, provided that the separate distribution of the work based on
+the Library and of the other library facilities is otherwise
+permitted, and provided that you do these two things:
+
+    a) Accompany the combined library with a copy of the same work
+    based on the Library, uncombined with any other library
+    facilities.  This must be distributed under the terms of the
+    Sections above.
+
+    b) Give prominent notice with the combined library of the fact
+    that part of it is a work based on the Library, and explaining
+    where to find the accompanying uncombined form of the same work.
+
+  8. You may not copy, modify, sublicense, link with, or distribute
+the Library except as expressly provided under this License.  Any
+attempt otherwise to copy, modify, sublicense, link with, or
+distribute the Library is void, and will automatically terminate your
+rights under this License.  However, parties who have received copies,
+or rights, from you under this License will not have their licenses
+terminated so long as such parties remain in full compliance.
+
+  9. You are not required to accept this License, since you have not
+signed it.  However, nothing else grants you permission to modify or
+distribute the Library or its derivative works.  These actions are
+prohibited by law if you do not accept this License.  Therefore, by
+modifying or distributing the Library (or any work based on the
+Library), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Library or works based on it.
+
+  10. Each time you redistribute the Library (or any work based on the
+Library), the recipient automatically receives a license from the
+original licensor to copy, distribute, link with or modify the Library
+subject to these terms and conditions.  You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties with
+this License.
+
+  11. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License.  If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Library at all.  For example, if a patent
+license would not permit royalty-free redistribution of the Library by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Library.
+
+If any portion of this section is held invalid or unenforceable under any
+particular circumstance, the balance of the section is intended to apply,
+and the section as a whole is intended to apply in other circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system which is
+implemented by public license practices.  Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+
+  12. If the distribution and/or use of the Library is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Library under this License may add
+an explicit geographical distribution limitation excluding those countries,
+so that distribution is permitted only in or among countries not thus
+excluded.  In such case, this License incorporates the limitation as if
+written in the body of this License.
+
+  13. The Free Software Foundation may publish revised and/or new
+versions of the Lesser General Public License from time to time.
+Such new versions will be similar in spirit to the present version,
+but may differ in detail to address new problems or concerns.
+
+Each version is given a distinguishing version number.  If the Library
+specifies a version number of this License which applies to it and
+"any later version", you have the option of following the terms and
+conditions either of that version or of any later version published by
+the Free Software Foundation.  If the Library does not specify a
+license version number, you may choose any version ever published by
+the Free Software Foundation.
+
+  14. If you wish to incorporate parts of the Library into other free
+programs whose distribution conditions are incompatible with these,
+write to the author to ask for permission.  For software which is
+copyrighted by the Free Software Foundation, write to the Free
+Software Foundation; we sometimes make exceptions for this.  Our
+decision will be guided by the two goals of preserving the free status
+of all derivatives of our free software and of promoting the sharing
+and reuse of software generally.
+
+                           NO WARRANTY
+
+  15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
+WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
+EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
+OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
+KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
+LIBRARY IS WITH YOU.  SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
+THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
+
+  16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
+WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
+AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
+FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
+CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
+LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
+RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
+FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
+SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
+DAMAGES.
+
+                    END OF TERMS AND CONDITIONS
+
+           How to Apply These Terms to Your New Libraries
+
+  If you develop a new library, and you want it to be of the greatest
+possible use to the public, we recommend making it free software that
+everyone can redistribute and change.  You can do so by permitting
+redistribution under these terms (or, alternatively, under the terms of the
+ordinary General Public License).
+
+  To apply these terms, attach the following notices to the library.  It is
+safest to attach them to the start of each source file to most effectively
+convey the exclusion of warranty; and each file should have at least the
+"copyright" line and a pointer to where the full notice is found.
+
+    <one line to give the library's name and a brief idea of what it does.>
+    Copyright (C) <year>  <name of author>
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+
+Also add information on how to contact you by electronic and paper mail.
+
+You should also get your employer (if you work as a programmer) or your
+school, if any, to sign a "copyright disclaimer" for the library, if
+necessary.  Here is a sample; alter the names:
+
+  Yoyodyne, Inc., hereby disclaims all copyright interest in the
+  library `Frob' (a library for tweaking knobs) written by James Random Hacker.
+
+  <signature of Ty Coon>, 1 April 1990
+  Ty Coon, President of Vice
+
+That's all there is to it!
+
+
+
+
+Academic Free License
+v. 2.0
+This Academic Free License (the "License") applies to any original work of authorship (the "Original Work") whose owner (the "Licensor") has placed the following notice immediately following the copyright notice for the Original Work:
+
+Licensed under the Academic Free License version 2.0
+
+1) Grant of Copyright License. Licensor hereby grants You a world-wide, royalty-free, non-exclusive, perpetual, sublicenseable license to do the following:
+
+a) to reproduce the Original Work in copies;
+b) to prepare derivative works ("Derivative Works") based upon the Original Work;
+
+c) to distribute copies of the Original Work and Derivative Works to the public;
+
+d) to perform the Original Work publicly; and
+
+e) to display the Original Work publicly.
+
+2) Grant of Patent License. Licensor hereby grants You a world-wide, royalty-free, non-exclusive, perpetual, sublicenseable license, under patent claims owned or controlled by the Licensor that are embodied in the Original Work as furnished by the Licensor, to make, use, sell and offer for sale the Original Work and Derivative Works.
+
+3) Grant of Source Code License. The term "Source Code" means the preferred form of the Original Work for making modifications to it and all available documentation describing how to modify the Original Work. Licensor hereby agrees to provide a machine-readable copy of the Source Code of the Original Work along with each copy of the Original Work that Licensor distributes. Licensor reserves the right to satisfy this obligation by placing a machine-readable copy of the Source Code in an information repository reasonably calculated to permit inexpensive and convenient access by You for as long as Licensor continues to distribute the Original Work, and by publishing the address of that information repository in a notice immediately following the copyright notice that applies to the Original Work.
+
+4) Exclusions From License Grant. Neither the names of Licensor, nor the names of any contributors to the Original Work, nor any of their trademarks or service marks, may be used to endorse or promote products derived from this Original Work without express prior written permission of the Licensor. Nothing in this License shall be deemed to grant any rights to trademarks, copyrights, patents, trade secrets or any other intellectual property of Licensor except as expressly stated herein. No patent license is granted to make, use, sell or offer to sell embodiments of any patent claims other than the licensed claims defined in Section 2. No right is granted to the trademarks of Licensor even if such marks are included in the Original Work. Nothing in this License shall be interpreted to prohibit Licensor from licensing under different terms from this License any Original Work that Licensor otherwise would have a right to license.
+
+5) This section intentionally omitted.
+
+6) Attribution Rights. You must retain, in the Source Code of any Derivative Works that You create, all copyright, patent or trademark notices from the Source Code of the Original Work, as well as any notices of licensing and any descriptive text identified therein as an "Attribution Notice." You must cause the Source Code for any Derivative Works that You create to carry a prominent Attribution Notice reasonably calculated to inform recipients that You have modified the Original Work.
+
+7) Warranty of Provenance and Disclaimer of Warranty. Licensor warrants that the copyright in and to the Original Work and the patent rights granted herein by Licensor are owned by the Licensor or are sublicensed to You under the terms of this License with the permission of the contributor(s) of those copyrights and patent rights. Except as expressly stated in the immediately proceeding sentence, the Original Work is provided under this License on an "AS IS" BASIS and WITHOUT WARRANTY, either express or implied, including, without limitation, the warranties of NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY OF THE ORIGINAL WORK IS WITH YOU. This DISCLAIMER OF WARRANTY constitutes an essential part of this License. No license to Original Work is granted hereunder except under this disclaimer.
+
+8) Limitation of Liability. Under no circumstances and under no legal theory, whether in tort (including negligence), contract, or otherwise, shall the Licensor be liable to any person for any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or the use of the Original Work including, without limitation, damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses. This limitation of liability shall not apply to liability for death or personal injury resulting from Licensor's negligence to the extent applicable law prohibits such limitation. Some jurisdictions do not allow the exclusion or limitation of incidental or consequential damages, so this exclusion and limitation may not apply to You.
+
+9) Acceptance and Termination. If You distribute copies of the Original Work or a Derivative Work, You must make a reasonable effort under the circumstances to obtain the express assent of recipients to the terms of this License. Nothing else but this License (or another written agreement between Licensor and You) grants You permission to create Derivative Works based upon the Original Work or to exercise any of the rights granted in Section 1 herein, and any attempt to do so except under the terms of this License (or another written agreement between Licensor and You) is expressly prohibited by U.S. copyright law, the equivalent laws of other countries, and by international treaty. Therefore, by exercising any of the rights granted to You in Section 1 herein, You indicate Your acceptance of this License and all of its terms and conditions.
+
+10) Termination for Patent Action. This License shall terminate automatically and You may no longer exercise any of the rights granted to You by this License as of the date You commence an action, including a cross-claim or counterclaim, for patent infringement (i) against Licensor with respect to a patent applicable to software or (ii) against any entity with respect to a patent applicable to the Original Work (but excluding combinations of the Original Work with other software or hardware).
+
+11) Jurisdiction, Venue and Governing Law. Any action or suit relating to this License may be brought only in the courts of a jurisdiction wherein the Licensor resides or in which Licensor conducts its primary business, and under the laws of that jurisdiction excluding its conflict-of-law provisions. The application of the United Nations Convention on Contracts for the International Sale of Goods is expressly excluded. Any use of the Original Work outside the scope of this License or after its termination shall be subject to the requirements and penalties of the U.S. Copyright Act, 17 U.S.C. 101 et seq., the equivalent laws of other countries, and international treaty. This section shall survive the termination of this License.
+
+12) Attorneys Fees. In any action to enforce the terms of this License or seeking damages relating thereto, the prevailing party shall be entitled to recover its costs and expenses, including, without limitation, reasonable attorneys' fees and costs incurred in connection with such action, including any appeal of such action. This section shall survive the termination of this License.
+
+13) Miscellaneous. This License represents the complete agreement concerning the subject matter hereof. If any provision of this License is held to be unenforceable, such provision shall be reformed only to the extent necessary to make it enforceable.
+
+14) Definition of "You" in This License. "You" throughout this License, whether in upper or lower case, means an individual or a legal entity exercising rights under, and complying with all of the terms of, this License. For legal entities, "You" includes any entity that controls, is controlled by, or is under common control with you. For purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
+
+15) Right to Use. You may use the Original Work in all ways not otherwise restricted or conditioned by this License or by law, and Licensor promises not to interfere with or be responsible for such uses by You.
+
+This license is Copyright (C) 2003 Lawrence E. Rosen. All rights reserved. Permission is hereby granted to copy and distribute this license without modification. This license may not be modified without the express written permission of its copyright owner.
diff --git a/debian/changelog b/debian/changelog
new file mode 100644 (file)
index 0000000..6a2eae8
--- /dev/null
@@ -0,0 +1,128 @@
+xdgmime (0.0.12-2) unstable; urgency=low
+
+  * add requires info to spec file
+  * Git: slp/pkgs/x/xdgmime
+  * Tag: xdgmime_0.0.12-2
+
+ -- Hyungdeuk Kim <hd3.kim@samsung.com>  Tue, 27 Mar 2012 17:32:14 +0900
+
+xdgmime (0.0.12-1) unstable; urgency=low
+
+  * upload spec file
+  * Git: slp/pkgs/x/xdgmime
+  * Tag: xdgmime_0.0.12-1
+
+ -- Hyungdeuk Kim <hd3.kim@samsung.com>  Fri, 16 Mar 2012 02:32:14 +0900
+
+xdgmime (0.0.12-0) unstable; urgency=low
+
+  * Remove test-mime-data.c, text-mime.c due to license issue
+  * Add boilerplate to perf.h, xdgmime_test.c
+  * Git: 165.213.180.234:slp/pkgs/x/xdgmime
+  * Tag: xdgmime_0.0.12-0
+
+ -- Seokkyu Jang <seokkyu.jang@samsung.com>  Mon, 22 Aug 2011 11:32:14 +0900
+
+xdgmime (0.0.11-0) unstable; urgency=low
+
+  * Add boilerplate & remove xdgmime_api.h
+  * Git: 165.213.180.234:slp/pkgs/x/xdgmime
+  * Tag: xdgmime_0.0.11-0
+
+ -- Seokkyu Jang <seokkyu.jang@samsung.com>  Tue, 09 Aug 2011 15:52:05 +0900
+
+xdgmime (0.0.10-1) unstable; urgency=low
+
+  * Re-versioning
+  * Git: 165.213.180.234:/slp/pkgs/x/xdgmime
+  * Tag: xdgmime_0.0.10-1
+
+ -- Youmin Ha <youmin.ha@samsung.com>  Tue, 14 Dec 2010 15:43:33 +0900
+
+xdgmime (0.0.10) unstable; urgency=low
+
+  * Fix memory initialization bug
+  * Git: 165.213.180.234:/slp/pkgs/x/xdgmime
+  * Tag: xdgmime_0.0.10
+
+ -- Youmin Ha <youmin.ha@samsung.com>  Tue, 14 Dec 2010 15:29:47 +0900
+
+xdgmime (0.0.9-1) unstable; urgency=low
+
+  * reupload
+  * Git: 165.213.180.234:/slp/pkgs/x/xdgmime
+  * Tag: xdgmime_0.0.9-1
+
+ -- Youmin Ha <youmin.ha@samsung.com>  Sat, 11 Dec 2010 16:27:44 +0900
+
+xdgmime (0.0.9) unstable; urgency=low
+
+  * NULL check before strcmp() functions in xdgmimeglobs2.c
+  * Git: 165.213.180.234:/git/slp/pkgs/xdgmime
+  * Tag: xdgmime_0.0.9
+
+ -- Youmin Ha <youmin.ha@samsung.com>  Sat, 11 Dec 2010 15:56:05 +0900
+
+xdgmime (0.0.8) unstable; urgency=low
+
+  * set new function to API
+  * Git: 165.213.180.234:/git/slp/pkgs/xdgmime
+  * Tag: xdgmime_0.0.8
+
+ -- Youmin Ha <youmin.ha@samsung.com>  Fri, 29 Oct 2010 14:22:20 +0900
+
+xdgmime (0.0.7) unstable; urgency=low
+
+  * Add API: xdg_mime_get_file_names_from_mime_type() 
+  * Git: 165.213.180.234:/git/slp/pkgs/xdgmime
+  * Tag: xdgmime_0.0.7
+
+ -- Youmin Ha <youmin.ha@samsung.com>  Thu, 28 Oct 2010 11:25:23 +0900
+
+xdgmime (0.0.6) unstable; urgency=low
+
+  * API - xdg_mime_unalias_mime_type 
+  * Git: 165.213.180.234:/git/slp/pkgs/xdgmime
+  * Tag: xdgmime_0.0.6
+
+ -- Kunhoon Baik <knhoon.baik@samsung.com>  Mon, 25 Oct 2010 11:54:06 +0900
+
+xdgmime (0.0.5) unstable; urgency=low
+
+  * build error fix 
+  * Git: 165.213.180.234:/git/slp/pkgs/xdgmime
+  * Tag: xdgmime_0.0.5
+
+ -- Kunhoon Baik <knhoon.baik@samsung.com>  Wed, 20 Oct 2010 15:52:53 +0900
+
+xdgmime (0.0.4) unstable; urgency=low
+
+  * Shared Library versioning 
+  * Git: 165.213.180.234:/git/slp/pkgs/xdgmime
+  * Tag: xdgmime_0.0.4
+
+ -- Kunhoon Baik <knhoon.baik@samsung.com>  Wed, 20 Oct 2010 14:47:28 +0900
+
+xdgmime (0.0.3) unstable; urgency=low
+
+  * release icon API  
+  * Git: 165.213.180.234:/git/slp/pkgs/xdgmime
+  * Tag: xdgmime_0.0.3
+
+ -- Kunhoon Baik <knhoon.baik@samsung.com>  Thu, 09 Sep 2010 10:34:11 +0900
+
+xdgmime (0.0.2) unstable; urgency=low
+
+  * change API prefix 
+  * Git: 165.213.180.234:/git/slp/pkgs/xdgmime
+  * Tag: xdgmime_0.0.2
+
+ -- Kunhoon Baik <knhoon.baik@samsung.com>  Mon, 09 Aug 2010 11:35:21 +0900
+
+xdgmime (0.0.1) unstable; urgency=low
+
+  * Initial release. 
+  * Git: 165.213.180.234:/git/slp/pkgs/xdgmime
+  * Tag: xdgmime_0.0.1
+
+ -- Kunhoon Baik <knhoon.baik@samsung.com>  Fri, 06 Aug 2010 19:32:55 +0900
diff --git a/debian/compat b/debian/compat
new file mode 100644 (file)
index 0000000..7ed6ff8
--- /dev/null
@@ -0,0 +1 @@
+5
diff --git a/debian/control b/debian/control
new file mode 100644 (file)
index 0000000..72068e6
--- /dev/null
@@ -0,0 +1,24 @@
+Source: xdgmime
+Section: devel
+Priority: extra
+Maintainer: Jayoun Lee <airjany@samsung.com>, Seokkyu Jang <seokkyu.jang@samsung.com>, Sangil Yoon <si83.yoon@samsung.com>
+Build-Depends: debhelper (>= 5)
+Standards-Version: 0.1.0 
+
+Package: libxdgmime
+Section: libs
+Architecture: any
+Depends: ${shlibs:Depends}, ${misc:Depends}
+Description: libxdgmime package
+
+Package: libxdgmime-dev
+Section: libs
+Architecture: any
+Depends: libxdgmime (= ${Source-Version})
+Description: libxdgmime dev package
+
+Package: libxdgmime-dbg
+Section: debug
+Architecture: any
+Depends: ${shlibs:Depends}, ${misc:Depends}, libxdgmime (= ${Source-Version})
+Description: libxdgmime dbg package 
diff --git a/debian/copyright b/debian/copyright
new file mode 100644 (file)
index 0000000..ae7f1fc
--- /dev/null
@@ -0,0 +1 @@
+GNU LGPL
diff --git a/debian/dirs b/debian/dirs
new file mode 100644 (file)
index 0000000..ca882bb
--- /dev/null
@@ -0,0 +1,2 @@
+usr/bin
+usr/sbin
diff --git a/debian/docs b/debian/docs
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/debian/libxdgmime-dev.install.in b/debian/libxdgmime-dev.install.in
new file mode 100644 (file)
index 0000000..0f2a4da
--- /dev/null
@@ -0,0 +1,2 @@
+@PREFIX@/include/*
+@PREFIX@/lib/pkgconfig/*.pc
diff --git a/debian/libxdgmime.install.in b/debian/libxdgmime.install.in
new file mode 100644 (file)
index 0000000..5633fdf
--- /dev/null
@@ -0,0 +1 @@
+@PREFIX@/lib/libxdgmime.*\r
diff --git a/debian/libxdgmime.postinst b/debian/libxdgmime.postinst
new file mode 100644 (file)
index 0000000..413b031
--- /dev/null
@@ -0,0 +1,7 @@
+#!/bin/sh
+if [ ${USER} == "root" ]
+then
+        chown root:root /usr/lib/libxdgmime.so.1.1.0
+fi
+
+chmod 644 /usr/lib/libxdgmime.so.1.1.0
diff --git a/debian/rules b/debian/rules
new file mode 100755 (executable)
index 0000000..166a693
--- /dev/null
@@ -0,0 +1,118 @@
+#!/usr/bin/make -f
+# -*- makefile -*-
+# Sample debian/rules that uses debhelper.
+# This file was originally written by Joey Hess and Craig Small.
+# As a special exception, when this file is copied by dh-make into a
+# dh-make output file, you may use that output file without restriction.
+# This special exception was added by Craig Small in version 0.37 of dh-make.
+
+# Uncomment this to turn on verbose mode.
+export DH_VERBOSE=1
+
+CFLAGS ?= -Wall -g
+LDFLAGS ?= 
+PREFIX ?= /usr
+DATADIR ?= /opt
+
+ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
+       CFLAGS += -O0
+else
+       CFLAGS += -O2
+endif
+
+CFLAGS += -fvisibility=hidden
+LDFLAGS += -Wl,--rpath=$(PREFIX)/lib -Wl,--as-needed
+
+CMAKE_TMP_DIR = $(CURDIR)/cmake_tmp
+
+configure: configure-stamp
+configure-stamp:
+       dh_testdir
+       # Add here commands to configure the package.
+       mkdir -p $(CMAKE_TMP_DIR);
+       cd $(CMAKE_TMP_DIR); CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" cmake .. -DCMAKE_INSTALL_PREFIX=$(PREFIX)
+
+       touch configure-stamp
+
+build: build-stamp
+
+build-stamp: configure-stamp 
+       dh_testdir
+
+       # Add here commands to compile the package.
+       cd $(CMAKE_TMP_DIR) && $(MAKE) all test
+
+       for f in `find $(CURDIR)/debian/ -name "*.in"`; do \
+               cat $$f > $${f%.in}; \
+               sed -i -e "s#@PREFIX@#$(PREFIX)#g" $${f%.in}; \
+               sed -i -e "s#@DATADIR@#$(DATADIR)#g" $${f%.in}; \
+       done
+
+       touch $@
+
+clean:
+       dh_testdir
+       dh_testroot
+       rm -f build-stamp configure-stamp
+
+       # Add here commands to clean up after the build process.
+       rm -rf $(CMAKE_TMP_DIR)
+
+       for f in `find $(CURDIR)/debian/ -name "*.in"`; do \
+               rm -f $${f%.in}; \
+       done
+
+       rm -rf GRTAGS
+       rm -rf GTAGS
+       rm -rf GPATH
+       rm -rf GSYMS
+       rm -rf doc
+
+       dh_clean 
+
+install: build
+       dh_testdir
+       dh_testroot
+       dh_clean -k 
+       dh_installdirs
+
+       # Add here commands to install the package into debian/wavplayer.
+       cd $(CMAKE_TMP_DIR) && $(MAKE) DESTDIR=$(CURDIR)/debian/tmp install
+
+# Build architecture-independent files here.
+binary-indep: build install
+# We have nothing to do by default.
+
+# Build architecture-dependent files here.
+binary-arch: build install
+       dh_testdir
+       dh_testroot
+       dh_installchangelogs 
+       dh_installdocs
+       dh_installexamples
+       dh_install --sourcedir=debian/tmp
+#      dh_installmenu
+#      dh_installdebconf       
+#      dh_installlogrotate
+#      dh_installemacsen
+#      dh_installpam
+#      dh_installmime
+#      dh_python
+#      dh_installinit
+#      dh_installcron
+#      dh_installinfo
+       dh_installman
+       dh_link
+       dh_strip --dbg-package=libxdgmime-dbg
+       dh_compress
+       dh_fixperms
+#      dh_perl
+       dh_makeshlibs
+       dh_installdeb
+       dh_shlibdeps
+       dh_gencontrol
+       dh_md5sums
+       dh_builddeb
+
+binary: binary-indep binary-arch
+.PHONY: build clean binary-indep binary-arch binary install configure
diff --git a/packaging/xdgmime.spec b/packaging/xdgmime.spec
new file mode 100644 (file)
index 0000000..1098deb
--- /dev/null
@@ -0,0 +1,53 @@
+Name:       xdgmime
+Summary:    Pkg xdgmime
+Version:    0.0.12
+Release:    4
+Group:      TO_BE/FILLED_IN
+License:    TO_BE_FILLED
+Source0:    xdgmime-%{version}.tar.gz
+Requires(post): /sbin/ldconfig, /bin/chown, /bin/chmod
+Requires(postun): /sbin/ldconfig
+BuildRequires:  cmake
+
+%description
+Package xdgmime 
+
+%package devel
+Summary:    Pkg xdgmime (devel)
+Group:      Development/Libraries
+Requires:   %{name} = %{version}-%{release}
+
+%description devel
+Package xdgmime (devel)
+
+%prep
+%setup -q -n %{name}-%{version}
+
+%build
+cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix}
+
+make %{?jobs:-j%jobs}
+
+%install
+rm -rf %{buildroot}
+%make_install
+mkdir -p %{buildroot}/usr/share/license
+install COPYING %{buildroot}/usr/share/license/%{name}
+
+%post
+chmod 644 /usr/lib/libxdgmime.so.1.1.0
+/sbin/ldconfig
+
+%postun -p /sbin/ldconfig
+
+%files
+%defattr(-,root,root,-)
+/usr/lib/libxdgmime.so.*
+/usr/share/license/%{name}
+%manifest xdgmime.manifest
+
+%files devel
+%defattr(-,root,root,-)
+/usr/include/xdgmime.h
+/usr/lib/libxdgmime.so
+/usr/lib/pkgconfig/xdgmime.pc
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
new file mode 100644 (file)
index 0000000..a6afaea
--- /dev/null
@@ -0,0 +1,6 @@
+# Test executables
+add_executable(xdgmime_test
+               xdgmime_test.c)
+target_link_libraries(xdgmime_test xdgmime)
+#INSTALL(TARGETS xdgmime_test DESTINATION bin)
+
diff --git a/test/perf.h b/test/perf.h
new file mode 100755 (executable)
index 0000000..29474e7
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * perf.h
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: Jayoun Lee                         <airjany@samsung.com>
+ * Contact: Seokkyu Jang                        <seokkyu.jang@samsung.com>
+ * Contact: Sangil Yoon                         <si83.yoon@samsung.com>
+ *
+ * This library is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the
+ * Free Software Foundation; either version 2.1 of the License, or (at your option)
+ * any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, write to the Free Software Foundation, Inc., 51
+ * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#ifndef __PERF_H__
+#define __PERF_H__
+
+#ifdef PERF_ACTIVATE
+
+#include <sys/time.h>
+static struct timeval __g_base_time = {
+       .tv_sec = 0,
+       .tv_usec = 0
+};
+
+#define INIT_PERF(kb)\
+do{\
+       struct timeval tv;\
+       gettimeofday(&tv, NULL);\
+       __g_base_time.tv_sec = tv.tv_sec;\
+       __g_base_time.tv_usec = tv.tv_usec;\
+}while(0);
+
+#define PERF(fmt,arg...)\
+do{\
+       struct timeval cur;\
+       struct timeval res;\
+       gettimeofday(&cur,NULL);\
+       if(__g_base_time.tv_sec != 0){\
+               timersub(&cur, &__g_base_time, &res);\
+                printf("%c[1;31m[%s,%d] %u sec %u msec "fmt" %c[0m\n",\
+                       27,__FUNCTION__,__LINE__,\
+                               res.tv_sec,res.tv_usec/1000,##arg,27);\
+       }\
+}while(0);
+
+#else
+
+#define INIT_PERF(kb)
+#define PERF(fmt,arg...) 
+
+#endif
+
+#endif
diff --git a/test/xdgmime_test.c b/test/xdgmime_test.c
new file mode 100755 (executable)
index 0000000..483f128
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * xdgmime_test.c
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: Jayoun Lee                         <airjany@samsung.com>
+ * Contact: Seokkyu Jang                        <seokkyu.jang@samsung.com>
+ * Contact: Sangil Yoon                         <si83.yoon@samsung.com>
+ *
+ * This library is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the
+ * Free Software Foundation; either version 2.1 of the License, or (at your option)
+ * any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, write to the Free Software Foundation, Inc., 51
+ * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#include <stdio.h>
+#include "xdgmime/src/xdgmime.h"
+
+#define PERF_ACTIVATE
+#include "perf.h"
+
+int main(int argc, char** argv)
+{
+       const char* res;
+       if(argc < 2){
+               printf("[usage] %s <filename> or <mimetype>\n",argv[0]);
+               exit(0);
+       }
+
+       printf("unaliased = %s\n",xdg_mime_unalias_mime_type(argv[1]));
+
+       INIT_PERF("start");
+       res = xdg_mime_get_mime_type_for_file(argv[1],NULL);
+       if(strcmp(res,"application/octet-stream")==0)
+               res = xdg_mime_get_mime_type_from_file_name(argv[1]);
+       PERF("end");
+       printf("%s has a mime-type of %s\n", argv[1], res);
+
+       return 0;
+}
+
+/* vi: set ts=8 sts=8 sw=8: */
diff --git a/xdgmime.manifest b/xdgmime.manifest
new file mode 100644 (file)
index 0000000..75b0fa5
--- /dev/null
@@ -0,0 +1,5 @@
+<manifest>
+    <request>
+        <domain name="_"/>
+    </request>
+</manifest>
diff --git a/xdgmime.pc.in b/xdgmime.pc.in
new file mode 100644 (file)
index 0000000..36126f6
--- /dev/null
@@ -0,0 +1,13 @@
+# Package Information for pkg-config
+
+prefix=/usr
+exec_prefix=@EXEC_PREFIX@
+libdir=@LIBDIR@
+includedir=@INCLUDEDIR@
+
+Name: libxdgmime
+Description: FreeDesktop Shared-Mime-Info Library
+Version: @VERSION@
+Requires: 
+Libs: -L${libdir} -lxdgmime
+Cflags: -I${includedir}
diff --git a/xdgmime/ChangeLog b/xdgmime/ChangeLog
new file mode 100644 (file)
index 0000000..85e0504
--- /dev/null
@@ -0,0 +1,741 @@
+2009-10-06  Bastien Nocera  <hadess@hadess.net>
+
+       * src/xdgmimecache.c (cache_glob_lookup_file_name):
+       * src/xdgmimeglob.c (_xdg_glob_hash_lookup_file_name):
+       Remove unused variables
+
+2009-10-02  Alexander Larsson  <alexl@redhat.com>
+
+       * src/xdgmime.c: (xdg_mime_get_icon):
+       * src/xdgmimecache.c: (_xdg_mime_cache_get_icon):
+       Fix mime lookup order, imported from glib copy.
+       Fixes gnome bug #555711
+
+2009-10-02  Alexander Larsson  <alexl@redhat.com>
+
+       * src/xdgmimecache.c: (cache_magic_matchlet_compare_to_data):
+       Use correct bounds check for magic ranges
+
+2009-10-02  Alexander Larsson  <alexl@redhat.com>
+
+       * src/xdgmimecache.c: (cache_glob_lookup_literal),
+       (cache_glob_lookup_fnmatch), (cache_glob_node_lookup_suffix),
+       (cache_glob_lookup_suffix), (ascii_tolower),
+       (cache_glob_lookup_file_name):
+       * src/xdgmimeglob.c: (_xdg_glob_list_append),
+       (_xdg_glob_hash_insert_ucs4), (_xdg_glob_hash_insert_text),
+       (_xdg_glob_hash_node_lookup_file_name), (ascii_tolower),
+       (_xdg_glob_hash_lookup_file_name), (_xdg_glob_hash_append_glob):
+       Support the case-sensitive attribute
+
+2009-10-02  Alexander Larsson  <alexl@redhat.com>
+
+       * src/xdgmimecache.c: (_xdg_mime_cache_new_from_file):
+       Support reading cache files with minor number 2
+
+2009-10-02  Alexander Larsson  <alexl@redhat.com>
+
+       * src/xdgmime.c: (xdg_mime_init_from_directory):
+       * src/xdgmimeglob.c: (_xdg_glob_hash_append_glob),
+       (_xdg_mime_glob_read_from_file):
+       * src/xdgmimeglob.h:
+       Read the new updated glob2 format with flags
+
+2009-03-09  Bastien Nocera  <hadess@hadess.net>
+
+       * src/test-mime.c (test_one_icon): Fix possible
+       crash when xdg_mime_get_icon() returns NULL, spotted by
+       Sanel Zukan <sanelz@gmail.com> (Closes: #20555)
+
+2009-01-08  Bastien Nocera  <hadess@hadess.net>
+
+       * src/xdgmime.c (xdg_mime_shutdown): Patch from
+       Carlos Garcia Campos <carlosgc@gnome.org> to
+       fix a memory leak on shutdown (Closes: #16972)
+
+2008-09-27  Bastien Nocera  <hadess@hadess.net>
+
+       * src/xdgmime.c (xdg_dir_time_list_add): Patch from
+       Christian Persch, closing a memleak (Closes: #17464)
+
+2008-06-09  Bastien Nocera  <hadess@hadess.net>
+
+       * src/xdgmimecache.c (cache_glob_node_lookup_suffix):
+       Patch by Matthias Clasen <mclasen@redhat.com> to implement
+       the compact suffix tree
+
+2008-06-05  Bastien Nocera  <hadess@hadess.net>
+
+       * src/xdgmimecache.c (cache_glob_node_lookup_suffix):
+       Patch by Matthias Clasen <mclasen@redhat.com> to fix reading
+       the suffix lookup (wrong offsets when reading the mime-type
+       offset and weight)
+
+2008-06-03  Bastien Nocera  <hadess@hadess.net>
+
+       * src/xdgmime.c (xdg_mime_dump):
+       * src/xdgmimecache.c (cache_glob_node_lookup_suffix),
+       (dump_glob_node), (_xdg_mime_cache_glob_dump):
+       * src/xdgmimecache.h: dumping code for the suffix reverse tree
+       from Matthias Clasen <mclasen@redhat.com>, and hooked up in
+       xdg_mime_dump, init the cache in xdg_mime_dump() in case
+       it's the only xdgmime function called
+
+2008-06-03  Bastien Nocera  <hadess@hadess.net>
+
+       * src/xdgmimecache.c: Patch from Matthias Clasen
+       <mclasen@redhat.com>: Bump the cache version to 1.1
+
+2008-06-03  Bastien Nocera  <hadess@hadess.net>
+
+       * src/Makefile:
+       * src/test-mime.c (test_one_icon), (test_icons), (main):
+       * src/xdgmime.c (xdg_mime_init_from_directory), (xdg_mime_init),
+       (xdg_mime_get_icon), (xdg_mime_get_generic_icon):
+       * src/xdgmime.h:
+       * src/xdgmimecache.c (cache_lookup_icon),
+       (_xdg_mime_cache_get_generic_icon), (_xdg_mime_cache_get_icon):
+       * src/xdgmimecache.h:
+       * src/xdgmimeicon.c:
+       * src/xdgmimeicon.h: Patch from Matthias Clasen
+       <mclasen@redhat.com>: Add icon and generic-icon support
+
+2008-06-03  Bastien Nocera  <hadess@hadess.net>
+
+       * src/xdgmimecache.c (cache_glob_lookup_file_name):
+       * src/xdgmimeglob.c (_xdg_glob_hash_insert_text),
+       (_xdg_glob_hash_lookup_file_name):
+       * src/xdgmimeint.c (_xdg_convert_to_ucs4), (_xdg_reverse_ucs4):
+       * src/xdgmimeint.h: Patch from Matthias Clasen
+       <mclasen@redhat.com>: Cleanups
+
+2008-06-03  Bastien Nocera  <hadess@hadess.net>
+
+       * src/xdgmimecache.c (cache_glob_node_lookup_suffix),
+       (cache_glob_lookup_suffix), (to_ucs4),
+       (cache_glob_lookup_file_name):
+       * src/xdgmimeglob.c (_xdg_glob_hash_insert_ucs4), (to_ucs4),
+       (ucs4_reverse), (_xdg_glob_hash_insert_text),
+       (_xdg_glob_hash_node_lookup_file_name),
+       (_xdg_glob_hash_lookup_file_name): Patch from Matthias Clasen
+       <mclasen@redhat.com>: Use reverted suffix trees
+
+2008-06-03  Bastien Nocera  <hadess@hadess.net>
+
+       * src/test-mime.c (test_subclassing), (test_one_match),
+       (test_matches), (main): Patch from Matthias Clasen
+       <mclasen@redhat.com>: Add some glob tests
+
+2008-06-03  Bastien Nocera  <hadess@hadess.net>
+
+       * src/xdgmime.c (xdg_mime_dump): Patch from Matthias Clasen
+       <mclasen@redhat.com>: Include globs in the dump
+
+2008-06-03  Bastien Nocera  <hadess@hadess.net>
+
+       * src/xdgmime.c (xdg_mime_init_from_directory):
+       * src/xdgmimecache.c (cache_glob_lookup_literal),
+       (cache_glob_lookup_fnmatch), (cache_glob_node_lookup_suffix),
+       (cache_glob_lookup_suffix), (find_stopchars),
+       (compare_mime_weight), (cache_glob_lookup_file_name),
+       (_xdg_mime_cache_get_mime_type_for_file):
+       * src/xdgmimeglob.c (_xdg_glob_list_append),
+       (_xdg_glob_hash_node_dump), (_xdg_glob_hash_insert_text),
+       (_xdg_glob_hash_node_lookup_file_name), (compare_mime_weight),
+       (_xdg_glob_hash_lookup_file_name), (_xdg_glob_determine_type),
+       (_xdg_glob_hash_append_glob), (_xdg_glob_hash_dump),
+       (_xdg_mime_glob_read_from_file):
+       * src/xdgmimeglob.h: Patch from Matthias Clasen
+       <mclasen@redhat.com>: Implement glob weights
+
+2008-06-03  Bastien Nocera  <hadess@hadess.net>
+
+       * src/xdgmimeglob.c (_xdg_glob_hash_insert_text),
+       (_xdg_glob_hash_node_lookup_file_name), (_xdg_glob_determine_type),
+       (_xdg_mime_glob_read_from_file): Patch from Matthias Clasen
+       <mclasen@redhat.com>: remove dead code and some trivial cleanups
+
+2008-06-02  Bastien Nocera  <hadess@hadess.net>
+
+       * src/Makefile: Remove test-mime-data on clean
+       * src/test-mime-data.c (test_by_data): Fix for API changes below
+
+       * src/xdgmime.c (xdg_dir_time_list_add),
+       (xdg_mime_init_from_directory), (xdg_check_file), (xdg_check_dir),
+       (xdg_mime_get_mime_type_for_data),
+       (xdg_mime_get_mime_type_for_file),
+       (xdg_mime_get_mime_type_from_file_name),
+       (xdg_mime_get_mime_types_from_file_name), (xdg_mime_shutdown),
+       (xdg_mime_get_max_buffer_extents), (_xdg_mime_unalias_mime_type),
+       (xdg_mime_media_type_equal), (_xdg_mime_mime_type_subclass),
+       (xdg_mime_list_mime_parents):
+       * src/xdgmime.h:
+       * src/xdgmimealias.h:
+       * src/xdgmimecache.c (cache_magic_lookup_data),
+       (cache_alias_lookup), (cache_glob_lookup_literal),
+       (cache_glob_lookup_fnmatch), (cache_glob_node_lookup_suffix),
+       (cache_glob_lookup_suffix), (find_stopchars),
+       (_xdg_mime_cache_get_max_buffer_extents),
+       (cache_get_mime_type_for_data),
+       (_xdg_mime_cache_get_mime_type_for_data),
+       (_xdg_mime_cache_get_mime_type_for_file),
+       (_xdg_mime_cache_get_mime_type_from_file_name),
+       (_xdg_mime_cache_get_mime_types_from_file_name),
+       (_xdg_mime_cache_mime_type_subclass),
+       (_xdg_mime_cache_list_mime_parents):
+       * src/xdgmimecache.h:
+       * src/xdgmimeglob.c (_xdg_glob_hash_node_lookup_file_name):
+       * src/xdgmimeglob.h:
+       * src/xdgmimemagic.c (_xdg_mime_magic_parse_magic_line),
+       (_xdg_mime_magic_lookup_data):
+       * src/xdgmimemagic.h:
+       * src/xdgmimeparent.h:
+
+       Merge from downstream changes in GIO/GTK+:
+       - Rename _xdg_mime_caches to _caches and make sure it's not exported
+       - Rework the timestamp checking code
+       to protect against duplicate directories in XDG_DATA_DIRS.
+       - Fix a thinko that leads to constantly reloading
+       the mime data if a mime.cache is present.
+       - Support more than 2 duplicate globs
+       - Add xdg_mime_get_mime_types_from_file_name()
+       - xdg_mime_media_type_equal() doesn't require an _init after all
+       - Handle super-types in _xdg_mime_mime_type_subclass()
+       - Make cache_get_mime_type_for_data() and
+       _xdg_mime_magic_lookup_data() return the priority of the
+       matching mime-type
+       - Fix _xdg_mime_cache_list_mime_parents returning duplicate parents in
+       some cases
+
+2008-06-02  Bastien Nocera  <hadess@hadess.net>
+
+       * src/xdgmime.c (xdg_mime_init_from_directory), (xdg_mime_dump):
+       * src/xdgmimecache.c (cache_glob_lookup_literal),
+       (cache_glob_lookup_fnmatch), (cache_glob_node_lookup_suffix),
+       (cache_glob_lookup_suffix), (find_stopchars),
+       (cache_glob_lookup_file_name),
+       (_xdg_mime_cache_get_mime_type_for_file):
+       * src/xdgmimeglob.c (_xdg_glob_list_append),
+       (_xdg_glob_list_prepend), (_xdg_glob_hash_node_dump),
+       (_xdg_glob_hash_insert_text),
+       (_xdg_glob_hash_node_lookup_file_name),
+       (_xdg_glob_hash_lookup_file_name), (_xdg_glob_determine_type),
+       (_xdg_glob_hash_append_glob), (_xdg_glob_hash_dump),
+       (_xdg_mime_glob_read_from_file):
+       * src/xdgmimeglob.h: Revert previous patches, we need to rebase first
+
+2008-06-02  Bastien Nocera  <hadess@hadess.net>
+
+       * src/test-mime.c (test_subclassing), (test_one_match),
+       (test_matches), (main): Patch from Matthias Clasen
+       <mclasen@redhat.com> to add some globs tests to the
+       test program
+
+2008-06-02  Bastien Nocera  <hadess@hadess.net>
+
+       * src/xdgmime.c (xdg_mime_dump): Patch from Matthias Clasen
+       <mclasen@redhat.com> to include globs output in the dump
+
+2008-06-02  Bastien Nocera  <hadess@hadess.net>
+
+       * src/xdgmime.c (xdg_mime_init_from_directory):
+       * src/xdgmimecache.c (cache_glob_lookup_literal),
+       (cache_glob_lookup_fnmatch), (cache_glob_node_lookup_suffix),
+       (cache_glob_lookup_suffix), (find_stopchars),
+       (compare_mime_weight), (cache_glob_lookup_file_name),
+       (_xdg_mime_cache_get_mime_type_for_file):
+       * src/xdgmimeglob.c (_xdg_glob_list_append),
+       (_xdg_glob_hash_node_dump), (_xdg_glob_hash_insert_text),
+       (_xdg_glob_hash_node_lookup_file_name), (compare_mime_weight),
+       (_xdg_glob_hash_lookup_file_name), (_xdg_glob_determine_type),
+       (_xdg_glob_hash_append_glob), (_xdg_glob_hash_dump),
+       (_xdg_mime_glob_read_from_file):
+       * src/xdgmimeglob.h: Patch from Matthias Clasen <mclasen@redhat.com>
+       to implement glob weights
+
+2008-06-02  Bastien Nocera  <hadess@hadess.net>
+
+       * src/xdgmimeglob.c (_xdg_glob_hash_insert_text),
+       (_xdg_glob_hash_node_lookup_file_name), (_xdg_glob_determine_type),
+       (_xdg_mime_glob_read_from_file): Patch from Matthias Clasen
+       <mclasen@redhat.com> to remove dead code and do some trivial changes
+
+2008-04-18  Bastien Nocera  <hadess@hadess.net>
+
+       * src/xdgmimecache.c (cache_magic_lookup_data),
+       (_xdg_mime_cache_mime_type_subclass): Fix possible crasher when
+       XDG_DATA_DIRS contains the same directory twice, patch by Joe Shaw
+       <joeshaw@novell.com> (#12512)
+
+       Make _xdg_mime_cache_mime_type_subclass use the internal version of
+       xdg_mime_media_type_equal(), spotted by Federico Mena-Quintero
+       <federico@ximian.com>
+
+2008-04-10  Bastien Nocera  <hadess@hadess.net>
+
+       * src/test-mime-data.c (main): Only error out when
+       the number of errors is different from the number of expected errors,
+       or when there's unexpected success
+
+2007-08-28  Bastien Nocera  <hadess@hadess.net>
+
+       * src/.cvsignore:
+       * src/Makefile:
+       * src/test-mime-data.c: Add test-mime-data test program from
+       Matthias Clasen <mclasen@redhat.com> (Closes: #5210)
+
+2007-06-04  Christian Neumair  <cneumair@gnome.org>
+
+       * src/xdgmime*.[ch]: Rename _caches to _xdg_mime_caches. Also
+       namespace all reserved symbols that are declared in headers using
+       XDG_RESERVED_ENTRY(), #7074 and #11124. Thanks to Yevgen Muntyan
+       <muntyan@tamu.edu> and Joe Marcus Clarke <marcus@freebsd.org>.
+
+2007-06-02  Christian Neumair  <cneumair@gnome.org>
+
+       * src/xdgmime.[ch]: Move xdg_mime_type_unknown to .rodata.
+       * src/xdgmime.h: Add XDG_ENTRY() entry for xdg_mime_dump.
+       GTK+ resync.
+
+       * src/xdgmime.c: (_xdg_mime_alias_list_lookup): Initialize mime_type
+       to NULL instead of 0, GNOME #341812. Thanks to Kjartan Maraas
+       <kmaraas@gnome.org>.
+
+       * src/xdgmime.[ch]: (xdg_mime_media_type_equal),
+       (_xdg_mime_media_type_equal): Add private version of
+       xdg_mime_media_type_equal that doesn't call xdg_mime_init(), use it in
+       _xdg_mime_mime_type_subclass.
+
+2007-06-02  Christian Neumair  <cneumair@gnome.org>
+
+       * src/xdgmimemagic.c: Fallback to getc() if getc_unlocked() is not
+       available, GNOME #381499. Thanks to Paul <ephraim_owns@hotmail.com>.
+
+2007-06-01  Christian Neumair  <cneumair@gnome.org>
+
+       * src/xdgmime.c: (xdg_mime_get_mime_type_for_file): Only request two
+       MIME types from cache.
+
+       * src/xdgmime.c: (xdg_mime_dump): Dump cache.
+
+       * src/xdgmimecache.c: Warn if building without MMAP support.
+
+2007-06-01  Christian Neumair  <cneumair@gnome.org>
+
+       * src/xdgmime.c: (xdg_lookup_cache_for_file), (xdg_check_dir):
+       Fix constant re-creation of memory MIME cache, #7495.
+
+2007-06-01  Christian Neumair  <cneumair@gnome.org>
+
+       * src/xdgmimeglob.c: (_xdg_glob_hash_node_lookup_file_name): Don't
+       return NULL MIME types, #5241.
+
+2007-06-01  Christian Neumair  <cneumair@gnome.org>
+
+       * src/xdgmime.c: (xdg_mime_shutdown): Fix cache variable name from last
+       commit.
+
+       * src/xdgmime.c: (xdg_mime_unalias_mime_type), (xdg_mime_mime_type_equal):
+       * src/xdgmime.h:
+       * src/xdgmimemagic.c: (_xdg_mime_magic_lookup_data):
+       Define and use private versions of functions that may lead to an
+       xdg_mime_init() call and thus to an invalid cache, and a crash - #6824.
+       Thanks to Joe Shaw <joeshaw@novell.com>.
+
+2007-06-01  Christian Neumair  <cneumair@gnome.org>
+
+       * src/xdgmime.c: (xdg_mime_shutdown): Free caches, #7496.
+       Thanks to Yevgen Muntyan <muntyan@tamu.edu>.
+
+2007-06-01  Christian Neumair  <cneumair@gnome.org>
+
+       * src/xdgmimecache.c: (cache_glob_node_lookup_suffix): Don't return
+       MIME types if cache suffix lookup returns empty string, #9544.
+       Thanks to Yevgen Muntyan <muntyan@tamu.edu>.
+
+2007-06-01  Christian Neumair  <cneumair@gnome.org>
+
+       * src/xdgmimemagic.c: (_xdg_mime_magic_lookup_data): Fix issue where
+       MIME type was not detected when two MIME types with parent
+       relationship match and have equal priority, and child
+       was found before parent in cache, #9242.
+       Thanks to Tom Parker <freedesktop@tevp.net>.
+
+2007-06-01  Christian Neumair  <cneumair@gnome.org>
+
+       * src/xdgmimecache.c: (_xdg_mime_cache_list_mime_parents): Fix
+       several problems with this function, #9560. Thanks to Yevgen Muntyan.
+
+2007-06-01  Christian Neumair  <cneumair@gnome.org>
+
+       * src/xdgmimemagic.c: Don't declare errno, fixes build on Dragonfly
+       BSD, GNOME #336382.
+
+2006-07-13  Christian Neumair  <chris@gnome-de.org>
+
+       * src/xdgmimemagic.c: (_xdg_mime_magic_parse_magic_line):
+       Only declare 'i' #if LITTLE_ENDIAN. Partially fixes GNOME #340277.
+       Thanks to James Andrewartha.
+
+2006-03-02  Christian Neumair  <chris@gnome-de.org>
+
+       * src/xdgmimeglob.c: (_xdg_glob_hash_insert_text),
+       (_xdg_glob_hash_append_glob): Don't strdup the MIME type when passing
+       it to _xdg_glob_hash_insert_text, but let the function itself figure
+       out whether string duplication is needed. Fixes #5993.
+       Thanks to Martin Wehner.
+
+2006-02-25  Christian Neumair  <chris@gnome-de.org>
+
+       * src/xdgmimemagic.c: (_xdg_mime_magic_lookup_data): Make
+       priority equality check more robust so that it works for three or more
+       matches. Also allow matchlets with 0 priority.
+
+2006-02-19  Christian Neumair  <chris@gnome-de.org>
+
+       * src/xdgmimemagic.c: (_xdg_mime_magic_lookup_data): When two
+       unrelated MIME types with equal priorities match, don't assume one of
+       them matches.
+       http://bugzilla.gnome.org/show_bug.cgi?id=331719
+
+2006-01-03  Christian Neumair  <chris@gnome-de.org>
+
+       * src/xdgmimemagic.c: (_xdg_mime_magic_lookup_data): Also consider
+       match priority for the returned MIME type.
+
+2005-12-01  Christian Neumair  <chris@gnome-de.org>
+
+       * src/xdgmime.c: (xdg_mime_get_mime_type_from_file_name):
+       * src/xdgmimecache.c: (_xdg_mime_cache_get_mime_type_from_file_name):
+       Return XDG_MIME_TYPE_UNKNOWN if multiple MIME types match a simple
+       pattern.
+
+2005-12-01  Matthias Clasen  <mclasen@redhat.com>
+
+       * src/xdgmimecache.h:
+       * src/xdgmimecache.c (_xdg_mime_cache_get_mime_type_for_file): 
+       Allow passing in a struct stat * to avoid re-stat()ing. 
+       Forgotten commit from an earlier entry.
+
+       * src/xdgmimecache.c (cache_glob_lookup_literal):
+       (cache_glob_lookup_fnmatch):
+       (cache_glob_node_lookup_suffix): Change these functions to
+       allow returning more than one mime type if identical globs
+       match.
+
+       * src/xdgmimemagic.h: 
+       * src/xdgmimeglob.h: 
+       * src/xdgmimemagic.c (_xdg_mime_magic_lookup_data): 
+       * src/xdgmimeglob.c (_xdg_glob_hash_lookup_file_name): 
+       Change these functions to allow returning more than one
+       mime type if identical globs match.
+       
+       * src/xdgmimecache.c (_xdg_mime_cache_get_mime_type_for_file): 
+       * src/xdgmime.c (xdg_mime_get_mime_type_for_file): 
+       If multiple identical globs match, use magic to disambiguate.
+       
+2005-11-04  Matthias Clasen  <mclasen@redhat.com>
+
+       * xdgmime.c (xdg_mime_list_mime_parents): Prevent
+       a segfault.
+
+2005-10-18  Matthias Clasen  <mclasen@redhat.com>
+       
+       * src/xdgmime.c:
+       * src/xdgmimecache.h:
+       * src/xdgmimecache.c: Make the array of caches NULL-terminated
+       and rename it to _caches.  (#4011)
+
+       * src/xdgmime.h:
+       * src/xdgmime.c: Add a struct statbuf * argument to 
+       xdg_mime_get_mime_type_for_file().  (#3529)
+
+       * src/test-mime.c: Adjust callers. Add License.
+
+       * src/xdgmimecache.c: Make magic comparisons work correctly.
+
+Thu Jun  9 23:55:25 2005  Jonathan Blandford  <jrb@redhat.com>
+
+       * src/xdgmime.c (xdg_mime_init_from_directory): patch from
+       federico to realloc the right size, #3506
+
+2005-04-17  Christophe Fergeau  <teuf@gnome.org>
+
+       * src/xdgmimeint.c: fix gcc4 signedness warning
+
+2005-04-17  Christophe Fergeau  <teuf@gnome.org>
+
+       * src/xdgmimemagic.c: (_xdg_mime_magic_matchlet_compare_to_data),
+       (_xdg_mime_magic_matchlet_compare_level),
+       (_xdg_mime_magic_lookup_data): when  magic patterns matches, check if
+       there aren't subtypes with matching patterns too, and if so, favour
+       the subtype over the parent type, should fix #2686
+
+2005-04-16  Christophe Fergeau  <teuf@gnome.org>
+
+       * src/xdgmime.c: (xdg_mime_init_from_directory): fix leak when
+       mime.cache doesn't exist
+
+2005-04-16  Christophe Fergeau  <teuf@gnome.org>
+
+       * src/test-mime.c: (main): disabled call to xdg_mime_dump for now
+       * src/xdgmimemagic.c: (_xdg_mime_magic_matchlet_compare_to_data):
+       fixed off by 1 error when handling offsets, fixes bug #2050 and 
+       partly bug #2359
+
+Fri Apr  8 23:37:33 2005  Jonathan Blandford  <jrb@redhat.com>
+
+       * src/xdgmimecache.c: Actually add the file.  Also, patch from
+       Matthias Clasen <mclasen@redhat.com> to fix small bugs, #2939
+
+Fri Apr  1 14:59:43 2005  Jonathan Blandford  <jrb@redhat.com>
+
+       * src/xdgmimecache.c: Patch from Matthias Clasen to mmap the
+       cached xdg file.
+
+Mon Mar 28 13:58:32 2005  Jonathan Blandford  <jrb@redhat.com>
+
+       * src/xdgmimeglob.c (_xdg_glob_hash_insert_text): patch from
+       Matthias Clasen to handle globs that don't have '.' chars in
+       them.  As an example 'foo~' should match '*~'
+
+Mon Mar 21 13:16:12 2005  Jonathan Blandford  <jrb@redhat.com>
+
+       * src/xdgmime.c (xdg_mime_shutdown): fix from  Axel Liljencrantz
+       <f97-ali@nada.kth.se> to free parent_list in shutdown.
+
+2005-01-10  Christophe Fergeau  <teuf@gnome.org>
+
+       * src/xdgmimeglob.c: (_xdg_glob_hash_lookup_file_name): make previous
+       commit actually work...
+
+2005-01-10  Christophe Fergeau  <teuf@gnome.org>
+
+       * src/xdgmimeglob.c: (_xdg_glob_hash_lookup_file_name): don't get
+       confused by multiple dots in filenames when doing extension matching
+
+2004-12-13  Marco Pesenti Gritti  <marco@gnome.org>
+
+       * src/xdgmime.h:
+
+        wrap new API in XDG_ENTRY()
+
+2004-12-13  Marco Pesenti Gritti  <marco@gnome.org>
+
+       * src/xdgmimeglob.c: (_xdg_glob_hash_lookup_file_name):
+
+       Do not assume the filename is UTF8. We just need to look
+       for the dot which is ASCII.
+
+2004-12-09  Marco Pesenti Gritti  <marco@gnome.org>
+
+       * src/xdgmimeint.h:
+
+       Remove spacings I introduced by mistake
+
+2004-12-09  Marco Pesenti Gritti  <marco@gnome.org>
+
+       * src/xdgmimealias.c: (_xdg_mime_alias_read_from_file):
+       * src/xdgmimeint.c: (_xdg_ucs4_to_lower):
+       * src/xdgmimeint.h:
+       * src/xdgmimeparent.c: (_xdg_mime_parent_read_from_file):
+
+       Check in Mariano Suárez-Alvarez <msuarezalvarez@arnet.com.ar> patch
+       for GNOME bug #160838.
+
+2004-12-09  Marco Pesenti Gritti  <marco@gnome.org>
+
+       * src/xdgmimeglob.c: (_xdg_glob_hash_node_lookup_file_name):
+       * src/xdgmimeint.c: (_xdg_ucs4_to_lower):
+       * src/xdgmimeint.h:
+
+       Follow the freedesktop spec about case sensitiveness. Fix #732
+
+2004-12-08  Christophe Fergeau  <teuf@gnome.org>
+
+       * src/xdgmimeglob.c: (_xdg_mime_glob_read_from_file): backing out
+       "fix" for bug #1048 since it frees memory that shouldn't be freed.
+
+2004-12-08  Marco Pesenti Gritti  <marco@gnome.org>
+
+       * src/xdgmimemagic.c: (_xdg_mime_magic_read_from_file):
+
+       Check that fread succeeded reading all chars. Fix #1049
+
+2004-12-08  Marco Pesenti Gritti  <marco@gnome.org>
+
+       * src/xdgmime.c:
+       * src/xdgmimealias.c:
+       * src/xdgmimeglob.c:
+       * src/xdgmimeint.c:
+       * src/xdgmimemagic.c:
+       * src/xdgmimeparent.c:
+
+       Include config.h. Fix #913
+
+2004-12-08  Marco Pesenti Gritti  <marco@gnome.org>
+
+       * src/xdgmimealias.c: (_xdg_mime_alias_list_lookup):
+
+       Fix a typo
+
+2004-12-08  Marco Pesenti Gritti  <marco@gnome.org>
+
+       * src/xdgmime.c: (xdg_mime_unalias_mime_type),
+       (xdg_mime_mime_type_equal), (xdg_mime_mime_type_subclass),
+       (xdg_mime_get_mime_parents):
+       * src/xdgmime.h:
+
+       Add apis to get parents and to unalias mime type
+
+2004-12-08  Marco Pesenti Gritti  <marco@gnome.org>
+
+       * src/xdgmimealias.c: (_xdg_mime_alias_list_lookup):
+       * src/xdgmimeparent.c: (_xdg_mime_parent_list_lookup):
+
+       Protect against stupid bsearch() implementations.  (#1961,
+       Morten Welinder)
+
+2004-12-08  Marco Pesenti Gritti  <marco@gnome.org>
+
+       * src/xdgmimeparent.c: (_xdg_mime_parent_read_from_file):
+
+       Initialize the parent field of the newly allocate list 
+       entry.  (#1916, Alex Larsson)
+
+2004-12-08  Marco Pesenti Gritti  <marco@gnome.org>
+
+       * src/xdgmimeglob.c: (_xdg_mime_glob_read_from_file):
+
+       Patch from Matthias Clasen <mclasen@redhat.com> to fix
+       a mem leak. Bug #1048
+
+2004-12-08  Marco Pesenti Gritti  <marco@gnome.org>
+
+       * src/xdgmimeglob.h:
+
+       Patch from Michael.Wilson@bull.net to fix compile error on AIX
+
+Sun Nov  7 02:25:21 2004  Jonathan Blandford  <jrb@redhat.com>
+
+       * src/xdgmime.h: Patch from Matthias Clasen <mclasen@redhat.com>
+       to add alias and inheritence support.
+
+2004-09-16  Christophe Fergeau  <teuf@gnome.org>
+
+       * src/xdgmimeglob.c: (_xdg_glob_hash_free_nodes):
+       * src/xdgmimemagic.c: (_xdg_mime_magic_free): fix memory leaks, 
+         fixes http://bugzilla.gnome.org/show_bug.cgi?id=152771 and
+         http://bugzilla.gnome.org/show_bug.cgi?id=152768
+
+Mon Jul 19 00:23:00 2004  Jonathan Blandford  <jrb@gnome.org>
+
+       * src/xdgmime.c (xdg_mime_register_reload_callback): register a
+       callback when we reload MIME data.
+
+       * src/xdgmime.c (xdg_mime_remove_callback): Add capability to
+       remove callback.
+
+Sun Jul 18 20:56:22 2004  Jonathan Blandford  <jrb@gnome.org>
+
+       * src/xdgmime.c (xdg_mime_shutdown):
+       (xdg_mime_init): reread data when it changes on disk.
+
+Thu May 27 16:18:14 2004  Jonathan Blandford  <jrb@gnome.org>
+
+       * src/xdgmime.h: move xdg_mime_shutdown into the XDG_ENTRY guard.
+
+Thu May 27 15:02:13 2004  Jonathan Blandford  <jrb@gnome.org>
+
+       * src/xdgmimemagic.c (_xdg_mime_magic_read_magic_file): patch from
+       Hongli Lai <h.lai@chello.nl> to catch magic files that don't end
+       with a '\n'.
+
+Fri Apr 30 11:56:01 2004  Jonathan Blandford  <jrb@gnome.org>
+
+       * src/xdgmimemagic.c (_xdg_mime_magic_read_a_number): make the
+       buffer the right size.  Reported by Morten Welinder, #136323
+
+Sun Mar 21 23:56:46 2004  Jonathan Blandford  <jrb@gnome.org>
+
+       * src/xdgmimemagic.c: Patch from Arjan van de Ven
+       <arjanv@redhat.com> to do s/fgetc/getc_unlocked/g.
+
+Wed Mar 10 22:28:41 2004  Jonathan Blandford  <jrb@gnome.org>
+
+       * src/xdgmimemagic.c (_xdg_mime_magic_read_a_number): fix usage of
+       isdigit.  Reported by Morten Welinder, #136323
+
+       * src/xdgmimemagic.c (_xdg_mime_magic_read_magic_file): patch from
+       Christophe Fergeau to reverse the order of the matchlet before
+       adding it to the list.
+
+       * src/xdgmimeint.h (_xdg_utf8_skip): patch from Alexander Larsson
+       to make extern.
+
+Wed Jan 21 09:29:41 2004  Jonathan Blandford  <jrb@gnome.org>
+
+       * src/xdgmimemagic.c (_xdg_mime_magic_insert_match): dropped
+       patches.  Fix.
+
+       * src/xdgmimeglob.c (_xdg_glob_hash_free_nodes): dropped patches.
+       Fix.
+
+Tue Jan 20 14:55:39 2004  Jonathan Blandford  <jrb@gnome.org>
+
+       * src/xdgmime.h (XDG_MIME_TYPE_UNKNOWN): move the definition so
+       that it catches the XDG_ENTRY mangling.
+
+       * src/xdgmimemagic.c: make some functions static
+
+Tue Jan 20 14:34:26 2004  Jonathan Blandford  <jrb@gnome.org>
+
+       * src/xdgmime.c (xdg_mime_get_max_buffer_extents): add function so
+       that it's easy to get the max buffer extents.
+
+Tue Jan 20 12:55:55 2004  Jonathan Blandford  <jrb@gnome.org>
+
+       * src/Makefile: Test prefix code
+
+       * src/xdgmime*.h: Fully use the prefix code
+
+       * src/xdgmime.c: finish the syncing from both GTK+ and gnome-vfs.
+
+Tue Jan 13 16:21:04 2004  Jonathan Blandford  <jrb@gnome.org>
+
+       * src/xdgmime.[ch] (XDG_MIME_TYPE_UNKNOWN): make an extern const
+       char * so that comparisons can work.
+
+       * src/xdgmimeint.c (_xdg_utf8_to_ucs4): patch from Dave Jones
+       <davej@redhat.com> to make operations more explicit.
+
+Tue Oct 28 15:09:06 2003  Jonathan Blandford  <jrb@redhat.com>
+
+       * README: Add a readme, and clarify the licensing terms of the
+       software.
+
+Tue Oct 28 14:47:37 2003  Jonathan Blandford  <jrb@redhat.com>
+
+       * src/xdgmime.c (xdg_mime_shutdown): implement shutdown.  This
+       frees all memory and resets to an uninitialized state as best as
+       possible.
+
+Mon Oct 27 11:45:58 2003  Jonathan Blandford  <jrb@redhat.com>
+
+       * src/xdgmimemagic.c (_xdg_mime_magic_read_a_number): strtol
+       returns a long, not an int.  Thanks to Manish Singh for pointing
+       this out.
+
+       * src/xdgmimemagic.c (_xdg_mime_magic_parse_magic_line): change
+       assertion to avoid a warning.
+
+Tue Oct 21 15:56:55 2003  Jonathan Blandford  <jrb@gnome.org>
+
+       * Makefile: add a simple makefile
+       * src/Makefile: ditto
+
+Tue Jul 22 15:37:45 2003  Jonathan Blandford  <jrb@gnome.org>
+
+       * xdgmime/xdgmime.c (xdg_mime_init): use XDG_DATA_HOME instead of
+       XDG_CONFIG_HOME.
+
diff --git a/xdgmime/Makefile b/xdgmime/Makefile
new file mode 100644 (file)
index 0000000..28cfaef
--- /dev/null
@@ -0,0 +1,12 @@
+SUBDIRS=src
+
+all-recursive clean-recursive:
+       target=`echo $@ | sed s/-recursive//`;          \
+       for subdir in $(SUBDIRS); do                    \
+               (cd $$subdir && make "$$target")        \
+       done;
+
+all: all-recursive
+
+clean: clean-recursive
+       rm -f *~
diff --git a/xdgmime/README b/xdgmime/README
new file mode 100644 (file)
index 0000000..e7f3f68
--- /dev/null
@@ -0,0 +1,8 @@
+This module is a simple module that parses the proposed MIME spec listed
+at http://freedesktop.org/.  It is currently targetted at version 0.12.
+There are no formal releases planned for this module, and it is not
+intended to be installed at this time.  Rather, it is meant to be used
+by other libraries or applications to add support for the MIME system.
+
+It is dual-licensed under the terms of the GNU Lesser General Public
+License, and the Academic Free License, version 2.0.
diff --git a/xdgmime/src/.cvsignore b/xdgmime/src/.cvsignore
new file mode 100644 (file)
index 0000000..38ea779
--- /dev/null
@@ -0,0 +1,3 @@
+test-mime
+test-mime-data
+*.o
diff --git a/xdgmime/src/Makefile b/xdgmime/src/Makefile
new file mode 100644 (file)
index 0000000..c07f760
--- /dev/null
@@ -0,0 +1,12 @@
+
+CFLAGS=-Wall -Wmissing-prototypes  -Wno-sign-compare -g -DXDG_PREFIX=xdg_test -DHAVE_MMAP
+
+all: test-mime test-mime-data
+
+test-mime: xdgmime.o xdgmimeglob.o xdgmimeint.o xdgmimemagic.o xdgmimealias.o xdgmimeparent.o xdgmimecache.o xdgmimeicon.o
+
+test-mime-data: xdgmime.o xdgmimeglob.o xdgmimeint.o xdgmimemagic.o xdgmimealias.o xdgmimeparent.o xdgmimecache.o xdgmimeicon.o
+
+clean:
+       rm -f *~ *.o test-mime test-mime-data
+
diff --git a/xdgmime/src/xdgmime.c b/xdgmime/src/xdgmime.c
new file mode 100755 (executable)
index 0000000..3ec0969
--- /dev/null
@@ -0,0 +1,930 @@
+/* -*- mode: C; c-file-style: "gnu" -*- */
+/* xdgmime.c: XDG Mime Spec mime resolver.  Based on version 0.11 of the spec.
+ *
+ * More info can be found at http://www.freedesktop.org/standards/
+ * 
+ * Copyright (C) 2003,2004  Red Hat, Inc.
+ * Copyright (C) 2003,2004  Jonathan Blandford <jrb@alum.mit.edu>
+ *
+ * Licensed under the Academic Free License version 2.0
+ * Or under the following terms:
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "xdgmime.h"
+#include "xdgmimeint.h"
+#include "xdgmimeglob.h"
+#include "xdgmimemagic.h"
+#include "xdgmimealias.h"
+#include "xdgmimeicon.h"
+#include "xdgmimeparent.h"
+#include "xdgmimecache.h"
+#include <stdio.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/time.h>
+#include <unistd.h>
+#include <assert.h>
+
+#ifndef API
+#define API __attribute__ ((visibility("default")))
+#endif
+
+typedef struct XdgDirTimeList XdgDirTimeList;
+typedef struct XdgCallbackList XdgCallbackList;
+
+static int need_reread = TRUE;
+static time_t last_stat_time = 0;
+
+static XdgGlobHash *global_hash = NULL;
+static XdgMimeMagic *global_magic = NULL;
+static XdgAliasList *alias_list = NULL;
+static XdgParentList *parent_list = NULL;
+static XdgDirTimeList *dir_time_list = NULL;
+static XdgCallbackList *callback_list = NULL;
+static XdgIconList *icon_list = NULL;
+static XdgIconList *generic_icon_list = NULL;
+
+XdgMimeCache **_caches = NULL;
+static int n_caches = 0;
+
+const char xdg_mime_type_unknown[] = "application/octet-stream";
+
+
+enum
+{
+  XDG_CHECKED_UNCHECKED,
+  XDG_CHECKED_VALID,
+  XDG_CHECKED_INVALID
+};
+
+struct XdgDirTimeList
+{
+  time_t mtime;
+  char *directory_name;
+  int checked;
+  XdgDirTimeList *next;
+};
+
+struct XdgCallbackList
+{
+  XdgCallbackList *next;
+  XdgCallbackList *prev;
+  int              callback_id;
+  XdgMimeCallback  callback;
+  void            *data;
+  XdgMimeDestroy   destroy;
+};
+
+/* Function called by xdg_run_command_on_dirs.  If it returns TRUE, further
+ * directories aren't looked at */
+typedef int (*XdgDirectoryFunc) (const char *directory,
+                                void       *user_data);
+
+static void
+xdg_dir_time_list_add (char   *file_name, 
+                      time_t  mtime)
+{
+  XdgDirTimeList *list;
+
+  for (list = dir_time_list; list; list = list->next) 
+    {
+      if (strcmp (list->directory_name, file_name) == 0)
+        {
+          free (file_name);
+          return;
+        }
+    }
+  
+  list = calloc (1, sizeof (XdgDirTimeList));
+  list->checked = XDG_CHECKED_UNCHECKED;
+  list->directory_name = file_name;
+  list->mtime = mtime;
+  list->next = dir_time_list;
+  dir_time_list = list;
+}
+static void
+xdg_dir_time_list_free (XdgDirTimeList *list)
+{
+  XdgDirTimeList *next;
+
+  while (list)
+    {
+      next = list->next;
+      free (list->directory_name);
+      free (list);
+      list = next;
+    }
+}
+
+static int
+xdg_mime_init_from_directory (const char *directory)
+{
+  char *file_name;
+  struct stat st;
+
+  assert (directory != NULL);
+
+  file_name = malloc (strlen (directory) + strlen ("/mime/mime.cache") + 1);
+  strcpy (file_name, directory); strcat (file_name, "/mime/mime.cache");
+  if (stat (file_name, &st) == 0)
+    {
+      XdgMimeCache *cache = _xdg_mime_cache_new_from_file (file_name);
+
+      if (cache != NULL)
+       {
+         xdg_dir_time_list_add (file_name, st.st_mtime);
+
+         _caches = realloc (_caches, sizeof (XdgMimeCache *) * (n_caches + 2));
+         _caches[n_caches] = cache;
+          _caches[n_caches + 1] = NULL;
+         n_caches++;
+
+         return FALSE;
+       }
+    }
+  free (file_name);
+
+  file_name = malloc (strlen (directory) + strlen ("/mime/globs2") + 1);
+  strcpy (file_name, directory); strcat (file_name, "/mime/globs2");
+  if (stat (file_name, &st) == 0)
+    {
+      _xdg_mime_glob_read_from_file (global_hash, file_name, TRUE);
+      xdg_dir_time_list_add (file_name, st.st_mtime);
+    }
+  else
+    {
+      free (file_name);
+      file_name = malloc (strlen (directory) + strlen ("/mime/globs") + 1);
+      strcpy (file_name, directory); strcat (file_name, "/mime/globs");
+      if (stat (file_name, &st) == 0)
+        {
+          _xdg_mime_glob_read_from_file (global_hash, file_name, FALSE);
+          xdg_dir_time_list_add (file_name, st.st_mtime);
+        }
+      else
+        {
+          free (file_name);
+        }
+    }
+
+  file_name = malloc (strlen (directory) + strlen ("/mime/magic") + 1);
+  strcpy (file_name, directory); strcat (file_name, "/mime/magic");
+  if (stat (file_name, &st) == 0)
+    {
+      _xdg_mime_magic_read_from_file (global_magic, file_name);
+      xdg_dir_time_list_add (file_name, st.st_mtime);
+    }
+  else
+    {
+      free (file_name);
+    }
+
+  file_name = malloc (strlen (directory) + strlen ("/mime/aliases") + 1);
+  strcpy (file_name, directory); strcat (file_name, "/mime/aliases");
+  _xdg_mime_alias_read_from_file (alias_list, file_name);
+  free (file_name);
+
+  file_name = malloc (strlen (directory) + strlen ("/mime/subclasses") + 1);
+  strcpy (file_name, directory); strcat (file_name, "/mime/subclasses");
+  _xdg_mime_parent_read_from_file (parent_list, file_name);
+  free (file_name);
+
+  file_name = malloc (strlen (directory) + strlen ("/mime/icons") + 1);
+  strcpy (file_name, directory); strcat (file_name, "/mime/icons");
+  _xdg_mime_icon_read_from_file (icon_list, file_name);
+  free (file_name);
+
+  file_name = malloc (strlen (directory) + strlen ("/mime/generic-icons") + 1);
+  strcpy (file_name, directory); strcat (file_name, "/mime/generic-icons");
+  _xdg_mime_icon_read_from_file (generic_icon_list, file_name);
+  free (file_name);
+
+  return FALSE; /* Keep processing */
+}
+
+/* Runs a command on all the directories in the search path */
+static void
+xdg_run_command_on_dirs (XdgDirectoryFunc  func,
+                        void             *user_data)
+{
+  const char *xdg_data_home;
+  const char *xdg_data_dirs;
+  const char *ptr;
+
+  xdg_data_home = getenv ("XDG_DATA_HOME");
+  if (xdg_data_home)
+    {
+      if ((func) (xdg_data_home, user_data))
+       return;
+    }
+  else
+    {
+      const char *home;
+
+      home = getenv ("HOME");
+      if (home != NULL)
+       {
+         char *guessed_xdg_home;
+         int stop_processing;
+
+         guessed_xdg_home = malloc (strlen (home) + strlen ("/.local/share/") + 1);
+         strcpy (guessed_xdg_home, home);
+         strcat (guessed_xdg_home, "/.local/share/");
+         stop_processing = (func) (guessed_xdg_home, user_data);
+         free (guessed_xdg_home);
+
+         if (stop_processing)
+           return;
+       }
+    }
+
+  xdg_data_dirs = getenv ("XDG_DATA_DIRS");
+  if (xdg_data_dirs == NULL)
+    xdg_data_dirs = "/usr/local/share/:/usr/share/";
+
+  ptr = xdg_data_dirs;
+
+  while (*ptr != '\000')
+    {
+      const char *end_ptr;
+      char *dir;
+      int len;
+      int stop_processing;
+
+      end_ptr = ptr;
+      while (*end_ptr != ':' && *end_ptr != '\000')
+       end_ptr ++;
+
+      if (end_ptr == ptr)
+       {
+         ptr++;
+         continue;
+       }
+
+      if (*end_ptr == ':')
+       len = end_ptr - ptr;
+      else
+       len = end_ptr - ptr + 1;
+      dir = malloc (len + 1);
+      strncpy (dir, ptr, len);
+      dir[len] = '\0';
+      stop_processing = (func) (dir, user_data);
+      free (dir);
+
+      if (stop_processing)
+       return;
+
+      ptr = end_ptr;
+    }
+}
+
+/* Checks file_path to make sure it has the same mtime as last time it was
+ * checked.  If it has a different mtime, or if the file doesn't exist, it
+ * returns FALSE.
+ *
+ * FIXME: This doesn't protect against permission changes.
+ */
+static int
+xdg_check_file (const char *file_path,
+                int        *exists)
+{
+  struct stat st;
+
+  /* If the file exists */
+  if (stat (file_path, &st) == 0)
+    {
+      XdgDirTimeList *list;
+
+      if (exists)
+        *exists = TRUE;
+
+      for (list = dir_time_list; list; list = list->next)
+       {
+         if (! strcmp (list->directory_name, file_path))
+           {
+             if (st.st_mtime == list->mtime)
+               list->checked = XDG_CHECKED_VALID;
+             else 
+               list->checked = XDG_CHECKED_INVALID;
+
+             return (list->checked != XDG_CHECKED_VALID);
+           }
+       }
+      return TRUE;
+    }
+
+  if (exists)
+    *exists = FALSE;
+
+  return FALSE;
+}
+
+static int
+xdg_check_dir (const char *directory,
+              int        *invalid_dir_list)
+{
+  int invalid, exists;
+  char *file_name;
+
+  assert (directory != NULL);
+
+  /* Check the mime.cache file */
+  file_name = malloc (strlen (directory) + strlen ("/mime/mime.cache") + 1);
+  strcpy (file_name, directory); strcat (file_name, "/mime/mime.cache");
+  invalid = xdg_check_file (file_name, &exists);
+  free (file_name);
+  if (invalid)
+    {
+      *invalid_dir_list = TRUE;
+      return TRUE;
+    }
+  else if (exists)
+    {
+      return FALSE;
+    }
+
+  /* Check the globs file */
+  file_name = malloc (strlen (directory) + strlen ("/mime/globs") + 1);
+  strcpy (file_name, directory); strcat (file_name, "/mime/globs");
+  invalid = xdg_check_file (file_name, NULL);
+  free (file_name);
+  if (invalid)
+    {
+      *invalid_dir_list = TRUE;
+      return TRUE;
+    }
+
+  /* Check the magic file */
+  file_name = malloc (strlen (directory) + strlen ("/mime/magic") + 1);
+  strcpy (file_name, directory); strcat (file_name, "/mime/magic");
+  invalid = xdg_check_file (file_name, NULL);
+  free (file_name);
+  if (invalid)
+    {
+      *invalid_dir_list = TRUE;
+      return TRUE;
+    }
+
+  return FALSE; /* Keep processing */
+}
+
+/* Walks through all the mime files stat()ing them to see if they've changed.
+ * Returns TRUE if they have. */
+static int
+xdg_check_dirs (void)
+{
+  XdgDirTimeList *list;
+  int invalid_dir_list = FALSE;
+
+  for (list = dir_time_list; list; list = list->next)
+    list->checked = XDG_CHECKED_UNCHECKED;
+
+  xdg_run_command_on_dirs ((XdgDirectoryFunc) xdg_check_dir,
+                          &invalid_dir_list);
+
+  if (invalid_dir_list)
+    return TRUE;
+
+  for (list = dir_time_list; list; list = list->next)
+    {
+      if (list->checked != XDG_CHECKED_VALID)
+       return TRUE;
+    }
+
+  return FALSE;
+}
+
+/* We want to avoid stat()ing on every single mime call, so we only look for
+ * newer files every 5 seconds.  This will return TRUE if we need to reread the
+ * mime data from disk.
+ */
+static int
+xdg_check_time_and_dirs (void)
+{
+  struct timeval tv;
+  time_t current_time;
+  int retval = FALSE;
+
+  gettimeofday (&tv, NULL);
+  current_time = tv.tv_sec;
+
+  if (current_time >= last_stat_time + 5)
+    {
+      retval = xdg_check_dirs ();
+      last_stat_time = current_time;
+    }
+
+  return retval;
+}
+
+/* Called in every public function.  It reloads the hash function if need be.
+ */
+static void
+xdg_mime_init (void)
+{
+  if (xdg_check_time_and_dirs ())
+    {
+      xdg_mime_shutdown ();
+    }
+
+  if (need_reread)
+    {
+      global_hash = _xdg_glob_hash_new ();
+      global_magic = _xdg_mime_magic_new ();
+      alias_list = _xdg_mime_alias_list_new ();
+      parent_list = _xdg_mime_parent_list_new ();
+      icon_list = _xdg_mime_icon_list_new ();
+      generic_icon_list = _xdg_mime_icon_list_new ();
+
+      xdg_run_command_on_dirs ((XdgDirectoryFunc) xdg_mime_init_from_directory,
+                              NULL);
+
+      need_reread = FALSE;
+    }
+}
+
+API const char *
+xdg_mime_get_mime_type_for_data (const void *data,
+                                size_t      len,
+                                int        *result_prio)
+{
+  const char *mime_type;
+
+  xdg_mime_init ();
+
+  if (_caches)
+    return _xdg_mime_cache_get_mime_type_for_data (data, len, result_prio);
+
+  mime_type = _xdg_mime_magic_lookup_data (global_magic, data, len, result_prio, NULL, 0);
+
+  if (mime_type)
+    return mime_type;
+
+  return XDG_MIME_TYPE_UNKNOWN;
+}
+
+API const char *
+xdg_mime_get_mime_type_for_file (const char  *file_name,
+                                 struct stat *statbuf)
+{
+  const char *mime_type;
+  /* currently, only a few globs occur twice, and none
+   * more often, so 5 seems plenty.
+   */
+  const char *mime_types[5];
+  FILE *file;
+  unsigned char *data;
+  int max_extent;
+  int bytes_read;
+  struct stat buf;
+  const char *base_name;
+  int n;
+
+  if (file_name == NULL)
+    return NULL;
+  if (! _xdg_utf8_validate (file_name))
+    return NULL;
+
+  xdg_mime_init ();
+
+  if (_caches)
+    return _xdg_mime_cache_get_mime_type_for_file (file_name, statbuf);
+
+  base_name = _xdg_get_base_name (file_name);
+  n = _xdg_glob_hash_lookup_file_name (global_hash, base_name, mime_types, 5);
+
+  if (n == 1)
+    return mime_types[0];
+
+  if (!statbuf)
+    {
+      if (stat (file_name, &buf) != 0)
+       return XDG_MIME_TYPE_UNKNOWN;
+
+      statbuf = &buf;
+    }
+
+  if (!S_ISREG (statbuf->st_mode))
+    return XDG_MIME_TYPE_UNKNOWN;
+
+  /* FIXME: Need to make sure that max_extent isn't totally broken.  This could
+   * be large and need getting from a stream instead of just reading it all
+   * in. */
+  max_extent = _xdg_mime_magic_get_buffer_extents (global_magic);
+  data = malloc (max_extent);
+  if (data == NULL)
+    return XDG_MIME_TYPE_UNKNOWN;
+        
+  file = fopen (file_name, "r");
+  if (file == NULL)
+    {
+      free (data);
+      return XDG_MIME_TYPE_UNKNOWN;
+    }
+
+  bytes_read = fread (data, 1, max_extent, file);
+  if (ferror (file))
+    {
+      free (data);
+      fclose (file);
+      return XDG_MIME_TYPE_UNKNOWN;
+    }
+
+  mime_type = _xdg_mime_magic_lookup_data (global_magic, data, bytes_read, NULL,
+                                          mime_types, n);
+
+  free (data);
+  fclose (file);
+
+  if (mime_type)
+    return mime_type;
+
+  return XDG_MIME_TYPE_UNKNOWN;
+}
+
+API const char *
+xdg_mime_get_mime_type_from_file_name (const char *file_name)
+{
+  const char *mime_type;
+
+  xdg_mime_init ();
+
+  if (_caches)
+    return _xdg_mime_cache_get_mime_type_from_file_name (file_name);
+
+  if (_xdg_glob_hash_lookup_file_name (global_hash, file_name, &mime_type, 1))
+    return mime_type;
+  else
+    return XDG_MIME_TYPE_UNKNOWN;
+}
+
+int
+xdg_mime_get_mime_types_from_file_name (const char *file_name,
+                                       const char  *mime_types[],
+                                       int          n_mime_types)
+{
+  xdg_mime_init ();
+  
+  if (_caches)
+    return _xdg_mime_cache_get_mime_types_from_file_name (file_name, mime_types, n_mime_types);
+  
+  return _xdg_glob_hash_lookup_file_name (global_hash, file_name, mime_types, n_mime_types);
+}
+
+int
+xdg_mime_is_valid_mime_type (const char *mime_type)
+{
+  /* FIXME: We should make this a better test
+   */
+  return _xdg_utf8_validate (mime_type);
+}
+
+void
+xdg_mime_shutdown (void)
+{
+  XdgCallbackList *list;
+
+  /* FIXME: Need to make this (and the whole library) thread safe */
+  if (dir_time_list)
+    {
+      xdg_dir_time_list_free (dir_time_list);
+      dir_time_list = NULL;
+    }
+       
+  if (global_hash)
+    {
+      _xdg_glob_hash_free (global_hash);
+      global_hash = NULL;
+    }
+  if (global_magic)
+    {
+      _xdg_mime_magic_free (global_magic);
+      global_magic = NULL;
+    }
+
+  if (alias_list)
+    {
+      _xdg_mime_alias_list_free (alias_list);
+      alias_list = NULL;
+    }
+
+  if (parent_list)
+    {
+      _xdg_mime_parent_list_free (parent_list);
+      parent_list = NULL;
+    }
+
+  if (icon_list)
+    {
+      _xdg_mime_icon_list_free (icon_list);
+      icon_list = NULL;
+    }
+
+  if (generic_icon_list)
+    {
+      _xdg_mime_icon_list_free (generic_icon_list);
+      generic_icon_list = NULL;
+    }
+  
+  if (_caches)
+    {
+      int i;
+
+      for (i = 0; i < n_caches; i++)
+        _xdg_mime_cache_unref (_caches[i]);
+      free (_caches);
+      _caches = NULL;
+      n_caches = 0;
+    }
+
+  for (list = callback_list; list; list = list->next)
+    (list->callback) (list->data);
+
+  need_reread = TRUE;
+}
+
+int
+xdg_mime_get_max_buffer_extents (void)
+{
+  xdg_mime_init ();
+  
+  if (_caches)
+    return _xdg_mime_cache_get_max_buffer_extents ();
+
+  return _xdg_mime_magic_get_buffer_extents (global_magic);
+}
+
+const char *
+_xdg_mime_unalias_mime_type (const char *mime_type)
+{
+  const char *lookup;
+
+  if (_caches)
+    return _xdg_mime_cache_unalias_mime_type (mime_type);
+
+  if ((lookup = _xdg_mime_alias_list_lookup (alias_list, mime_type)) != NULL)
+    return lookup;
+
+  return mime_type;
+}
+
+API const char *
+xdg_mime_unalias_mime_type (const char *mime_type)
+{
+  xdg_mime_init ();
+
+  return _xdg_mime_unalias_mime_type (mime_type);
+}
+
+int
+_xdg_mime_mime_type_equal (const char *mime_a,
+                          const char *mime_b)
+{
+  const char *unalias_a, *unalias_b;
+
+  unalias_a = _xdg_mime_unalias_mime_type (mime_a);
+  unalias_b = _xdg_mime_unalias_mime_type (mime_b);
+
+  if (strcmp (unalias_a, unalias_b) == 0)
+    return 1;
+
+  return 0;
+}
+
+int
+xdg_mime_mime_type_equal (const char *mime_a,
+                         const char *mime_b)
+{
+  xdg_mime_init ();
+
+  return _xdg_mime_mime_type_equal (mime_a, mime_b);
+}
+
+int
+xdg_mime_media_type_equal (const char *mime_a,
+                          const char *mime_b)
+{
+  char *sep;
+
+  sep = strchr (mime_a, '/');
+  
+  if (sep && strncmp (mime_a, mime_b, sep - mime_a + 1) == 0)
+    return 1;
+
+  return 0;
+}
+
+#if 1
+static int
+xdg_mime_is_super_type (const char *mime)
+{
+  int length;
+  const char *type;
+
+  length = strlen (mime);
+  type = &(mime[length - 2]);
+
+  if (strcmp (type, "/*") == 0)
+    return 1;
+
+  return 0;
+}
+#endif
+
+int
+_xdg_mime_mime_type_subclass (const char *mime,
+                             const char *base)
+{
+  const char *umime, *ubase;
+  const char **parents;
+
+  if (_caches)
+    return _xdg_mime_cache_mime_type_subclass (mime, base);
+
+  umime = _xdg_mime_unalias_mime_type (mime);
+  ubase = _xdg_mime_unalias_mime_type (base);
+
+  if (strcmp (umime, ubase) == 0)
+    return 1;
+
+#if 1  
+  /* Handle supertypes */
+  if (xdg_mime_is_super_type (ubase) &&
+      xdg_mime_media_type_equal (umime, ubase))
+    return 1;
+#endif
+
+  /*  Handle special cases text/plain and application/octet-stream */
+  if (strcmp (ubase, "text/plain") == 0 && 
+      strncmp (umime, "text/", 5) == 0)
+    return 1;
+
+  if (strcmp (ubase, "application/octet-stream") == 0)
+    return 1;
+  
+  parents = _xdg_mime_parent_list_lookup (parent_list, umime);
+  for (; parents && *parents; parents++)
+    {
+      if (_xdg_mime_mime_type_subclass (*parents, ubase))
+       return 1;
+    }
+
+  return 0;
+}
+
+int
+xdg_mime_mime_type_subclass (const char *mime,
+                            const char *base)
+{
+  xdg_mime_init ();
+
+  return _xdg_mime_mime_type_subclass (mime, base);
+}
+
+char **
+xdg_mime_list_mime_parents (const char *mime)
+{
+  const char **parents;
+  char **result;
+  int i, n;
+
+  if (_caches)
+    return _xdg_mime_cache_list_mime_parents (mime);
+
+  parents = xdg_mime_get_mime_parents (mime);
+
+  if (!parents)
+    return NULL;
+
+  for (i = 0; parents[i]; i++) ;
+  
+  n = (i + 1) * sizeof (char *);
+  result = (char **) malloc (n);
+  memcpy (result, parents, n);
+
+  return result;
+}
+
+const char **
+xdg_mime_get_mime_parents (const char *mime)
+{
+  const char *umime;
+
+  xdg_mime_init ();
+
+  umime = _xdg_mime_unalias_mime_type (mime);
+
+  return _xdg_mime_parent_list_lookup (parent_list, umime);
+}
+
+void 
+xdg_mime_dump (void)
+{
+  xdg_mime_init();
+
+  printf ("*** ALIASES ***\n\n");
+  _xdg_mime_alias_list_dump (alias_list);
+  printf ("\n*** PARENTS ***\n\n");
+  _xdg_mime_parent_list_dump (parent_list);
+  printf ("\n*** CACHE ***\n\n");
+  _xdg_glob_hash_dump (global_hash);
+  printf ("\n*** GLOBS ***\n\n");
+  _xdg_glob_hash_dump (global_hash);
+  printf ("\n*** GLOBS REVERSE TREE ***\n\n");
+  _xdg_mime_cache_glob_dump ();
+}
+
+
+/* Registers a function to be called every time the mime database reloads its files
+ */
+int
+xdg_mime_register_reload_callback (XdgMimeCallback  callback,
+                                  void            *data,
+                                  XdgMimeDestroy   destroy)
+{
+  XdgCallbackList *list_el;
+  static int callback_id = 1;
+
+  /* Make a new list element */
+  list_el = calloc (1, sizeof (XdgCallbackList));
+  list_el->callback_id = callback_id;
+  list_el->callback = callback;
+  list_el->data = data;
+  list_el->destroy = destroy;
+  list_el->next = callback_list;
+  if (list_el->next)
+    list_el->next->prev = list_el;
+
+  callback_list = list_el;
+  callback_id ++;
+
+  return callback_id - 1;
+}
+
+void
+xdg_mime_remove_callback (int callback_id)
+{
+  XdgCallbackList *list;
+
+  for (list = callback_list; list; list = list->next)
+    {
+      if (list->callback_id == callback_id)
+       {
+         if (list->next)
+           list->next = list->prev;
+
+         if (list->prev)
+           list->prev->next = list->next;
+         else
+           callback_list = list->next;
+
+         /* invoke the destroy handler */
+         (list->destroy) (list->data);
+         free (list);
+         return;
+       }
+    }
+}
+
+API const char *
+xdg_mime_get_icon (const char *mime)
+{
+  xdg_mime_init ();
+  
+  if (_caches)
+    return _xdg_mime_cache_get_icon (mime);
+
+  return _xdg_mime_icon_list_lookup (icon_list, mime);
+}
+
+API const char *
+xdg_mime_get_generic_icon (const char *mime)
+{
+  xdg_mime_init ();
+  
+  if (_caches)
+    return _xdg_mime_cache_get_generic_icon (mime);
+
+  return _xdg_mime_icon_list_lookup (generic_icon_list, mime);
+}
diff --git a/xdgmime/src/xdgmime.h b/xdgmime/src/xdgmime.h
new file mode 100644 (file)
index 0000000..c62500e
--- /dev/null
@@ -0,0 +1,130 @@
+/* -*- mode: C; c-file-style: "gnu" -*- */
+/* xdgmime.h: XDG Mime Spec mime resolver.  Based on version 0.11 of the spec.
+ *
+ * More info can be found at http://www.freedesktop.org/standards/
+ * 
+ * Copyright (C) 2003  Red Hat, Inc.
+ * Copyright (C) 2003  Jonathan Blandford <jrb@alum.mit.edu>
+ *
+ * Licensed under the Academic Free License version 2.0
+ * Or under the following terms:
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+
+#ifndef __XDG_MIME_H__
+#define __XDG_MIME_H__
+
+#include <stdlib.h>
+#include <sys/stat.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+#ifdef XDG_PREFIX
+#define XDG_ENTRY(func) _XDG_ENTRY2(XDG_PREFIX,func)
+#define _XDG_ENTRY2(prefix,func) _XDG_ENTRY3(prefix,func)
+#define _XDG_ENTRY3(prefix,func) prefix##_##func
+
+#define XDG_RESERVED_ENTRY(func) _XDG_RESERVED_ENTRY2(XDG_PREFIX,func)
+#define _XDG_RESERVED_ENTRY2(prefix,func) _XDG_RESERVED_ENTRY3(prefix,func)
+#define _XDG_RESERVED_ENTRY3(prefix,func) _##prefix##_##func
+#endif
+
+typedef void (*XdgMimeCallback) (void *user_data);
+typedef void (*XdgMimeDestroy)  (void *user_data);
+
+  
+#ifdef XDG_PREFIX
+#define xdg_mime_get_mime_type_for_data       XDG_ENTRY(get_mime_type_for_data)
+#define xdg_mime_get_mime_type_for_file       XDG_ENTRY(get_mime_type_for_file)
+#define xdg_mime_get_mime_type_from_file_name XDG_ENTRY(get_mime_type_from_file_name)
+#define xdg_mime_get_mime_types_from_file_name XDG_ENTRY(get_mime_types_from_file_name)
+#define xdg_mime_is_valid_mime_type           XDG_ENTRY(is_valid_mime_type)
+#define xdg_mime_mime_type_equal              XDG_ENTRY(mime_type_equal)
+#define xdg_mime_media_type_equal             XDG_ENTRY(media_type_equal)
+#define xdg_mime_mime_type_subclass           XDG_ENTRY(mime_type_subclass)
+#define xdg_mime_get_mime_parents             XDG_ENTRY(get_mime_parents)
+#define xdg_mime_list_mime_parents            XDG_ENTRY(list_mime_parents)
+#define xdg_mime_unalias_mime_type            XDG_ENTRY(unalias_mime_type)
+#define xdg_mime_get_max_buffer_extents       XDG_ENTRY(get_max_buffer_extents)
+#define xdg_mime_shutdown                     XDG_ENTRY(shutdown)
+#define xdg_mime_dump                         XDG_ENTRY(dump)
+#define xdg_mime_register_reload_callback     XDG_ENTRY(register_reload_callback)
+#define xdg_mime_remove_callback              XDG_ENTRY(remove_callback)
+#define xdg_mime_type_unknown                 XDG_ENTRY(type_unknown)
+#define xdg_mime_get_icon                     XDG_ENTRY(get_icon)
+#define xdg_mime_get_generic_icon             XDG_ENTRY(get_generic_icon)
+
+#define _xdg_mime_mime_type_equal             XDG_RESERVED_ENTRY(mime_type_equal)
+#define _xdg_mime_mime_type_subclass          XDG_RESERVED_ENTRY(mime_type_subclass)
+#define _xdg_mime_unalias_mime_type           XDG_RESERVED_ENTRY(unalias_mime_type)  
+#endif
+
+extern const char xdg_mime_type_unknown[];
+#define XDG_MIME_TYPE_UNKNOWN xdg_mime_type_unknown
+
+const char  *xdg_mime_get_mime_type_for_data       (const void *data,
+                                                   size_t      len,
+                                                   int        *result_prio);
+const char  *xdg_mime_get_mime_type_for_file       (const char *file_name,
+                                                    struct stat *statbuf);
+const char  *xdg_mime_get_mime_type_from_file_name (const char *file_name);
+int          xdg_mime_get_mime_types_from_file_name(const char *file_name,
+                                                   const char *mime_types[],
+                                                   int         n_mime_types);
+int          xdg_mime_is_valid_mime_type           (const char *mime_type);
+int          xdg_mime_mime_type_equal              (const char *mime_a,
+                                                   const char *mime_b);
+int          xdg_mime_media_type_equal             (const char *mime_a,
+                                                   const char *mime_b);
+int          xdg_mime_mime_type_subclass           (const char *mime_a,
+                                                   const char *mime_b);
+  /* xdg_mime_get_mime_parents() is deprecated since it does
+   * not work correctly with caches. Use xdg_mime_list_parents() 
+   * instead, but notice that that function expects you to free
+   * the array it returns. 
+   */
+const char **xdg_mime_get_mime_parents            (const char *mime);
+char **      xdg_mime_list_mime_parents                   (const char *mime);
+const char  *xdg_mime_unalias_mime_type                   (const char *mime);
+const char  *xdg_mime_get_icon                     (const char *mime);
+const char  *xdg_mime_get_generic_icon             (const char *mime);
+int          xdg_mime_get_max_buffer_extents       (void);
+void         xdg_mime_shutdown                     (void);
+void         xdg_mime_dump                         (void);
+int          xdg_mime_register_reload_callback     (XdgMimeCallback  callback,
+                                                   void            *data,
+                                                   XdgMimeDestroy   destroy);
+void         xdg_mime_remove_callback              (int              callback_id);
+
+  /* added by Youmin Ha <youmin.ha@samsung.com> */
+const char **xdg_mime_get_file_names_from_mime_type(const char *mime_type);
+
+
+   /* Private versions of functions that don't call xdg_mime_init () */
+int          _xdg_mime_mime_type_equal             (const char *mime_a,
+                                                   const char *mime_b);
+int          _xdg_mime_mime_type_subclass          (const char *mime,
+                                                   const char *base);
+const char  *_xdg_mime_unalias_mime_type           (const char *mime);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+#endif /* __XDG_MIME_H__ */
diff --git a/xdgmime/src/xdgmimealias.c b/xdgmime/src/xdgmimealias.c
new file mode 100644 (file)
index 0000000..07d89eb
--- /dev/null
@@ -0,0 +1,184 @@
+/* -*- mode: C; c-file-style: "gnu" -*- */
+/* xdgmimealias.c: Private file.  Datastructure for storing the aliases.
+ *
+ * More info can be found at http://www.freedesktop.org/standards/
+ *
+ * Copyright (C) 2004  Red Hat, Inc.
+ * Copyright (C) 2004  Matthias Clasen <mclasen@redhat.com>
+ *
+ * Licensed under the Academic Free License version 2.0
+ * Or under the following terms:
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "xdgmimealias.h"
+#include "xdgmimeint.h"
+#include <stdlib.h>
+#include <stdio.h>
+#include <assert.h>
+#include <string.h>
+#include <fnmatch.h>
+
+#ifndef        FALSE
+#define        FALSE   (0)
+#endif
+
+#ifndef        TRUE
+#define        TRUE    (!FALSE)
+#endif
+
+typedef struct XdgAlias XdgAlias;
+
+struct XdgAlias 
+{
+  char *alias;
+  char *mime_type;
+};
+
+struct XdgAliasList
+{
+  struct XdgAlias *aliases;
+  int n_aliases;
+};
+
+XdgAliasList *
+_xdg_mime_alias_list_new (void)
+{
+  XdgAliasList *list;
+
+  list = malloc (sizeof (XdgAliasList));
+
+  list->aliases = NULL;
+  list->n_aliases = 0;
+
+  return list;
+}
+
+void         
+_xdg_mime_alias_list_free (XdgAliasList *list)
+{
+  int i;
+
+  if (list->aliases)
+    {
+      for (i = 0; i < list->n_aliases; i++)
+       {
+         free (list->aliases[i].alias);
+         free (list->aliases[i].mime_type);
+       }
+      free (list->aliases);
+    }
+  free (list);
+}
+
+static int
+alias_entry_cmp (const void *v1, const void *v2)
+{
+  return strcmp (((XdgAlias *)v1)->alias, ((XdgAlias *)v2)->alias);
+}
+
+const char  *
+_xdg_mime_alias_list_lookup (XdgAliasList *list,
+                            const char   *alias)
+{
+  XdgAlias *entry;
+  XdgAlias key;
+
+  if (list->n_aliases > 0)
+    {
+      key.alias = (char *)alias;
+      key.mime_type = NULL;
+
+      entry = bsearch (&key, list->aliases, list->n_aliases,
+                      sizeof (XdgAlias), alias_entry_cmp);
+      if (entry)
+        return entry->mime_type;
+    }
+
+  return NULL;
+}
+
+void
+_xdg_mime_alias_read_from_file (XdgAliasList *list,
+                               const char   *file_name)
+{
+  FILE *file;
+  char line[255];
+  int alloc;
+
+  file = fopen (file_name, "r");
+
+  if (file == NULL)
+    return;
+
+  /* FIXME: Not UTF-8 safe.  Doesn't work if lines are greater than 255 chars.
+   * Blah */
+  alloc = list->n_aliases + 16;
+  list->aliases = realloc (list->aliases, alloc * sizeof (XdgAlias));
+  while (fgets (line, 255, file) != NULL)
+    {
+      char *sep;
+      if (line[0] == '#')
+       continue;
+
+      sep = strchr (line, ' ');
+      if (sep == NULL)
+       continue;
+      *(sep++) = '\000';
+      sep[strlen (sep) -1] = '\000';
+      if (list->n_aliases == alloc)
+       {
+         alloc <<= 1;
+         list->aliases = realloc (list->aliases, 
+                                  alloc * sizeof (XdgAlias));
+       }
+      list->aliases[list->n_aliases].alias = strdup (line);
+      list->aliases[list->n_aliases].mime_type = strdup (sep);
+      list->n_aliases++;
+    }
+  list->aliases = realloc (list->aliases, 
+                          list->n_aliases * sizeof (XdgAlias));
+
+  fclose (file);  
+  
+  if (list->n_aliases > 1)
+    qsort (list->aliases, list->n_aliases, 
+           sizeof (XdgAlias), alias_entry_cmp);
+}
+
+
+void
+_xdg_mime_alias_list_dump (XdgAliasList *list)
+{
+  int i;
+
+  if (list->aliases)
+    {
+      for (i = 0; i < list->n_aliases; i++)
+       {
+         printf ("%s %s\n", 
+                 list->aliases[i].alias,
+                 list->aliases[i].mime_type);
+       }
+    }
+}
+
+
diff --git a/xdgmime/src/xdgmimealias.h b/xdgmime/src/xdgmimealias.h
new file mode 100644 (file)
index 0000000..3c28012
--- /dev/null
@@ -0,0 +1,51 @@
+/* -*- mode: C; c-file-style: "gnu" -*- */
+/* xdgmimealias.h: Private file.  Datastructure for storing the aliases.
+ *
+ * More info can be found at http://www.freedesktop.org/standards/
+ *
+ * Copyright (C) 2004  Red Hat, Inc.
+ * Copyright (C) 200  Matthias Clasen <mclasen@redhat.com>
+ *
+ * Licensed under the Academic Free License version 2.0
+ * Or under the following terms:
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __XDG_MIME_ALIAS_H__
+#define __XDG_MIME_ALIAS_H__
+
+#include "xdgmime.h"
+
+typedef struct XdgAliasList XdgAliasList;
+
+#ifdef XDG_PREFIX
+#define _xdg_mime_alias_read_from_file        XDG_RESERVED_ENTRY(alias_read_from_file)
+#define _xdg_mime_alias_list_new              XDG_RESERVED_ENTRY(alias_list_new)
+#define _xdg_mime_alias_list_free             XDG_RESERVED_ENTRY(alias_list_free)
+#define _xdg_mime_alias_list_lookup           XDG_RESERVED_ENTRY(alias_list_lookup)
+#define _xdg_mime_alias_list_dump             XDG_RESERVED_ENTRY(alias_list_dump)
+#endif
+
+void          _xdg_mime_alias_read_from_file (XdgAliasList *list,
+                                             const char   *file_name);
+XdgAliasList *_xdg_mime_alias_list_new       (void);
+void          _xdg_mime_alias_list_free      (XdgAliasList *list);
+const char   *_xdg_mime_alias_list_lookup    (XdgAliasList *list,
+                                             const char  *alias);
+void          _xdg_mime_alias_list_dump      (XdgAliasList *list);
+
+#endif /* __XDG_MIME_ALIAS_H__ */
diff --git a/xdgmime/src/xdgmimecache.c b/xdgmime/src/xdgmimecache.c
new file mode 100644 (file)
index 0000000..1e99b3e
--- /dev/null
@@ -0,0 +1,1054 @@
+/* -*- mode: C; c-file-style: "gnu" -*- */
+/* xdgmimealias.c: Private file.  mmappable caches for mime data
+ *
+ * More info can be found at http://www.freedesktop.org/standards/
+ *
+ * Copyright (C) 2005  Matthias Clasen <mclasen@redhat.com>
+ *
+ * Licensed under the Academic Free License version 2.0
+ * Or under the following terms:
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <fcntl.h>
+#include <unistd.h>
+#include <fnmatch.h>
+#include <assert.h>
+
+#include <netinet/in.h> /* for ntohl/ntohs */
+
+#ifdef HAVE_MMAP
+#include <sys/mman.h>
+#else
+#warning Building xdgmime without MMAP support. Binary "mime.info" cache files will not be used.
+#endif
+
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include "xdgmimecache.h"
+#include "xdgmimeint.h"
+
+#ifndef MAX
+#define MAX(a,b) ((a) > (b) ? (a) : (b))
+#endif
+
+#ifndef        FALSE
+#define        FALSE   (0)
+#endif
+
+#ifndef        TRUE
+#define        TRUE    (!FALSE)
+#endif
+
+#ifndef _O_BINARY
+#define _O_BINARY 0
+#endif
+
+#ifndef MAP_FAILED
+#define MAP_FAILED ((void *) -1)
+#endif
+
+#define MAJOR_VERSION 1
+#define MINOR_VERSION_MIN 1
+#define MINOR_VERSION_MAX 2
+
+struct _XdgMimeCache
+{
+  int ref_count;
+  int minor;
+
+  size_t  size;
+  char   *buffer;
+};
+
+#define GET_UINT16(cache,offset) (ntohs(*(xdg_uint16_t*)((cache) + (offset))))
+#define GET_UINT32(cache,offset) (ntohl(*(xdg_uint32_t*)((cache) + (offset))))
+
+XdgMimeCache *
+_xdg_mime_cache_ref (XdgMimeCache *cache)
+{
+  cache->ref_count++;
+  return cache;
+}
+
+void
+_xdg_mime_cache_unref (XdgMimeCache *cache)
+{
+  cache->ref_count--;
+
+  if (cache->ref_count == 0)
+    {
+#ifdef HAVE_MMAP
+      munmap (cache->buffer, cache->size);
+#endif
+      free (cache);
+    }
+}
+
+XdgMimeCache *
+_xdg_mime_cache_new_from_file (const char *file_name)
+{
+  XdgMimeCache *cache = NULL;
+
+#ifdef HAVE_MMAP
+  int fd = -1;
+  struct stat st;
+  char *buffer = NULL;
+  int minor;
+
+  /* Open the file and map it into memory */
+  fd = open (file_name, O_RDONLY|_O_BINARY, 0);
+
+  if (fd < 0)
+    return NULL;
+  
+  if (fstat (fd, &st) < 0 || st.st_size < 4)
+    goto done;
+
+  buffer = (char *) mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
+
+  if (buffer == MAP_FAILED)
+    goto done;
+
+  minor = GET_UINT16 (buffer, 2);
+  /* Verify version */
+  if (GET_UINT16 (buffer, 0) != MAJOR_VERSION ||
+      (minor < MINOR_VERSION_MIN ||
+       minor > MINOR_VERSION_MAX))
+    {
+      munmap (buffer, st.st_size);
+
+      goto done;
+    }
+  
+  cache = (XdgMimeCache *) malloc (sizeof (XdgMimeCache));
+  cache->minor = minor;
+  cache->ref_count = 1;
+  cache->buffer = buffer;
+  cache->size = st.st_size;
+
+ done:
+  if (fd != -1)
+    close (fd);
+
+#endif  /* HAVE_MMAP */
+
+  return cache;
+}
+
+static int
+cache_magic_matchlet_compare_to_data (XdgMimeCache *cache, 
+                                     xdg_uint32_t  offset,
+                                     const void   *data,
+                                     size_t        len)
+{
+  xdg_uint32_t range_start = GET_UINT32 (cache->buffer, offset);
+  xdg_uint32_t range_length = GET_UINT32 (cache->buffer, offset + 4);
+  xdg_uint32_t data_length = GET_UINT32 (cache->buffer, offset + 12);
+  xdg_uint32_t data_offset = GET_UINT32 (cache->buffer, offset + 16);
+  xdg_uint32_t mask_offset = GET_UINT32 (cache->buffer, offset + 20);
+  
+  int i, j;
+
+  for (i = range_start; i < range_start + range_length; i++)
+    {
+      int valid_matchlet = TRUE;
+      
+      if (i + data_length > len)
+       return FALSE;
+
+      if (mask_offset)
+       {
+         for (j = 0; j < data_length; j++)
+           {
+             if ((((unsigned char *)cache->buffer)[data_offset + j] & ((unsigned char *)cache->buffer)[mask_offset + j]) !=
+                 ((((unsigned char *) data)[j + i]) & ((unsigned char *)cache->buffer)[mask_offset + j]))
+               {
+                 valid_matchlet = FALSE;
+                 break;
+               }
+           }
+       }
+      else
+       {
+         for (j = 0; j < data_length; j++)
+           {
+             if (((unsigned char *)cache->buffer)[data_offset + j] != ((unsigned char *) data)[j + i])
+               {
+                 valid_matchlet = FALSE;
+                 break;
+               }
+           }
+       }
+      
+      if (valid_matchlet)
+       return TRUE;
+    }
+  
+  return FALSE;  
+}
+
+static int
+cache_magic_matchlet_compare (XdgMimeCache *cache, 
+                             xdg_uint32_t  offset,
+                             const void   *data,
+                             size_t        len)
+{
+  xdg_uint32_t n_children = GET_UINT32 (cache->buffer, offset + 24);
+  xdg_uint32_t child_offset = GET_UINT32 (cache->buffer, offset + 28);
+
+  int i;
+  
+  if (cache_magic_matchlet_compare_to_data (cache, offset, data, len))
+    {
+      if (n_children == 0)
+       return TRUE;
+      
+      for (i = 0; i < n_children; i++)
+       {
+         if (cache_magic_matchlet_compare (cache, child_offset + 32 * i,
+                                           data, len))
+           return TRUE;
+       }
+    }
+  
+  return FALSE;  
+}
+
+static const char *
+cache_magic_compare_to_data (XdgMimeCache *cache, 
+                            xdg_uint32_t  offset,
+                            const void   *data, 
+                            size_t        len, 
+                            int          *prio)
+{
+  xdg_uint32_t priority = GET_UINT32 (cache->buffer, offset);
+  xdg_uint32_t mimetype_offset = GET_UINT32 (cache->buffer, offset + 4);
+  xdg_uint32_t n_matchlets = GET_UINT32 (cache->buffer, offset + 8);
+  xdg_uint32_t matchlet_offset = GET_UINT32 (cache->buffer, offset + 12);
+
+  int i;
+
+  for (i = 0; i < n_matchlets; i++)
+    {
+      if (cache_magic_matchlet_compare (cache, matchlet_offset + i * 32, 
+                                       data, len))
+       {
+         *prio = priority;
+         
+         return cache->buffer + mimetype_offset;
+       }
+    }
+
+  return NULL;
+}
+
+static const char *
+cache_magic_lookup_data (XdgMimeCache *cache, 
+                        const void   *data, 
+                        size_t        len, 
+                        int          *prio,
+                        const char   *mime_types[],
+                        int           n_mime_types)
+{
+  xdg_uint32_t list_offset;
+  xdg_uint32_t n_entries;
+  xdg_uint32_t offset;
+
+  int j, n;
+
+  *prio = 0;
+
+  list_offset = GET_UINT32 (cache->buffer, 24);
+  n_entries = GET_UINT32 (cache->buffer, list_offset);
+  offset = GET_UINT32 (cache->buffer, list_offset + 8);
+  
+  for (j = 0; j < n_entries; j++)
+    {
+      const char *match;
+
+      match = cache_magic_compare_to_data (cache, offset + 16 * j, 
+                                          data, len, prio);
+      if (match)
+       return match;
+      else
+       {
+         xdg_uint32_t mimetype_offset;
+         const char *non_match;
+         
+         mimetype_offset = GET_UINT32 (cache->buffer, offset + 16 * j + 4);
+         non_match = cache->buffer + mimetype_offset;
+
+         for (n = 0; n < n_mime_types; n++)
+           {
+             if (mime_types[n] && 
+                 _xdg_mime_mime_type_equal (mime_types[n], non_match))
+               mime_types[n] = NULL;
+           }
+       }
+    }
+
+  return NULL;
+}
+
+static const char *
+cache_alias_lookup (const char *alias)
+{
+  const char *ptr;
+  int i, min, max, mid, cmp;
+
+  for (i = 0; _caches[i]; i++)
+    {
+      XdgMimeCache *cache = _caches[i];
+      xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 4);
+      xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
+      xdg_uint32_t offset;
+
+      min = 0; 
+      max = n_entries - 1;
+      while (max >= min) 
+       {
+         mid = (min + max) / 2;
+
+         offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * mid);
+         ptr = cache->buffer + offset;
+         cmp = strcmp (ptr, alias);
+         
+         if (cmp < 0)
+           min = mid + 1;
+         else if (cmp > 0)
+           max = mid - 1;
+         else
+           {
+             offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * mid + 4);
+             return cache->buffer + offset;
+           }
+       }
+    }
+
+  return NULL;
+}
+
+typedef struct {
+  const char *mime;
+  int weight;
+} MimeWeight;
+
+static int
+cache_glob_lookup_literal (const char *file_name,
+                          const char *mime_types[],
+                          int         n_mime_types,
+                          int         case_sensitive_check)
+{
+  const char *ptr;
+  int i, min, max, mid, cmp;
+
+  for (i = 0; _caches[i]; i++)
+    {
+      XdgMimeCache *cache = _caches[i];
+      xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 12);
+      xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
+      xdg_uint32_t offset;
+
+      min = 0; 
+      max = n_entries - 1;
+      while (max >= min) 
+       {
+         mid = (min + max) / 2;
+
+         offset = GET_UINT32 (cache->buffer, list_offset + 4 + 12 * mid);
+         ptr = cache->buffer + offset;
+         cmp = strcmp (ptr, file_name);
+
+         if (cmp < 0)
+           min = mid + 1;
+         else if (cmp > 0)
+           max = mid - 1;
+         else
+           {
+             int weight = GET_UINT32 (cache->buffer, list_offset + 4 + 12 * mid + 8);
+             int case_sensitive = weight & 0x100;
+             weight = weight & 0xff;
+
+             if (case_sensitive_check || !case_sensitive)
+               {
+                 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 12 * mid + 4);
+                 mime_types[0] = (const char *)(cache->buffer + offset);
+
+                 return 1;
+               }
+             return 0;
+           }
+       }
+    }
+
+  return 0;
+}
+
+static int
+cache_glob_lookup_fnmatch (const char *file_name,
+                          MimeWeight  mime_types[],
+                          int         n_mime_types)
+{
+  const char *mime_type;
+  const char *ptr;
+
+  int i, j, n;
+
+  n = 0;
+  for (i = 0; _caches[i]; i++)
+    {
+      XdgMimeCache *cache = _caches[i];
+
+      xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 20);
+      xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
+
+      for (j = 0; j < n_entries && n < n_mime_types; j++)
+       {
+         xdg_uint32_t offset = GET_UINT32 (cache->buffer, list_offset + 4 + 12 * j);
+         xdg_uint32_t mimetype_offset = GET_UINT32 (cache->buffer, list_offset + 4 + 12 * j + 4);
+         int weight = GET_UINT32 (cache->buffer, list_offset + 4 + 12 * j + 8);
+         weight = weight & 0xff;
+         ptr = cache->buffer + offset;
+         mime_type = cache->buffer + mimetype_offset;
+
+         /* FIXME: Not UTF-8 safe */
+         if (fnmatch (ptr, file_name, 0) == 0)
+           {
+             mime_types[n].mime = mime_type;
+             mime_types[n].weight = weight;
+             n++;
+           }
+       }
+
+      if (n > 0)
+       return n;
+    }
+  
+  return 0;
+}
+
+static int
+cache_glob_node_lookup_suffix (XdgMimeCache  *cache,
+                              xdg_uint32_t   n_entries,
+                              xdg_uint32_t   offset,
+                              const char    *file_name,
+                              int            len,
+                              int            case_sensitive_check,
+                              MimeWeight     mime_types[],
+                              int            n_mime_types)
+{
+  xdg_unichar_t character;
+  xdg_unichar_t match_char;
+  xdg_uint32_t mimetype_offset;
+  xdg_uint32_t n_children;
+  xdg_uint32_t child_offset; 
+  int weight;
+  int case_sensitive;
+
+  int min, max, mid, n, i;
+
+  character = file_name[len - 1];
+
+  assert (character != 0);
+
+  min = 0;
+  max = n_entries - 1;
+  while (max >= min)
+    {
+      mid = (min + max) /  2;
+      match_char = GET_UINT32 (cache->buffer, offset + 12 * mid);
+      if (match_char < character)
+       min = mid + 1;
+      else if (match_char > character)
+       max = mid - 1;
+      else 
+       {
+          len--;
+          n = 0;
+          n_children = GET_UINT32 (cache->buffer, offset + 12 * mid + 4);
+          child_offset = GET_UINT32 (cache->buffer, offset + 12 * mid + 8);
+      
+          if (len > 0)
+            {
+              n = cache_glob_node_lookup_suffix (cache, 
+                                                 n_children, child_offset,
+                                                 file_name, len, 
+                                                 case_sensitive_check,
+                                                 mime_types,
+                                                 n_mime_types);
+            }
+          if (n == 0)
+            {
+             i = 0;
+             while (n < n_mime_types && i < n_children)
+               {
+                 match_char = GET_UINT32 (cache->buffer, child_offset + 12 * i);
+                 if (match_char != 0)
+                   break;
+
+                 mimetype_offset = GET_UINT32 (cache->buffer, child_offset + 12 * i + 4);
+                 weight = GET_UINT32 (cache->buffer, child_offset + 12 * i + 8);
+                 case_sensitive = weight & 0x100;
+                 weight = weight & 0xff;
+
+                 if (case_sensitive_check || !case_sensitive)
+                   {
+                     mime_types[n].mime = cache->buffer + mimetype_offset;
+                     mime_types[n].weight = weight;
+                     n++;
+                   }
+                 i++;
+               }
+           }
+         return n;
+       }
+    }
+  return 0;
+}
+
+static int
+cache_glob_lookup_suffix (const char *file_name,
+                         int         len,
+                         int         ignore_case,
+                         MimeWeight  mime_types[],
+                         int         n_mime_types)
+{
+  int i, n;
+
+  for (i = 0; _caches[i]; i++)
+    {
+      XdgMimeCache *cache = _caches[i];
+
+      xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 16);
+      xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
+      xdg_uint32_t offset = GET_UINT32 (cache->buffer, list_offset + 4);
+
+      n = cache_glob_node_lookup_suffix (cache, 
+                                        n_entries, offset, 
+                                        file_name, len,
+                                        ignore_case,
+                                        mime_types,
+                                        n_mime_types);
+      if (n > 0)
+       return n;
+    }
+
+  return 0;
+}
+
+static int compare_mime_weight (const void *a, const void *b)
+{
+  const MimeWeight *aa = (const MimeWeight *)a;
+  const MimeWeight *bb = (const MimeWeight *)b;
+
+  return bb->weight - aa->weight;
+}
+
+#define ISUPPER(c)             ((c) >= 'A' && (c) <= 'Z')
+static char *
+ascii_tolower (const char *str)
+{
+  char *p, *lower;
+
+  lower = strdup (str);
+  p = lower;
+  while (*p != 0)
+    {
+      char c = *p;
+      *p++ = ISUPPER (c) ? c - 'A' + 'a' : c;
+    }
+  return lower;
+}
+
+static int
+cache_glob_lookup_file_name (const char *file_name, 
+                            const char *mime_types[],
+                            int         n_mime_types)
+{
+  int n;
+  MimeWeight mimes[10];
+  int n_mimes = 10;
+  int i;
+  int len;
+  char *lower_case;
+
+  assert (file_name != NULL && n_mime_types > 0);
+
+  /* First, check the literals */
+
+  lower_case = ascii_tolower (file_name);
+
+  n = cache_glob_lookup_literal (lower_case, mime_types, n_mime_types, FALSE);
+  if (n > 0)
+    {
+      free (lower_case);
+      return n;
+    }
+
+  n = cache_glob_lookup_literal (file_name, mime_types, n_mime_types, TRUE);
+  if (n > 0)
+    {
+      free (lower_case);
+      return n;
+    }
+
+  len = strlen (file_name);
+  n = cache_glob_lookup_suffix (lower_case, len, FALSE, mimes, n_mimes);
+  if (n == 0)
+    n = cache_glob_lookup_suffix (file_name, len, TRUE, mimes, n_mimes);
+
+  free (lower_case);
+
+  /* Last, try fnmatch */
+  if (n == 0)
+    n = cache_glob_lookup_fnmatch (file_name, mimes, n_mimes);
+
+  qsort (mimes, n, sizeof (MimeWeight), compare_mime_weight);
+
+  if (n_mime_types < n)
+    n = n_mime_types;
+
+  for (i = 0; i < n; i++)
+    mime_types[i] = mimes[i].mime;
+
+  return n;
+}
+
+int
+_xdg_mime_cache_get_max_buffer_extents (void)
+{
+  xdg_uint32_t offset;
+  xdg_uint32_t max_extent;
+  int i;
+
+  max_extent = 0;
+  for (i = 0; _caches[i]; i++)
+    {
+      XdgMimeCache *cache = _caches[i];
+
+      offset = GET_UINT32 (cache->buffer, 24);
+      max_extent = MAX (max_extent, GET_UINT32 (cache->buffer, offset + 4));
+    }
+
+  return max_extent;
+}
+
+static const char *
+cache_get_mime_type_for_data (const void *data,
+                             size_t      len,
+                             int        *result_prio,
+                             const char *mime_types[],
+                             int         n_mime_types)
+{
+  const char *mime_type;
+  int i, n, priority;
+
+  priority = 0;
+  mime_type = NULL;
+  for (i = 0; _caches[i]; i++)
+    {
+      XdgMimeCache *cache = _caches[i];
+
+      int prio;
+      const char *match;
+
+      match = cache_magic_lookup_data (cache, data, len, &prio, 
+                                      mime_types, n_mime_types);
+      if (prio > priority)
+       {
+         priority = prio;
+         mime_type = match;
+       }
+    }
+
+  if (result_prio)
+    *result_prio = priority;
+  
+  if (priority > 0)
+    return mime_type;
+
+  for (n = 0; n < n_mime_types; n++)
+    {
+      
+      if (mime_types[n])
+       return mime_types[n];
+    }
+
+  return XDG_MIME_TYPE_UNKNOWN;
+}
+
+const char *
+_xdg_mime_cache_get_mime_type_for_data (const void *data,
+                                       size_t      len,
+                                       int        *result_prio)
+{
+  return cache_get_mime_type_for_data (data, len, result_prio, NULL, 0);
+}
+
+const char *
+_xdg_mime_cache_get_mime_type_for_file (const char  *file_name,
+                                       struct stat *statbuf)
+{
+  const char *mime_type;
+  const char *mime_types[10];
+  FILE *file;
+  unsigned char *data;
+  int max_extent;
+  int bytes_read;
+  struct stat buf;
+  const char *base_name;
+  int n;
+
+  if (file_name == NULL)
+    return NULL;
+
+  if (! _xdg_utf8_validate (file_name))
+    return NULL;
+
+  base_name = _xdg_get_base_name (file_name);
+  n = cache_glob_lookup_file_name (base_name, mime_types, 10);
+
+  if (n == 1)
+    return mime_types[0];
+
+  if (!statbuf)
+    {
+      if (stat (file_name, &buf) != 0)
+       return XDG_MIME_TYPE_UNKNOWN;
+
+      statbuf = &buf;
+    }
+
+  if (!S_ISREG (statbuf->st_mode))
+    return XDG_MIME_TYPE_UNKNOWN;
+
+  /* FIXME: Need to make sure that max_extent isn't totally broken.  This could
+   * be large and need getting from a stream instead of just reading it all
+   * in. */
+  max_extent = _xdg_mime_cache_get_max_buffer_extents ();
+  data = malloc (max_extent);
+  if (data == NULL)
+    return XDG_MIME_TYPE_UNKNOWN;
+        
+  file = fopen (file_name, "r");
+  if (file == NULL)
+    {
+      free (data);
+      return XDG_MIME_TYPE_UNKNOWN;
+    }
+
+  bytes_read = fread (data, 1, max_extent, file);
+  if (ferror (file))
+    {
+      free (data);
+      fclose (file);
+      return XDG_MIME_TYPE_UNKNOWN;
+    }
+
+  mime_type = cache_get_mime_type_for_data (data, bytes_read, NULL,
+                                           mime_types, n);
+
+  free (data);
+  fclose (file);
+
+  return mime_type;
+}
+
+const char *
+_xdg_mime_cache_get_mime_type_from_file_name (const char *file_name)
+{
+  const char *mime_type;
+
+  if (cache_glob_lookup_file_name (file_name, &mime_type, 1))
+    return mime_type;
+  else
+    return XDG_MIME_TYPE_UNKNOWN;
+}
+
+int
+_xdg_mime_cache_get_mime_types_from_file_name (const char *file_name,
+                                              const char  *mime_types[],
+                                              int          n_mime_types)
+{
+  return cache_glob_lookup_file_name (file_name, mime_types, n_mime_types);
+}
+
+#if 1
+static int
+is_super_type (const char *mime)
+{
+  int length;
+  const char *type;
+
+  length = strlen (mime);
+  type = &(mime[length - 2]);
+
+  if (strcmp (type, "/*") == 0)
+    return 1;
+
+  return 0;
+}
+#endif
+
+int
+_xdg_mime_cache_mime_type_subclass (const char *mime,
+                                   const char *base)
+{
+  const char *umime, *ubase;
+
+  int i, j, min, max, med, cmp;
+  
+  umime = _xdg_mime_cache_unalias_mime_type (mime);
+  ubase = _xdg_mime_cache_unalias_mime_type (base);
+
+  if (strcmp (umime, ubase) == 0)
+    return 1;
+
+  /* We really want to handle text/ * in GtkFileFilter, so we just
+   * turn on the supertype matching
+   */
+#if 1
+  /* Handle supertypes */
+  if (is_super_type (ubase) &&
+      xdg_mime_media_type_equal (umime, ubase))
+    return 1;
+#endif
+
+  /*  Handle special cases text/plain and application/octet-stream */
+  if (strcmp (ubase, "text/plain") == 0 && 
+      strncmp (umime, "text/", 5) == 0)
+    return 1;
+
+  if (strcmp (ubase, "application/octet-stream") == 0)
+    return 1;
+  for (i = 0; _caches[i]; i++)
+    {
+      XdgMimeCache *cache = _caches[i];
+      
+      xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 8);
+      xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
+      xdg_uint32_t offset, n_parents, parent_offset;
+
+      min = 0; 
+      max = n_entries - 1;
+      while (max >= min)
+       {
+         med = (min + max)/2;
+         
+         offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * med);
+         cmp = strcmp (cache->buffer + offset, umime);
+         if (cmp < 0)
+           min = med + 1;
+         else if (cmp > 0)
+           max = med - 1;
+         else
+           {
+             offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * med + 4);
+             n_parents = GET_UINT32 (cache->buffer, offset);
+             
+             for (j = 0; j < n_parents; j++)
+               {
+                 parent_offset = GET_UINT32 (cache->buffer, offset + 4 + 4 * j);
+                 if (_xdg_mime_cache_mime_type_subclass (cache->buffer + parent_offset, ubase))
+                   return 1;
+               }
+
+             break;
+           }
+       }
+    }
+
+  return 0;
+}
+
+const char *
+_xdg_mime_cache_unalias_mime_type (const char *mime)
+{
+  const char *lookup;
+  
+  lookup = cache_alias_lookup (mime);
+  
+  if (lookup)
+    return lookup;
+  
+  return mime;  
+}
+
+char **
+_xdg_mime_cache_list_mime_parents (const char *mime)
+{
+  int i, j, k, l, p;
+  char *all_parents[128]; /* we'll stop at 128 */ 
+  char **result;
+
+  mime = xdg_mime_unalias_mime_type (mime);
+
+  p = 0;
+  for (i = 0; _caches[i]; i++)
+    {
+      XdgMimeCache *cache = _caches[i];
+  
+      xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 8);
+      xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
+
+      for (j = 0; j < n_entries; j++)
+       {
+         xdg_uint32_t mimetype_offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * j);
+         xdg_uint32_t parents_offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * j + 4);
+
+         if (strcmp (cache->buffer + mimetype_offset, mime) == 0)
+           {
+             xdg_uint32_t parent_mime_offset;
+             xdg_uint32_t n_parents = GET_UINT32 (cache->buffer, parents_offset);
+
+             for (k = 0; k < n_parents && p < 127; k++)
+               {
+                 parent_mime_offset = GET_UINT32 (cache->buffer, parents_offset + 4 + 4 * k);
+
+                 /* Don't add same parent multiple times.
+                  * This can happen for instance if the same type is listed in multiple directories
+                  */
+                 for (l = 0; l < p; l++)
+                   {
+                     if (strcmp (all_parents[l], cache->buffer + parent_mime_offset) == 0)
+                       break;
+                   }
+
+                 if (l == p)
+                   all_parents[p++] = cache->buffer + parent_mime_offset;
+               }
+
+             break;
+           }
+       }
+    }
+  all_parents[p++] = NULL;
+  
+  result = (char **) malloc (p * sizeof (char *));
+  memcpy (result, all_parents, p * sizeof (char *));
+
+  return result;
+}
+
+static const char *
+cache_lookup_icon (const char *mime, int header)
+{
+  const char *ptr;
+  int i, min, max, mid, cmp;
+
+  for (i = 0; _caches[i]; i++)
+    {
+      XdgMimeCache *cache = _caches[i];
+      xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, header);
+      xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
+      xdg_uint32_t offset;
+
+      min = 0; 
+      max = n_entries - 1;
+      while (max >= min) 
+        {
+          mid = (min + max) / 2;
+
+          offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * mid);
+          ptr = cache->buffer + offset;
+          cmp = strcmp (ptr, mime);
+         
+          if (cmp < 0)
+            min = mid + 1;
+          else if (cmp > 0)
+            max = mid - 1;
+          else
+            {
+              offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * mid + 4);
+              return cache->buffer + offset;
+            }
+        }
+    }
+
+  return NULL;
+}
+
+const char *
+_xdg_mime_cache_get_generic_icon (const char *mime)
+{
+  return cache_lookup_icon (mime, 36);
+}
+
+const char *
+_xdg_mime_cache_get_icon (const char *mime)
+{
+  return cache_lookup_icon (mime, 32);
+}
+
+static void
+dump_glob_node (XdgMimeCache *cache,
+               xdg_uint32_t  offset,
+               int           depth)
+{
+  xdg_unichar_t character;
+  xdg_uint32_t mime_offset;
+  xdg_uint32_t n_children;
+  xdg_uint32_t child_offset;
+  int i;
+
+  character = GET_UINT32 (cache->buffer, offset);
+  mime_offset = GET_UINT32 (cache->buffer, offset + 4);
+  n_children = GET_UINT32 (cache->buffer, offset + 8);
+  child_offset = GET_UINT32 (cache->buffer, offset + 12);
+  for (i = 0; i < depth; i++)
+    printf (" ");
+  printf ("%c", character);
+  if (mime_offset)
+    printf (" - %s", cache->buffer + mime_offset);
+  printf ("\n");
+  if (child_offset)
+  {
+    for (i = 0; i < n_children; i++)
+      dump_glob_node (cache, child_offset + 20 * i, depth + 1);
+  }
+}
+
+void
+_xdg_mime_cache_glob_dump (void)
+{
+  int i, j;
+  for (i = 0; _caches[i]; i++)
+  {
+    XdgMimeCache *cache = _caches[i];
+    xdg_uint32_t list_offset;
+    xdg_uint32_t n_entries;
+    xdg_uint32_t offset;
+    list_offset = GET_UINT32 (cache->buffer, 16);
+    n_entries = GET_UINT32 (cache->buffer, list_offset);
+    offset = GET_UINT32 (cache->buffer, list_offset + 4);
+    for (j = 0; j < n_entries; j++)
+           dump_glob_node (cache, offset + 20 * j, 0);
+  }
+}
+
+
diff --git a/xdgmime/src/xdgmimecache.h b/xdgmime/src/xdgmimecache.h
new file mode 100644 (file)
index 0000000..27f42d0
--- /dev/null
@@ -0,0 +1,81 @@
+/* -*- mode: C; c-file-style: "gnu" -*- */
+/* xdgmimecache.h: Private file.  Datastructure for mmapped caches.
+ *
+ * More info can be found at http://www.freedesktop.org/standards/
+ *
+ * Copyright (C) 2005  Matthias Clasen <mclasen@redhat.com>
+ *
+ * Licensed under the Academic Free License version 2.0
+ * Or under the following terms:
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __XDG_MIME_CACHE_H__
+#define __XDG_MIME_CACHE_H__
+
+#include "xdgmime.h"
+
+typedef struct _XdgMimeCache XdgMimeCache;
+
+#ifdef XDG_PREFIX
+#define _xdg_mime_cache_new_from_file                 XDG_RESERVED_ENTRY(cache_new_from_file)
+#define _xdg_mime_cache_ref                           XDG_RESERVED_ENTRY(cache_ref)
+#define _xdg_mime_cache_unref                         XDG_RESERVED_ENTRY(cache_unref)
+#define _xdg_mime_cache_get_max_buffer_extents        XDG_RESERVED_ENTRY(cache_get_max_buffer_extents)
+#define _xdg_mime_cache_get_mime_type_for_data        XDG_RESERVED_ENTRY(cache_get_mime_type_for_data)
+#define _xdg_mime_cache_get_mime_type_for_file        XDG_RESERVED_ENTRY(cache_get_mime_type_for_file)
+#define _xdg_mime_cache_get_mime_type_from_file_name  XDG_RESERVED_ENTRY(cache_get_mime_type_from_file_name)
+#define _xdg_mime_cache_get_mime_types_from_file_name XDG_RESERVED_ENTRY(cache_get_mime_types_from_file_name)
+#define _xdg_mime_cache_list_mime_parents             XDG_RESERVED_ENTRY(cache_list_mime_parents)
+#define _xdg_mime_cache_mime_type_subclass            XDG_RESERVED_ENTRY(cache_mime_type_subclass)
+#define _xdg_mime_cache_unalias_mime_type             XDG_RESERVED_ENTRY(cache_unalias_mime_type)
+#define _xdg_mime_cache_get_icon                      XDG_RESERVED_ENTRY(cache_get_icon)
+#define _xdg_mime_cache_get_generic_icon              XDG_RESERVED_ENTRY(cache_get_generic_icon)
+#define _xdg_mime_cache_glob_dump                     XDG_RESERVED_ENTRY(cache_glob_dump)
+#endif
+
+extern XdgMimeCache **_caches;
+
+XdgMimeCache *_xdg_mime_cache_new_from_file (const char   *file_name);
+XdgMimeCache *_xdg_mime_cache_ref           (XdgMimeCache *cache);
+void          _xdg_mime_cache_unref         (XdgMimeCache *cache);
+
+
+const char  *_xdg_mime_cache_get_mime_type_for_data       (const void *data,
+                                                          size_t      len,
+                                                          int        *result_prio);
+const char  *_xdg_mime_cache_get_mime_type_for_file       (const char  *file_name,
+                                                          struct stat *statbuf);
+int          _xdg_mime_cache_get_mime_types_from_file_name (const char *file_name,
+                                                           const char  *mime_types[],
+                                                           int          n_mime_types);
+const char  *_xdg_mime_cache_get_mime_type_from_file_name (const char *file_name);
+int          _xdg_mime_cache_is_valid_mime_type           (const char *mime_type);
+int          _xdg_mime_cache_mime_type_equal              (const char *mime_a,
+                                                          const char *mime_b);
+int          _xdg_mime_cache_media_type_equal             (const char *mime_a,
+                                                          const char *mime_b);
+int          _xdg_mime_cache_mime_type_subclass           (const char *mime_a,
+                                                          const char *mime_b);
+char       **_xdg_mime_cache_list_mime_parents           (const char *mime);
+const char  *_xdg_mime_cache_unalias_mime_type            (const char *mime);
+int          _xdg_mime_cache_get_max_buffer_extents       (void);
+const char  *_xdg_mime_cache_get_icon                     (const char *mime);
+const char  *_xdg_mime_cache_get_generic_icon             (const char *mime);
+void         _xdg_mime_cache_glob_dump                    (void);
+
+#endif /* __XDG_MIME_CACHE_H__ */
diff --git a/xdgmime/src/xdgmimeglob.c b/xdgmime/src/xdgmimeglob.c
new file mode 100644 (file)
index 0000000..f8434bc
--- /dev/null
@@ -0,0 +1,691 @@
+/* -*- mode: C; c-file-style: "gnu" -*- */
+/* xdgmimeglob.c: Private file.  Datastructure for storing the globs.
+ *
+ * More info can be found at http://www.freedesktop.org/standards/
+ *
+ * Copyright (C) 2003  Red Hat, Inc.
+ * Copyright (C) 2003  Jonathan Blandford <jrb@alum.mit.edu>
+ *
+ * Licensed under the Academic Free License version 2.0
+ * Or under the following terms:
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "xdgmimeglob.h"
+#include "xdgmimeint.h"
+#include <stdlib.h>
+#include <stdio.h>
+#include <assert.h>
+#include <string.h>
+#include <fnmatch.h>
+
+#ifndef        FALSE
+#define        FALSE   (0)
+#endif
+
+#ifndef        TRUE
+#define        TRUE    (!FALSE)
+#endif
+
+typedef struct XdgGlobHashNode XdgGlobHashNode;
+typedef struct XdgGlobList XdgGlobList;
+
+struct XdgGlobHashNode
+{
+  xdg_unichar_t character;
+  const char *mime_type;
+  int weight;
+  int case_sensitive;
+  XdgGlobHashNode *next;
+  XdgGlobHashNode *child;
+};
+struct XdgGlobList
+{
+  const char *data;
+  const char *mime_type;
+  int weight;
+  int case_sensitive;
+  XdgGlobList *next;
+};
+
+struct XdgGlobHash
+{
+  XdgGlobList *literal_list;
+  XdgGlobHashNode *simple_node;
+  XdgGlobList *full_list;
+};
+
+
+/* XdgGlobList
+ */
+static XdgGlobList *
+_xdg_glob_list_new (void)
+{
+  XdgGlobList *new_element;
+
+  new_element = calloc (1, sizeof (XdgGlobList));
+
+  return new_element;
+}
+
+/* Frees glob_list and all of it's children */
+static void
+_xdg_glob_list_free (XdgGlobList *glob_list)
+{
+  XdgGlobList *ptr, *next;
+
+  ptr = glob_list;
+
+  while (ptr != NULL)
+    {
+      next = ptr->next;
+
+      if (ptr->data)
+       free ((void *) ptr->data);
+      if (ptr->mime_type)
+       free ((void *) ptr->mime_type);
+      free (ptr);
+
+      ptr = next;
+    }
+}
+
+static XdgGlobList *
+_xdg_glob_list_append (XdgGlobList *glob_list,
+                      void        *data,
+                      const char  *mime_type,
+                      int          weight,
+                      int          case_sensitive)
+{
+  XdgGlobList *new_element;
+  XdgGlobList *tmp_element;
+
+  tmp_element = glob_list;
+  while (tmp_element != NULL)
+    {
+      if (strcmp (tmp_element->data, data) == 0 &&
+         strcmp (tmp_element->mime_type, mime_type) == 0)
+       return glob_list;
+
+      tmp_element = tmp_element->next;
+    }
+
+  new_element = _xdg_glob_list_new ();
+  new_element->data = data;
+  new_element->mime_type = mime_type;
+  new_element->weight = weight;
+  new_element->case_sensitive = case_sensitive;
+  if (glob_list == NULL)
+    return new_element;
+
+  tmp_element = glob_list;
+  while (tmp_element->next != NULL)
+    tmp_element = tmp_element->next;
+
+  tmp_element->next = new_element;
+
+  return glob_list;
+}
+
+/* XdgGlobHashNode
+ */
+
+static XdgGlobHashNode *
+_xdg_glob_hash_node_new (void)
+{
+  XdgGlobHashNode *glob_hash_node;
+
+  glob_hash_node = calloc (1, sizeof (XdgGlobHashNode));
+
+  return glob_hash_node;
+}
+
+static void
+_xdg_glob_hash_node_dump (XdgGlobHashNode *glob_hash_node,
+                         int depth)
+{
+  int i;
+  for (i = 0; i < depth; i++)
+    printf (" ");
+
+  printf ("%c", (char)glob_hash_node->character);
+  if (glob_hash_node->mime_type)
+    printf (" - %s %d\n", glob_hash_node->mime_type, glob_hash_node->weight);
+  else
+    printf ("\n");
+  if (glob_hash_node->child)
+    _xdg_glob_hash_node_dump (glob_hash_node->child, depth + 1);
+  if (glob_hash_node->next)
+    _xdg_glob_hash_node_dump (glob_hash_node->next, depth);
+}
+
+static XdgGlobHashNode *
+_xdg_glob_hash_insert_ucs4 (XdgGlobHashNode *glob_hash_node,
+                           xdg_unichar_t   *text,
+                           const char      *mime_type,
+                           int              weight,
+                           int              case_sensitive)
+{
+  XdgGlobHashNode *node;
+  xdg_unichar_t character;
+
+  character = text[0];
+
+  if ((glob_hash_node == NULL) ||
+      (character < glob_hash_node->character))
+    {
+      node = _xdg_glob_hash_node_new ();
+      node->character = character;
+      node->next = glob_hash_node;
+      glob_hash_node = node;
+    }
+  else if (character == glob_hash_node->character)
+    {
+      node = glob_hash_node;
+    }
+  else
+    {
+      XdgGlobHashNode *prev_node;
+      int found_node = FALSE;
+
+      /* Look for the first character of text in glob_hash_node, and insert it if we
+       * have to.*/
+      prev_node = glob_hash_node;
+      node = prev_node->next;
+
+      while (node != NULL)
+       {
+         if (character < node->character)
+           {
+             node = _xdg_glob_hash_node_new ();
+             node->character = character;
+             node->next = prev_node->next;
+             prev_node->next = node;
+
+             found_node = TRUE;
+             break;
+           }
+         else if (character == node->character)
+           {
+             found_node = TRUE;
+             break;
+           }
+         prev_node = node;
+         node = node->next;
+       }
+
+      if (! found_node)
+       {
+         node = _xdg_glob_hash_node_new ();
+         node->character = character;
+         node->next = prev_node->next;
+         prev_node->next = node;
+       }
+    }
+
+  text++;
+  if (*text == 0)
+    {
+      if (node->mime_type)
+       {
+         if (strcmp (node->mime_type, mime_type) != 0)
+           {
+             XdgGlobHashNode *child;
+             int found_node = FALSE;
+
+             child = node->child;
+             while (child && child->character == 0)
+               {
+                 if (strcmp (child->mime_type, mime_type) == 0)
+                   {
+                     found_node = TRUE;
+                     break;
+                   }
+                 child = child->next;
+               }
+
+             if (!found_node)
+               {
+                 child = _xdg_glob_hash_node_new ();
+                 child->character = 0;
+                 child->mime_type = strdup (mime_type);
+                 child->weight = weight;
+                 child->case_sensitive = case_sensitive;
+                 child->child = NULL;
+                 child->next = node->child;
+                 node->child = child;
+               }
+           }
+       }
+      else
+       {
+         node->mime_type = strdup (mime_type);
+         node->weight = weight;
+         node->case_sensitive = case_sensitive;
+       }
+    }
+  else
+    {
+      node->child = _xdg_glob_hash_insert_ucs4 (node->child, text, mime_type, weight, case_sensitive);
+    }
+  return glob_hash_node;
+}
+
+/* glob must be valid UTF-8 */
+static XdgGlobHashNode *
+_xdg_glob_hash_insert_text (XdgGlobHashNode *glob_hash_node,
+                           const char      *text,
+                           const char      *mime_type,
+                           int              weight,
+                           int              case_sensitive)
+{
+  XdgGlobHashNode *node;
+  xdg_unichar_t *unitext;
+  int len;
+
+  unitext = _xdg_convert_to_ucs4 (text, &len);
+  _xdg_reverse_ucs4 (unitext, len);
+  node = _xdg_glob_hash_insert_ucs4 (glob_hash_node, unitext, mime_type, weight, case_sensitive);
+  free (unitext);
+  return node;
+}
+
+typedef struct {
+  const char *mime;
+  int weight;
+} MimeWeight;
+
+static int
+_xdg_glob_hash_node_lookup_file_name (XdgGlobHashNode *glob_hash_node,
+                                     const char      *file_name,
+                                     int              len,
+                                     int              case_sensitive_check,
+                                     MimeWeight       mime_types[],
+                                     int              n_mime_types)
+{
+  int n;
+  XdgGlobHashNode *node;
+  xdg_unichar_t character;
+
+  if (glob_hash_node == NULL)
+    return 0;
+
+  character = file_name[len - 1];
+
+  for (node = glob_hash_node; node && character >= node->character; node = node->next)
+    {
+      if (character == node->character)
+        {
+          len--;
+          n = 0;
+          if (len > 0) 
+           {
+             n = _xdg_glob_hash_node_lookup_file_name (node->child,
+                                                       file_name,
+                                                       len,
+                                                       case_sensitive_check,
+                                                       mime_types,
+                                                       n_mime_types);
+           }
+         if (n == 0)
+           {
+              if (node->mime_type &&
+                 (case_sensitive_check ||
+                  !node->case_sensitive))
+                {
+                 mime_types[n].mime = node->mime_type;
+                 mime_types[n].weight = node->weight;
+                 n++; 
+                }
+             node = node->child;
+             while (n < n_mime_types && node && node->character == 0)
+               {
+                  if (node->mime_type &&
+                     (case_sensitive_check ||
+                      !node->case_sensitive))
+                   {
+                     mime_types[n].mime = node->mime_type;
+                     mime_types[n].weight = node->weight;
+                     n++;
+                   }
+                 node = node->next;
+               }
+           }
+         return n;
+       }
+    }
+
+  return 0;
+}
+
+static int compare_mime_weight (const void *a, const void *b)
+{
+  const MimeWeight *aa = (const MimeWeight *)a;
+  const MimeWeight *bb = (const MimeWeight *)b;
+
+  return bb->weight - aa->weight;
+}
+
+#define ISUPPER(c)             ((c) >= 'A' && (c) <= 'Z')
+static char *
+ascii_tolower (const char *str)
+{
+  char *p, *lower;
+
+  lower = strdup (str);
+  p = lower;
+  while (*p != 0)
+    {
+      char c = *p;
+      *p++ = ISUPPER (c) ? c - 'A' + 'a' : c;
+    }
+  return lower;
+}
+
+int
+_xdg_glob_hash_lookup_file_name (XdgGlobHash *glob_hash,
+                                const char  *file_name,
+                                const char  *mime_types[],
+                                int          n_mime_types)
+{
+  XdgGlobList *list;
+  int i, n;
+  MimeWeight mimes[10];
+  int n_mimes = 10;
+  int len;
+  char *lower_case;
+
+  /* First, check the literals */
+
+  assert (file_name != NULL && n_mime_types > 0);
+
+  n = 0;
+
+  lower_case = ascii_tolower (file_name);
+
+  for (list = glob_hash->literal_list; list; list = list->next)
+    {
+      if (strcmp ((const char *)list->data, file_name) == 0)
+       {
+         mime_types[0] = list->mime_type;
+         free (lower_case);
+         return 1;
+       }
+    }
+
+  for (list = glob_hash->literal_list; list; list = list->next)
+    {
+      if (!list->case_sensitive &&
+         strcmp ((const char *)list->data, lower_case) == 0)
+       {
+         mime_types[0] = list->mime_type;
+         free (lower_case);
+         return 1;
+       }
+    }
+
+
+  len = strlen (file_name);
+  n = _xdg_glob_hash_node_lookup_file_name (glob_hash->simple_node, lower_case, len, FALSE,
+                                           mimes, n_mimes);
+  if (n == 0)
+    n = _xdg_glob_hash_node_lookup_file_name (glob_hash->simple_node, file_name, len, TRUE,
+                                             mimes, n_mimes);
+
+  if (n == 0)
+    {
+      for (list = glob_hash->full_list; list && n < n_mime_types; list = list->next)
+        {
+          if (fnmatch ((const char *)list->data, file_name, 0) == 0)
+           {
+             mimes[n].mime = list->mime_type;
+             mimes[n].weight = list->weight;
+             n++;
+           }
+        }
+    }
+  free (lower_case);
+
+  qsort (mimes, n, sizeof (MimeWeight), compare_mime_weight);
+
+  if (n_mime_types < n)
+    n = n_mime_types;
+
+  for (i = 0; i < n; i++)
+    mime_types[i] = mimes[i].mime;
+
+  return n;
+}
+
+
+
+/* XdgGlobHash
+ */
+
+XdgGlobHash *
+_xdg_glob_hash_new (void)
+{
+  XdgGlobHash *glob_hash;
+
+  glob_hash = calloc (1, sizeof (XdgGlobHash));
+
+  return glob_hash;
+}
+
+
+static void
+_xdg_glob_hash_free_nodes (XdgGlobHashNode *node)
+{
+  if (node)
+    {
+      if (node->child)
+       _xdg_glob_hash_free_nodes (node->child);
+      if (node->next)
+       _xdg_glob_hash_free_nodes (node->next);
+      if (node->mime_type)
+       free ((void *) node->mime_type);
+      free (node);
+    }
+}
+
+void
+_xdg_glob_hash_free (XdgGlobHash *glob_hash)
+{
+  _xdg_glob_list_free (glob_hash->literal_list);
+  _xdg_glob_list_free (glob_hash->full_list);
+  _xdg_glob_hash_free_nodes (glob_hash->simple_node);
+  free (glob_hash);
+}
+
+XdgGlobType
+_xdg_glob_determine_type (const char *glob)
+{
+  const char *ptr;
+  int maybe_in_simple_glob = FALSE;
+  int first_char = TRUE;
+
+  ptr = glob;
+
+  while (*ptr != '\0')
+    {
+      if (*ptr == '*' && first_char)
+       maybe_in_simple_glob = TRUE;
+      else if (*ptr == '\\' || *ptr == '[' || *ptr == '?' || *ptr == '*')
+         return XDG_GLOB_FULL;
+
+      first_char = FALSE;
+      ptr = _xdg_utf8_next_char (ptr);
+    }
+  if (maybe_in_simple_glob)
+    return XDG_GLOB_SIMPLE;
+  else
+    return XDG_GLOB_LITERAL;
+}
+
+/* glob must be valid UTF-8 */
+void
+_xdg_glob_hash_append_glob (XdgGlobHash *glob_hash,
+                           const char  *glob,
+                           const char  *mime_type,
+                           int          weight,
+                           int          case_sensitive)
+{
+  XdgGlobType type;
+
+  assert (glob_hash != NULL);
+  assert (glob != NULL);
+
+  type = _xdg_glob_determine_type (glob);
+
+  switch (type)
+    {
+    case XDG_GLOB_LITERAL:
+      glob_hash->literal_list = _xdg_glob_list_append (glob_hash->literal_list, strdup (glob), strdup (mime_type), weight, case_sensitive);
+      break;
+    case XDG_GLOB_SIMPLE:
+      glob_hash->simple_node = _xdg_glob_hash_insert_text (glob_hash->simple_node, glob + 1, mime_type, weight, case_sensitive);
+      break;
+    case XDG_GLOB_FULL:
+      glob_hash->full_list = _xdg_glob_list_append (glob_hash->full_list, strdup (glob), strdup (mime_type), weight, case_sensitive);
+      break;
+    }
+}
+
+void
+_xdg_glob_hash_dump (XdgGlobHash *glob_hash)
+{
+  XdgGlobList *list;
+  printf ("LITERAL STRINGS\n");
+  if (!glob_hash || glob_hash->literal_list == NULL)
+    {
+      printf ("    None\n");
+    }
+  else
+    {
+      for (list = glob_hash->literal_list; list; list = list->next)
+       printf ("    %s - %s %d\n", (char *)list->data, list->mime_type, list->weight);
+    }
+  printf ("\nSIMPLE GLOBS\n");
+  if (!glob_hash || glob_hash->simple_node == NULL)
+    {
+      printf ("    None\n");
+    }
+  else
+    {
+      _xdg_glob_hash_node_dump (glob_hash->simple_node, 4);
+    }
+
+  printf ("\nFULL GLOBS\n");
+  if (!glob_hash || glob_hash->full_list == NULL)
+    {
+      printf ("    None\n");
+    }
+  else
+    {
+      for (list = glob_hash->full_list; list; list = list->next)
+       printf ("    %s - %s %d\n", (char *)list->data, list->mime_type, list->weight);
+    }
+}
+
+
+void
+_xdg_mime_glob_read_from_file (XdgGlobHash *glob_hash,
+                              const char  *file_name,
+                              int          version_two)
+{
+  FILE *glob_file;
+  char line[255];
+  char *p;
+
+  glob_file = fopen (file_name, "r");
+
+  if (glob_file == NULL)
+    return;
+
+  /* FIXME: Not UTF-8 safe.  Doesn't work if lines are greater than 255 chars.
+   * Blah */
+  while (fgets (line, 255, glob_file) != NULL)
+    {
+      char *colon;
+      char *mimetype, *glob, *end;
+      int weight;
+      int case_sensitive;
+
+      if (line[0] == '#' || line[0] == 0)
+       continue;
+
+      end = line + strlen(line) - 1;
+      if (*end == '\n')
+       *end = 0;
+
+      p = line;
+      if (version_two)
+       {
+         colon = strchr (p, ':');
+         if (colon == NULL)
+           continue;
+         *colon = 0;
+          weight = atoi (p);
+         p = colon + 1;
+       }
+      else
+       weight = 50;
+
+      colon = strchr (p, ':');
+      if (colon == NULL)
+       continue;
+      *colon = 0;
+
+      mimetype = p;
+      p = colon + 1;
+      glob = p;
+      case_sensitive = FALSE;
+
+      colon = strchr (p, ':');
+      if (version_two && colon != NULL)
+       {
+         char *flag;
+
+         /* We got flags */
+         *colon = 0;
+         p = colon + 1;
+
+         /* Flags end at next colon */
+         colon = strchr (p, ':');
+         if (colon != NULL)
+           *colon = 0;
+
+         flag = strstr (p, "cs");
+         if (flag != NULL &&
+             /* Start or after comma */
+             (flag == p ||
+              flag[-1] == ',') &&
+             /* ends with comma or end of string */
+             (flag[2] == 0 ||
+              flag[2] == ','))
+           case_sensitive = TRUE;
+       }
+
+      _xdg_glob_hash_append_glob (glob_hash, glob, mimetype, weight, case_sensitive);
+    }
+
+  fclose (glob_file);
+}
diff --git a/xdgmime/src/xdgmimeglob.h b/xdgmime/src/xdgmimeglob.h
new file mode 100644 (file)
index 0000000..0018292
--- /dev/null
@@ -0,0 +1,70 @@
+/* -*- mode: C; c-file-style: "gnu" -*- */
+/* xdgmimeglob.h: Private file.  Datastructure for storing the globs.
+ *
+ * More info can be found at http://www.freedesktop.org/standards/
+ *
+ * Copyright (C) 2003  Red Hat, Inc.
+ * Copyright (C) 2003  Jonathan Blandford <jrb@alum.mit.edu>
+ *
+ * Licensed under the Academic Free License version 2.0
+ * Or under the following terms:
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __XDG_MIME_GLOB_H__
+#define __XDG_MIME_GLOB_H__
+
+#include "xdgmime.h"
+
+typedef struct XdgGlobHash XdgGlobHash;
+
+typedef enum
+{
+  XDG_GLOB_LITERAL, /* Makefile */
+  XDG_GLOB_SIMPLE,  /* *.gif */
+  XDG_GLOB_FULL     /* x*.[ch] */
+} XdgGlobType;
+
+  
+#ifdef XDG_PREFIX
+#define _xdg_mime_glob_read_from_file         XDG_RESERVED_ENTRY(glob_read_from_file)
+#define _xdg_glob_hash_new                    XDG_RESERVED_ENTRY(hash_new)
+#define _xdg_glob_hash_free                   XDG_RESERVED_ENTRY(hash_free)
+#define _xdg_glob_hash_lookup_file_name       XDG_RESERVED_ENTRY(hash_lookup_file_name)
+#define _xdg_glob_hash_append_glob            XDG_RESERVED_ENTRY(hash_append_glob)
+#define _xdg_glob_determine_type              XDG_RESERVED_ENTRY(determine_type)
+#define _xdg_glob_hash_dump                   XDG_RESERVED_ENTRY(hash_dump)
+#endif
+
+void         _xdg_mime_glob_read_from_file   (XdgGlobHash *glob_hash,
+                                             const char  *file_name,
+                                             int          version_two);
+XdgGlobHash *_xdg_glob_hash_new              (void);
+void         _xdg_glob_hash_free             (XdgGlobHash *glob_hash);
+int          _xdg_glob_hash_lookup_file_name (XdgGlobHash *glob_hash,
+                                             const char  *text,
+                                             const char  *mime_types[],
+                                             int          n_mime_types);
+void         _xdg_glob_hash_append_glob      (XdgGlobHash *glob_hash,
+                                             const char  *glob,
+                                             const char  *mime_type,
+                                             int          weight,
+                                             int          case_sensitive);
+XdgGlobType  _xdg_glob_determine_type        (const char  *glob);
+void         _xdg_glob_hash_dump             (XdgGlobHash *glob_hash);
+
+#endif /* __XDG_MIME_GLOB_H__ */
diff --git a/xdgmime/src/xdgmimeglobs2.c b/xdgmime/src/xdgmimeglobs2.c
new file mode 100755 (executable)
index 0000000..e5ad349
--- /dev/null
@@ -0,0 +1,302 @@
+/*
+ * xdgmimeglobs2.c
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: Jayoun Lee                         <airjany@samsung.com>
+ * Contact: Seokkyu Jang                        <seokkyu.jang@samsung.com>
+ * Contact: Sangil Yoon                         <si83.yoon@samsung.com>
+ *
+ * This library is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the
+ * Free Software Foundation; either version 2.1 of the License, or (at your option)
+ * any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, write to the Free Software Foundation, Inc., 51
+ * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "xdgmime.h"
+
+#ifndef API
+#define API __attribute__ ((visibility("default")))
+#endif
+
+
+#define GLOBS2_PATH "/usr/share/mime/globs2"
+#define MEM_INC_RATE 20
+
+
+/*************
+ * Data types
+ *************/
+typedef struct mime_type_info
+{
+       char *mime_type;
+       char **file_names;
+       size_t file_names_size;
+} mime_type_info;
+
+typedef struct mime_type_info_list
+{
+       mime_type_info **mti_list;
+       size_t mti_list_size;
+       time_t globs2_mtime;
+} mime_type_info_list;
+
+static mime_type_info_list *mti_list = NULL;
+
+
+/***************************
+ * mime_type_info functions
+ ***************************/
+static mime_type_info *
+mime_type_info_new(void)
+{
+       mime_type_info *mti = NULL;
+
+       mti = calloc(1, sizeof(mime_type_info));
+       mti->file_names = calloc(MEM_INC_RATE, sizeof(char *));
+       mti->file_names_size = MEM_INC_RATE;
+
+       return mti;
+}
+
+static void
+mime_type_info_free(mime_type_info *mti)
+{
+       if(!mti) return;
+
+       if(mti->mime_type) free(mti->mime_type);
+       if(mti->file_names) {
+               char **tmpstr;
+               for(tmpstr = mti->file_names; *tmpstr; tmpstr++) {
+                       free(*tmpstr);
+               }
+               free(mti->file_names);
+       }
+       free(mti);
+}
+
+static int
+mime_type_info_add_file_name(mime_type_info *mti,
+               const char *mime_type,
+               const char *file_name)
+{
+       if(!mti) return;
+
+       if(!mti->mime_type) {
+               mti->mime_type = strdup(mime_type);
+       }
+       else { 
+               /* mime_type already exist, but mime_type is different! */
+               if(strcmp(mti->mime_type, mime_type)) return -1;
+       }
+
+       /* Find position */
+       char **pname;
+       for(pname = mti->file_names; pname < (mti->file_names + mti->file_names_size - 1) && *pname; pname++) {
+               if(!strcmp(file_name, *pname)) return 0;        /* already exist! */
+       }
+
+       int old_file_names_size = mti->file_names_size;
+
+       if(pname == mti->file_names + old_file_names_size -1) {
+
+               /* memory full. realloc needed */
+               mti->file_names = realloc(mti->file_names, sizeof(char *)*(old_file_names_size+MEM_INC_RATE));
+               if(!mti->file_names) return -1;
+
+               memset(mti->file_names + old_file_names_size, 0x00, sizeof(char *)*MEM_INC_RATE);
+               pname = mti->file_names + old_file_names_size - 1;
+               mti->file_names_size += MEM_INC_RATE;
+       }
+
+       *pname = strdup(file_name);
+       
+       return 0;
+}
+
+
+
+/********************************
+ * mime_type_info_list functions
+ ********************************/
+
+/* definitions */
+
+static mime_type_info_list *
+mime_type_info_list_new(void)
+{
+       mime_type_info_list *mtil;
+
+       mtil = calloc(1, sizeof(mime_type_info_list));
+
+       mtil->mti_list = calloc(MEM_INC_RATE, sizeof(mime_type_info *));
+       mtil->mti_list_size = MEM_INC_RATE;
+
+       return mtil;
+}
+
+
+
+/* free and init mtlist */
+static void
+mime_type_info_list_free(mime_type_info_list *mtil)
+{
+       mime_type_info **mti;
+       char *tmp;
+
+       for(mti = mtil->mti_list; 
+                       *mti; 
+                       mti++) {
+               mime_type_info_free(*mti);
+       }
+
+       free(mtil->mti_list);
+       free(mtil);
+}
+
+static int
+mime_type_info_list_add_file_name(mime_type_info_list *mtil,
+               const char *mime_type,
+               const char *file_name)
+{
+       if(!mtil) return;
+
+       mime_type_info **mti;
+       int found = 0;
+
+       for(mti = mtil->mti_list; mti < (mtil->mti_list + mtil->mti_list_size - 1) && *mti; mti++) {
+               if((*mti)->mime_type && mime_type &&  /* NULL check */
+                       0 == strncmp((*mti)->mime_type, 
+                               mime_type, 
+                               strlen(mime_type))) {
+                       /* found! */
+                       found = 1;
+                       break;
+               }
+       }
+       if(!found) {
+               /* New mime_type_info must be needed */
+
+               /* check memory full */
+               int old_mti_list_size = mtil->mti_list_size;
+               //printf("sizeof(mti_list)=%d, sizeof(mime_type_info *)=%d, old info_list_size=%d\n", sizeof(mtil->mti_list), sizeof(mime_type_info *), old_mti_list_size);
+
+               /* last chance */
+               if(mti == mtil->mti_list + old_mti_list_size - 1) {
+                       /* memory full. realloc needed */
+                       mtil->mti_list = realloc(mtil->mti_list, sizeof(mime_type_info *) * (old_mti_list_size + MEM_INC_RATE));
+                       if(!mtil->mti_list) return -1;
+                       memset(mtil->mti_list + old_mti_list_size, 0x0, sizeof(mime_type_info *) * MEM_INC_RATE);
+                       mti = mtil->mti_list + old_mti_list_size - 1;
+
+                       mtil->mti_list_size += MEM_INC_RATE;
+               }
+       }
+
+       /* assign new data */
+       if(NULL == *mti) *mti = mime_type_info_new();
+       mime_type_info_add_file_name(*mti, mime_type, file_name);
+
+       return 0;
+}
+
+
+/* create or renew mtlist */
+static void
+mime_type_info_list_reload(mime_type_info_list *mtil)
+{
+       FILE *globs2 = NULL;
+       struct stat globs2_stat;
+       int r;
+       char buf[256];
+
+       if(!mtil) return;
+
+       /* Check glob2's mtime.
+        * If reconstruction is not needed, just exit function */
+       if( stat(GLOBS2_PATH, &globs2_stat) ||
+                       globs2_stat.st_mtime <= mtil->globs2_mtime ) return;
+
+       /* clean old mtil */
+       mime_type_info **mti;
+       for(mti = mtil->mti_list; 
+               *mti; 
+               mti++) {
+               mime_type_info_free(*mti);
+               *mti = NULL;
+       }
+
+       /* save globs2's mtime */
+       mtil->globs2_mtime = globs2_stat.st_mtime;
+
+       /* read globs2, and construct data structure */
+       globs2 = fopen(GLOBS2_PATH, "r");
+       char *weight, *mime_type, *file_name, *saveptr = NULL;
+       while(fgets(buf, 255, globs2)) {
+               /* skip comment */
+               if(*buf == '#') continue;
+               /* weight:mime_type:file_name */
+               weight = strtok_r(buf, ":\n", &saveptr);        /* ignored */
+               mime_type = strtok_r(NULL, ":\n", &saveptr);
+               file_name = strtok_r(NULL, ":\n", &saveptr);
+               
+               mime_type_info_list_add_file_name(mtil, mime_type, file_name);
+       }
+       fclose(globs2);
+
+       /* TODO: sort it! */
+
+}
+
+static const char **
+mime_type_info_list_get_file_names(mime_type_info_list *mtil, 
+               const char *mime_type)
+{
+
+       static const char *null_array[] = { NULL };
+
+       if(!mtil) return null_array;
+
+       mime_type_info **_mti;
+       for(_mti = mtil->mti_list; *_mti; _mti++) {
+               if((*_mti)->mime_type && mime_type &&   /* NULL check */
+                       !strcmp((*_mti)->mime_type, mime_type)) {
+                       return (const char **) (*_mti)->file_names;
+               }
+       }
+
+       return null_array;
+}
+
+
+/* API 
+ * Get file names' list from mime type
+ */
+API const char **
+xdg_mime_get_file_names_from_mime_type(const char *mime_type)
+{
+       /* init data structure */
+       static mime_type_info_list *mtil = NULL;
+       if(!mtil) mtil = mime_type_info_list_new();
+       mime_type_info_list_reload(mtil);
+
+       return mime_type_info_list_get_file_names(mtil, mime_type);
+}
+
diff --git a/xdgmime/src/xdgmimeicon.c b/xdgmime/src/xdgmimeicon.c
new file mode 100644 (file)
index 0000000..05c9473
--- /dev/null
@@ -0,0 +1,183 @@
+/* -*- mode: C; c-file-style: "gnu" -*- */
+/* xdgmimeicon.c: Private file.  Datastructure for storing the aliases.
+ *
+ * More info can be found at http://www.freedesktop.org/standards/
+ *
+ * Copyright (C) 2008  Red Hat, Inc.
+ *
+ * Licensed under the Academic Free License version 2.0
+ * Or under the following terms:
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "xdgmimeicon.h"
+#include "xdgmimeint.h"
+#include <stdlib.h>
+#include <stdio.h>
+#include <assert.h>
+#include <string.h>
+#include <fnmatch.h>
+
+#ifndef        FALSE
+#define        FALSE   (0)
+#endif
+
+#ifndef        TRUE
+#define        TRUE    (!FALSE)
+#endif
+
+typedef struct XdgIcon XdgIcon;
+
+struct XdgIcon 
+{
+  char *mime_type;
+  char *icon_name;
+};
+
+struct XdgIconList
+{
+  struct XdgIcon *icons;
+  int n_icons;
+};
+
+XdgIconList *
+_xdg_mime_icon_list_new (void)
+{
+  XdgIconList *list;
+
+  list = malloc (sizeof (XdgIconList));
+
+  list->icons = NULL;
+  list->n_icons = 0;
+
+  return list;
+}
+
+void         
+_xdg_mime_icon_list_free (XdgIconList *list)
+{
+  int i;
+
+  if (list->icons)
+    {
+      for (i = 0; i < list->n_icons; i++)
+       {
+         free (list->icons[i].mime_type);
+         free (list->icons[i].icon_name);
+       }
+      free (list->icons);
+    }
+  free (list);
+}
+
+static int
+icon_entry_cmp (const void *v1, const void *v2)
+{
+  return strcmp (((XdgIcon *)v1)->mime_type, ((XdgIcon *)v2)->mime_type);
+}
+
+const char  *
+_xdg_mime_icon_list_lookup (XdgIconList *list,
+                           const char  *mime_type)
+{
+  XdgIcon *entry;
+  XdgIcon key;
+
+  if (list->n_icons > 0)
+    {
+      key.mime_type = (char *)mime_type;
+      key.icon_name = NULL;
+
+      entry = bsearch (&key, list->icons, list->n_icons,
+                      sizeof (XdgIcon), icon_entry_cmp);
+      if (entry)
+        return entry->icon_name;
+    }
+
+  return NULL;
+}
+
+void
+_xdg_mime_icon_read_from_file (XdgIconList *list,
+                              const char   *file_name)
+{
+  FILE *file;
+  char line[255];
+  int alloc;
+
+  file = fopen (file_name, "r");
+
+  if (file == NULL)
+    return;
+
+  /* FIXME: Not UTF-8 safe.  Doesn't work if lines are greater than 255 chars.
+   * Blah */
+  alloc = list->n_icons + 16;
+  list->icons = realloc (list->icons, alloc * sizeof (XdgIcon));
+  while (fgets (line, 255, file) != NULL)
+    {
+      char *sep;
+      if (line[0] == '#')
+       continue;
+
+      sep = strchr (line, ':');
+      if (sep == NULL)
+       continue;
+      *(sep++) = '\000';
+      sep[strlen (sep) -1] = '\000';
+      if (list->n_icons == alloc)
+       {
+         alloc <<= 1;
+         list->icons = realloc (list->icons, 
+                                  alloc * sizeof (XdgIcon));
+       }
+      list->icons[list->n_icons].mime_type = strdup (line);
+      list->icons[list->n_icons].icon_name = strdup (sep);
+      list->n_icons++;
+    }
+  list->icons = realloc (list->icons, 
+                          list->n_icons * sizeof (XdgIcon));
+
+  fclose (file);  
+  
+  if (list->n_icons > 1)
+    qsort (list->icons, list->n_icons, 
+           sizeof (XdgIcon), icon_entry_cmp);
+}
+
+
+void
+_xdg_mime_icon_list_dump (XdgIconList *list)
+{
+  int i;
+
+  if (list->icons)
+    {
+      for (i = 0; i < list->n_icons; i++)
+       {
+         printf ("%s %s\n", 
+                 list->icons[i].mime_type,
+                 list->icons[i].icon_name);
+       }
+    }
+}
+
+
diff --git a/xdgmime/src/xdgmimeicon.h b/xdgmime/src/xdgmimeicon.h
new file mode 100644 (file)
index 0000000..b5f2583
--- /dev/null
@@ -0,0 +1,50 @@
+/* -*- mode: C; c-file-style: "gnu" -*- */
+/* xdgmimeicon.h: Private file.  Datastructure for storing the aliases.
+ *
+ * More info can be found at http://www.freedesktop.org/standards/
+ *
+ * Copyright (C) 2008  Red Hat, Inc.
+ *
+ * Licensed under the Academic Free License version 2.0
+ * Or under the following terms:
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __XDG_MIME_ICON_H__
+#define __XDG_MIME_ICON_H__
+
+#include "xdgmime.h"
+
+typedef struct XdgIconList XdgIconList;
+
+#ifdef XDG_PREFIX
+#define _xdg_mime_icon_read_from_file        XDG_ENTRY(icon_read_from_file)
+#define _xdg_mime_icon_list_new              XDG_ENTRY(icon_list_new)
+#define _xdg_mime_icon_list_free             XDG_ENTRY(icon_list_free)
+#define _xdg_mime_icon_list_lookup           XDG_ENTRY(icon_list_lookup)
+#define _xdg_mime_icon_list_dump             XDG_ENTRY(icon_list_dump)
+#endif
+
+void          _xdg_mime_icon_read_from_file (XdgIconList *list,
+                                           const char   *file_name);
+XdgIconList  *_xdg_mime_icon_list_new       (void);
+void          _xdg_mime_icon_list_free      (XdgIconList *list);
+const char   *_xdg_mime_icon_list_lookup    (XdgIconList *list,
+                                            const char  *mime);
+void          _xdg_mime_icon_list_dump      (XdgIconList *list);
+
+#endif /* __XDG_MIME_ICON_H__ */
diff --git a/xdgmime/src/xdgmimeint.c b/xdgmime/src/xdgmimeint.c
new file mode 100644 (file)
index 0000000..d372d2c
--- /dev/null
@@ -0,0 +1,191 @@
+/* -*- mode: C; c-file-style: "gnu" -*- */
+/* xdgmimeint.c: Internal defines and functions.
+ *
+ * More info can be found at http://www.freedesktop.org/standards/
+ *
+ * Copyright (C) 2003  Red Hat, Inc.
+ * Copyright (C) 2003  Jonathan Blandford <jrb@alum.mit.edu>
+ *
+ * Licensed under the Academic Free License version 2.0
+ * Or under the following terms:
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "xdgmimeint.h"
+#include <ctype.h>
+#include <string.h>
+
+#ifndef        FALSE
+#define        FALSE   (0)
+#endif
+
+#ifndef        TRUE
+#define        TRUE    (!FALSE)
+#endif
+
+static const char _xdg_utf8_skip_data[256] = {
+  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+  2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
+  3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,1,1
+};
+
+const char * const _xdg_utf8_skip = _xdg_utf8_skip_data;
+
+
+
+/* Returns the number of unprocessed characters. */
+xdg_unichar_t
+_xdg_utf8_to_ucs4(const char *source)
+{
+  xdg_unichar_t ucs32;
+  if( ! ( *source & 0x80 ) )
+    {
+      ucs32 = *source;
+    }
+  else
+    {
+      int bytelength = 0;
+      xdg_unichar_t result;
+      if ( ! (*source & 0x40) )
+       {
+         ucs32 = *source;
+       }
+      else
+       {
+         if ( ! (*source & 0x20) )
+           {
+             result = *source++ & 0x1F;
+             bytelength = 2;
+           }
+         else if ( ! (*source & 0x10) )
+           {
+             result = *source++ & 0x0F;
+             bytelength = 3;
+           }
+         else if ( ! (*source & 0x08) )
+           {
+             result = *source++ & 0x07;
+             bytelength = 4;
+           }
+         else if ( ! (*source & 0x04) )
+           {
+             result = *source++ & 0x03;
+             bytelength = 5;
+           }
+         else if ( ! (*source & 0x02) )
+           {
+             result = *source++ & 0x01;
+             bytelength = 6;
+           }
+         else
+           {
+             result = *source++;
+             bytelength = 1;
+           }
+
+         for ( bytelength --; bytelength > 0; bytelength -- )
+           {
+             result <<= 6;
+             result |= *source++ & 0x3F;
+           }
+         ucs32 = result;
+       }
+    }
+  return ucs32;
+}
+
+
+/* hullo.  this is great code.  don't rewrite it */
+
+xdg_unichar_t
+_xdg_ucs4_to_lower (xdg_unichar_t source)
+{
+  /* FIXME: Do a real to_upper sometime */
+  /* CaseFolding-3.2.0.txt has a table of rules. */
+  if ((source & 0xFF) == source)
+    return (xdg_unichar_t) tolower ((unsigned char) source);
+  return source;
+}
+
+int
+_xdg_utf8_validate (const char *source)
+{
+  /* FIXME: actually write */
+  return TRUE;
+}
+
+const char *
+_xdg_get_base_name (const char *file_name)
+{
+  const char *base_name;
+
+  if (file_name == NULL)
+    return NULL;
+
+  base_name = strrchr (file_name, '/');
+
+  if (base_name == NULL)
+    return file_name;
+  else
+    return base_name + 1;
+}
+
+xdg_unichar_t *
+_xdg_convert_to_ucs4 (const char *source, int *len)
+{
+  xdg_unichar_t *out;
+  int i;
+  const char *p;
+
+  out = malloc (sizeof (xdg_unichar_t) * (strlen (source) + 1));
+
+  p = source;
+  i = 0;
+  while (*p) 
+    {
+      out[i++] = _xdg_utf8_to_ucs4 (p);
+      p = _xdg_utf8_next_char (p); 
+    }
+  out[i] = 0;
+  *len = i;
+  return out;
+}
+
+void
+_xdg_reverse_ucs4 (xdg_unichar_t *source, int len)
+{
+  xdg_unichar_t c;
+  int i;
+
+  for (i = 0; i < len - i - 1; i++) 
+    {
+      c = source[i]; 
+      source[i] = source[len - i - 1];
+      source[len - i - 1] = c;
+    }
+}
+
diff --git a/xdgmime/src/xdgmimeint.h b/xdgmime/src/xdgmimeint.h
new file mode 100644 (file)
index 0000000..232c808
--- /dev/null
@@ -0,0 +1,77 @@
+/* -*- mode: C; c-file-style: "gnu" -*- */
+/* xdgmimeint.h: Internal defines and functions.
+ *
+ * More info can be found at http://www.freedesktop.org/standards/
+ *
+ * Copyright (C) 2003  Red Hat, Inc.
+ * Copyright (C) 2003  Jonathan Blandford <jrb@alum.mit.edu>
+ *
+ * Licensed under the Academic Free License version 2.0
+ * Or under the following terms:
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __XDG_MIME_INT_H__
+#define __XDG_MIME_INT_H__
+
+#include "xdgmime.h"
+
+
+#ifndef        FALSE
+#define        FALSE (0)
+#endif
+
+#ifndef        TRUE
+#define        TRUE (!FALSE)
+#endif
+
+/* FIXME: Needs to be configure check */
+typedef unsigned int   xdg_unichar_t;
+typedef unsigned char  xdg_uchar8_t;
+typedef unsigned short xdg_uint16_t;
+typedef unsigned int   xdg_uint32_t;
+
+#ifdef XDG_PREFIX
+#define _xdg_utf8_skip       XDG_RESERVED_ENTRY(utf8_skip)
+#define _xdg_utf8_to_ucs4    XDG_RESERVED_ENTRY(utf8_to_ucs4)
+#define _xdg_ucs4_to_lower   XDG_RESERVED_ENTRY(ucs4_to_lower)
+#define _xdg_utf8_validate   XDG_RESERVED_ENTRY(utf8_validate)
+#define _xdg_get_base_name   XDG_RESERVED_ENTRY(get_base_name)
+#define _xdg_convert_to_ucs4 XDG_RESERVED_ENTRY(convert_to_ucs4)
+#define _xdg_reverse_ucs4    XDG_RESERVED_ENTRY(reverse_ucs4)
+#endif
+
+#define SWAP_BE16_TO_LE16(val) (xdg_uint16_t)(((xdg_uint16_t)(val) << 8)|((xdg_uint16_t)(val) >> 8))
+
+#define SWAP_BE32_TO_LE32(val) (xdg_uint32_t)((((xdg_uint32_t)(val) & 0xFF000000U) >> 24) |    \
+                                             (((xdg_uint32_t)(val) & 0x00FF0000U) >> 8) |      \
+                                             (((xdg_uint32_t)(val) & 0x0000FF00U) << 8) |      \
+                                             (((xdg_uint32_t)(val) & 0x000000FFU) << 24))
+/* UTF-8 utils
+ */
+extern const char *const _xdg_utf8_skip;
+#define _xdg_utf8_next_char(p) (char *)((p) + _xdg_utf8_skip[*(unsigned char *)(p)])
+#define _xdg_utf8_char_size(p) (int) (_xdg_utf8_skip[*(unsigned char *)(p)])
+
+xdg_unichar_t  _xdg_utf8_to_ucs4  (const char    *source);
+xdg_unichar_t  _xdg_ucs4_to_lower (xdg_unichar_t  source);
+int            _xdg_utf8_validate (const char    *source);
+xdg_unichar_t *_xdg_convert_to_ucs4 (const char *source, int *len);
+void           _xdg_reverse_ucs4 (xdg_unichar_t *source, int len);
+const char    *_xdg_get_base_name (const char    *file_name);
+
+#endif /* __XDG_MIME_INT_H__ */
diff --git a/xdgmime/src/xdgmimemagic.c b/xdgmime/src/xdgmimemagic.c
new file mode 100644 (file)
index 0000000..a2320f5
--- /dev/null
@@ -0,0 +1,813 @@
+/* -*- mode: C; c-file-style: "gnu" -*- */
+/* xdgmimemagic.: Private file.  Datastructure for storing magic files.
+ *
+ * More info can be found at http://www.freedesktop.org/standards/
+ *
+ * Copyright (C) 2003  Red Hat, Inc.
+ * Copyright (C) 2003  Jonathan Blandford <jrb@alum.mit.edu>
+ *
+ * Licensed under the Academic Free License version 2.0
+ * Or under the following terms:
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <assert.h>
+#include "xdgmimemagic.h"
+#include "xdgmimeint.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include <errno.h>
+#include <limits.h>
+
+#ifndef        FALSE
+#define        FALSE   (0)
+#endif
+
+#ifndef        TRUE
+#define        TRUE    (!FALSE)
+#endif
+
+#if !defined getc_unlocked && !defined HAVE_GETC_UNLOCKED
+# define getc_unlocked(fp) getc (fp)
+#endif
+
+typedef struct XdgMimeMagicMatch XdgMimeMagicMatch;
+typedef struct XdgMimeMagicMatchlet XdgMimeMagicMatchlet;
+
+typedef enum
+{
+  XDG_MIME_MAGIC_SECTION,
+  XDG_MIME_MAGIC_MAGIC,
+  XDG_MIME_MAGIC_ERROR,
+  XDG_MIME_MAGIC_EOF
+} XdgMimeMagicState;
+
+struct XdgMimeMagicMatch
+{
+  const char *mime_type;
+  int priority;
+  XdgMimeMagicMatchlet *matchlet;
+  XdgMimeMagicMatch *next;
+};
+
+
+struct XdgMimeMagicMatchlet
+{
+  int indent;
+  int offset;
+  unsigned int value_length;
+  unsigned char *value;
+  unsigned char *mask;
+  unsigned int range_length;
+  unsigned int word_size;
+  XdgMimeMagicMatchlet *next;
+};
+
+
+struct XdgMimeMagic
+{
+  XdgMimeMagicMatch *match_list;
+  int max_extent;
+};
+
+static XdgMimeMagicMatch *
+_xdg_mime_magic_match_new (void)
+{
+  return calloc (1, sizeof (XdgMimeMagicMatch));
+}
+
+
+static XdgMimeMagicMatchlet *
+_xdg_mime_magic_matchlet_new (void)
+{
+  XdgMimeMagicMatchlet *matchlet;
+
+  matchlet = malloc (sizeof (XdgMimeMagicMatchlet));
+
+  matchlet->indent = 0;
+  matchlet->offset = 0;
+  matchlet->value_length = 0;
+  matchlet->value = NULL;
+  matchlet->mask = NULL;
+  matchlet->range_length = 1;
+  matchlet->word_size = 1;
+  matchlet->next = NULL;
+
+  return matchlet;
+}
+
+
+static void
+_xdg_mime_magic_matchlet_free (XdgMimeMagicMatchlet *mime_magic_matchlet)
+{
+  if (mime_magic_matchlet)
+    {
+      if (mime_magic_matchlet->next)
+       _xdg_mime_magic_matchlet_free (mime_magic_matchlet->next);
+      if (mime_magic_matchlet->value)
+       free (mime_magic_matchlet->value);
+      if (mime_magic_matchlet->mask)
+       free (mime_magic_matchlet->mask);
+      free (mime_magic_matchlet);
+    }
+}
+
+
+/* Frees mime_magic_match and the remainder of its list
+ */
+static void
+_xdg_mime_magic_match_free (XdgMimeMagicMatch *mime_magic_match)
+{
+  XdgMimeMagicMatch *ptr, *next;
+
+  ptr = mime_magic_match;
+  while (ptr)
+    {
+      next = ptr->next;
+
+      if (ptr->mime_type)
+       free ((void *) ptr->mime_type);
+      if (ptr->matchlet)
+       _xdg_mime_magic_matchlet_free (ptr->matchlet);
+      free (ptr);
+
+      ptr = next;
+    }
+}
+
+/* Reads in a hunk of data until a newline character or a '\000' is hit.  The
+ * returned string is null terminated, and doesn't include the newline.
+ */
+static unsigned char *
+_xdg_mime_magic_read_to_newline (FILE *magic_file,
+                                int  *end_of_file)
+{
+  unsigned char *retval;
+  int c;
+  int len, pos;
+
+  len = 128;
+  pos = 0;
+  retval = malloc (len);
+  *end_of_file = FALSE;
+
+  while (TRUE)
+    {
+      c = getc_unlocked (magic_file);
+      if (c == EOF)
+       {
+         *end_of_file = TRUE;
+         break;
+       }
+      if (c == '\n' || c == '\000')
+       break;
+      retval[pos++] = (unsigned char) c;
+      if (pos % 128 == 127)
+       {
+         len = len + 128;
+         retval = realloc (retval, len);
+       }
+    }
+
+  retval[pos] = '\000';
+  return retval;
+}
+
+/* Returns the number read from the file, or -1 if no number could be read.
+ */
+static int
+_xdg_mime_magic_read_a_number (FILE *magic_file,
+                              int  *end_of_file)
+{
+  /* LONG_MAX is about 20 characters on my system */
+#define MAX_NUMBER_SIZE 30
+  char number_string[MAX_NUMBER_SIZE + 1];
+  int pos = 0;
+  int c;
+  long retval = -1;
+
+  while (TRUE)
+    {
+      c = getc_unlocked (magic_file);
+
+      if (c == EOF)
+       {
+         *end_of_file = TRUE;
+         break;
+       }
+      if (! isdigit (c))
+       {
+         ungetc (c, magic_file);
+         break;
+       }
+      number_string[pos] = (char) c;
+      pos++;
+      if (pos == MAX_NUMBER_SIZE)
+       break;
+    }
+  if (pos > 0)
+    {
+      number_string[pos] = '\000';
+      errno = 0;
+      retval = strtol (number_string, NULL, 10);
+
+      if ((retval < INT_MIN) || (retval > INT_MAX) || (errno != 0))
+       return -1;
+    }
+
+  return retval;
+}
+
+/* Headers are of the format:
+ * [<priority>:<mime-type>]
+ */
+static XdgMimeMagicState
+_xdg_mime_magic_parse_header (FILE *magic_file, XdgMimeMagicMatch *match)
+{
+  int c;
+  char *buffer;
+  char *end_ptr;
+  int end_of_file = 0;
+
+  assert (magic_file != NULL);
+  assert (match != NULL);
+
+  c = getc_unlocked (magic_file);
+  if (c == EOF)
+    return XDG_MIME_MAGIC_EOF;
+  if (c != '[')
+    return XDG_MIME_MAGIC_ERROR;
+
+  match->priority = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
+  if (end_of_file)
+    return XDG_MIME_MAGIC_EOF;
+  if (match->priority == -1)
+    return XDG_MIME_MAGIC_ERROR;
+
+  c = getc_unlocked (magic_file);
+  if (c == EOF)
+    return XDG_MIME_MAGIC_EOF;
+  if (c != ':')
+    return XDG_MIME_MAGIC_ERROR;
+
+  buffer = (char *)_xdg_mime_magic_read_to_newline (magic_file, &end_of_file);
+  if (end_of_file)
+    return XDG_MIME_MAGIC_EOF;
+
+  end_ptr = buffer;
+  while (*end_ptr != ']' && *end_ptr != '\000' && *end_ptr != '\n')
+    end_ptr++;
+  if (*end_ptr != ']')
+    {
+      free (buffer);
+      return XDG_MIME_MAGIC_ERROR;
+    }
+  *end_ptr = '\000';
+
+  match->mime_type = strdup (buffer);
+  free (buffer);
+
+  return XDG_MIME_MAGIC_MAGIC;
+}
+
+static XdgMimeMagicState
+_xdg_mime_magic_parse_error (FILE *magic_file)
+{
+  int c;
+
+  while (1)
+    {
+      c = getc_unlocked (magic_file);
+      if (c == EOF)
+       return XDG_MIME_MAGIC_EOF;
+      if (c == '\n')
+       return XDG_MIME_MAGIC_SECTION;
+    }
+}
+
+/* Headers are of the format:
+ * [ indent ] ">" start-offset "=" value
+ * [ "&" mask ] [ "~" word-size ] [ "+" range-length ] "\n"
+ */
+static XdgMimeMagicState
+_xdg_mime_magic_parse_magic_line (FILE              *magic_file,
+                                 XdgMimeMagicMatch *match)
+{
+  XdgMimeMagicMatchlet *matchlet;
+  int c;
+  int end_of_file;
+  int indent = 0;
+  int bytes_read;
+
+  assert (magic_file != NULL);
+
+  /* Sniff the buffer to make sure it's a valid line */
+  c = getc_unlocked (magic_file);
+  if (c == EOF)
+    return XDG_MIME_MAGIC_EOF;
+  else if (c == '[')
+    {
+      ungetc (c, magic_file);
+      return XDG_MIME_MAGIC_SECTION;
+    }
+  else if (c == '\n')
+    return XDG_MIME_MAGIC_MAGIC;
+
+  /* At this point, it must be a digit or a '>' */
+  end_of_file = FALSE;
+  if (isdigit (c))
+    {
+      ungetc (c, magic_file);
+      indent = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
+      if (end_of_file)
+       return XDG_MIME_MAGIC_EOF;
+      if (indent == -1)
+       return XDG_MIME_MAGIC_ERROR;
+      c = getc_unlocked (magic_file);
+      if (c == EOF)
+       return XDG_MIME_MAGIC_EOF;
+    }
+
+  if (c != '>')
+    return XDG_MIME_MAGIC_ERROR;
+
+  matchlet = _xdg_mime_magic_matchlet_new ();
+  matchlet->indent = indent;
+  matchlet->offset = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
+  if (end_of_file)
+    {
+      _xdg_mime_magic_matchlet_free (matchlet);
+      return XDG_MIME_MAGIC_EOF;
+    }
+  if (matchlet->offset == -1)
+    {
+      _xdg_mime_magic_matchlet_free (matchlet);
+      return XDG_MIME_MAGIC_ERROR;
+    }
+  c = getc_unlocked (magic_file);
+  if (c == EOF)
+    {
+      _xdg_mime_magic_matchlet_free (matchlet);
+      return XDG_MIME_MAGIC_EOF;
+    }
+  else if (c != '=')
+    {
+      _xdg_mime_magic_matchlet_free (matchlet);
+      return XDG_MIME_MAGIC_ERROR;
+    }
+
+  /* Next two bytes determine how long the value is */
+  matchlet->value_length = 0;
+  c = getc_unlocked (magic_file);
+  if (c == EOF)
+    {
+      _xdg_mime_magic_matchlet_free (matchlet);
+      return XDG_MIME_MAGIC_EOF;
+    }
+  matchlet->value_length = c & 0xFF;
+  matchlet->value_length = matchlet->value_length << 8;
+
+  c = getc_unlocked (magic_file);
+  if (c == EOF)
+    {
+      _xdg_mime_magic_matchlet_free (matchlet);
+      return XDG_MIME_MAGIC_EOF;
+    }
+  matchlet->value_length = matchlet->value_length + (c & 0xFF);
+
+  matchlet->value = malloc (matchlet->value_length);
+
+  /* OOM */
+  if (matchlet->value == NULL)
+    {
+      _xdg_mime_magic_matchlet_free (matchlet);
+      return XDG_MIME_MAGIC_ERROR;
+    }
+  bytes_read = fread (matchlet->value, 1, matchlet->value_length, magic_file);
+  if (bytes_read != matchlet->value_length)
+    {
+      _xdg_mime_magic_matchlet_free (matchlet);
+      if (feof (magic_file))
+       return XDG_MIME_MAGIC_EOF;
+      else
+       return XDG_MIME_MAGIC_ERROR;
+    }
+
+  c = getc_unlocked (magic_file);
+  if (c == '&')
+    {
+      matchlet->mask = malloc (matchlet->value_length);
+      /* OOM */
+      if (matchlet->mask == NULL)
+       {
+         _xdg_mime_magic_matchlet_free (matchlet);
+         return XDG_MIME_MAGIC_ERROR;
+       }
+      bytes_read = fread (matchlet->mask, 1, matchlet->value_length, magic_file);
+      if (bytes_read != matchlet->value_length)
+       {
+         _xdg_mime_magic_matchlet_free (matchlet);
+         if (feof (magic_file))
+           return XDG_MIME_MAGIC_EOF;
+         else
+           return XDG_MIME_MAGIC_ERROR;
+       }
+      c = getc_unlocked (magic_file);
+    }
+
+  if (c == '~')
+    {
+      matchlet->word_size = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
+      if (end_of_file)
+       {
+         _xdg_mime_magic_matchlet_free (matchlet);
+         return XDG_MIME_MAGIC_EOF;
+       }
+      if (matchlet->word_size != 0 &&
+         matchlet->word_size != 1 &&
+         matchlet->word_size != 2 &&
+         matchlet->word_size != 4)
+       {
+         _xdg_mime_magic_matchlet_free (matchlet);
+         return XDG_MIME_MAGIC_ERROR;
+       }
+      c = getc_unlocked (magic_file);
+    }
+
+  if (c == '+')
+    {
+      matchlet->range_length = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
+      if (end_of_file)
+       {
+         _xdg_mime_magic_matchlet_free (matchlet);
+         return XDG_MIME_MAGIC_EOF;
+       }
+      if (matchlet->range_length == -1)
+       {
+         _xdg_mime_magic_matchlet_free (matchlet);
+         return XDG_MIME_MAGIC_ERROR;
+       }
+      c = getc_unlocked (magic_file);
+    }
+
+
+  if (c == '\n')
+    {
+      /* We clean up the matchlet, byte swapping if needed */
+      if (matchlet->word_size > 1)
+       {
+         int i;
+         if (matchlet->value_length % matchlet->word_size != 0)
+           {
+             _xdg_mime_magic_matchlet_free (matchlet);
+             return XDG_MIME_MAGIC_ERROR;
+           }
+         /* FIXME: need to get this defined in a <config.h> style file */
+#if LITTLE_ENDIAN
+         for (i = 0; i < matchlet->value_length; i = i + matchlet->word_size)
+           {
+             if (matchlet->word_size == 2)
+               *((xdg_uint16_t *) matchlet->value + i) = SWAP_BE16_TO_LE16 (*((xdg_uint16_t *) (matchlet->value + i)));
+             else if (matchlet->word_size == 4)
+               *((xdg_uint32_t *) matchlet->value + i) = SWAP_BE32_TO_LE32 (*((xdg_uint32_t *) (matchlet->value + i)));
+             if (matchlet->mask)
+               {
+                 if (matchlet->word_size == 2)
+                   *((xdg_uint16_t *) matchlet->mask + i) = SWAP_BE16_TO_LE16 (*((xdg_uint16_t *) (matchlet->mask + i)));
+                 else if (matchlet->word_size == 4)
+                   *((xdg_uint32_t *) matchlet->mask + i) = SWAP_BE32_TO_LE32 (*((xdg_uint32_t *) (matchlet->mask + i)));
+
+               }
+           }
+#endif
+       }
+
+      matchlet->next = match->matchlet;
+      match->matchlet = matchlet;
+
+
+      return XDG_MIME_MAGIC_MAGIC;
+    }
+
+  _xdg_mime_magic_matchlet_free (matchlet);
+  if (c == EOF)
+    return XDG_MIME_MAGIC_EOF;
+
+  return XDG_MIME_MAGIC_ERROR;
+}
+
+static int
+_xdg_mime_magic_matchlet_compare_to_data (XdgMimeMagicMatchlet *matchlet,
+                                         const void           *data,
+                                         size_t                len)
+{
+  int i, j;
+  for (i = matchlet->offset; i < matchlet->offset + matchlet->range_length; i++)
+    {
+      int valid_matchlet = TRUE;
+
+      if (i + matchlet->value_length > len)
+       return FALSE;
+
+      if (matchlet->mask)
+       {
+         for (j = 0; j < matchlet->value_length; j++)
+           {
+             if ((matchlet->value[j] & matchlet->mask[j]) !=
+                 ((((unsigned char *) data)[j + i]) & matchlet->mask[j]))
+               {
+                 valid_matchlet = FALSE;
+                 break;
+               }
+           }
+       }
+      else
+       {
+         for (j = 0; j <  matchlet->value_length; j++)
+           {
+             if (matchlet->value[j] != ((unsigned char *) data)[j + i])
+               {
+                 valid_matchlet = FALSE;
+                 break;
+               }
+           }
+       }
+      if (valid_matchlet)
+       return TRUE;
+    }
+  return FALSE;
+}
+
+static int
+_xdg_mime_magic_matchlet_compare_level (XdgMimeMagicMatchlet *matchlet,
+                                       const void           *data,
+                                       size_t                len,
+                                       int                   indent)
+{
+  while ((matchlet != NULL) && (matchlet->indent == indent))
+    {
+      if (_xdg_mime_magic_matchlet_compare_to_data (matchlet, data, len))
+       {
+         if ((matchlet->next == NULL) ||
+             (matchlet->next->indent <= indent))
+           return TRUE;
+
+         if (_xdg_mime_magic_matchlet_compare_level (matchlet->next,
+                                                     data,
+                                                     len,
+                                                     indent + 1))
+           return TRUE;
+       }
+
+      do
+       {
+         matchlet = matchlet->next;
+       }
+      while (matchlet && matchlet->indent > indent);
+    }
+
+  return FALSE;
+}
+
+static int
+_xdg_mime_magic_match_compare_to_data (XdgMimeMagicMatch *match,
+                                      const void        *data,
+                                      size_t             len)
+{
+  return _xdg_mime_magic_matchlet_compare_level (match->matchlet, data, len, 0);
+}
+
+static void
+_xdg_mime_magic_insert_match (XdgMimeMagic      *mime_magic,
+                             XdgMimeMagicMatch *match)
+{
+  XdgMimeMagicMatch *list;
+
+  if (mime_magic->match_list == NULL)
+    {
+      mime_magic->match_list = match;
+      return;
+    }
+
+  if (match->priority > mime_magic->match_list->priority)
+    {
+      match->next = mime_magic->match_list;
+      mime_magic->match_list = match;
+      return;
+    }
+
+  list = mime_magic->match_list;
+  while (list->next != NULL)
+    {
+      if (list->next->priority < match->priority)
+       {
+         match->next = list->next;
+         list->next = match;
+         return;
+       }
+      list = list->next;
+    }
+  list->next = match;
+  match->next = NULL;
+}
+
+XdgMimeMagic *
+_xdg_mime_magic_new (void)
+{
+  return calloc (1, sizeof (XdgMimeMagic));
+}
+
+void
+_xdg_mime_magic_free (XdgMimeMagic *mime_magic)
+{
+  if (mime_magic) {
+    _xdg_mime_magic_match_free (mime_magic->match_list);
+    free (mime_magic);
+  }
+}
+
+int
+_xdg_mime_magic_get_buffer_extents (XdgMimeMagic *mime_magic)
+{
+  return mime_magic->max_extent;
+}
+
+const char *
+_xdg_mime_magic_lookup_data (XdgMimeMagic *mime_magic,
+                            const void   *data,
+                            size_t        len,
+                            int           *result_prio,
+                             const char   *mime_types[],
+                             int           n_mime_types)
+{
+  XdgMimeMagicMatch *match;
+  const char *mime_type;
+  int n;
+  int prio;
+
+  prio = 0;
+  mime_type = NULL;
+  for (match = mime_magic->match_list; match; match = match->next)
+    {
+      if (_xdg_mime_magic_match_compare_to_data (match, data, len))
+       {
+         prio = match->priority;
+         mime_type = match->mime_type;
+         break;
+       }
+      else 
+       {
+         for (n = 0; n < n_mime_types; n++)
+           {
+             if (mime_types[n] && 
+                 _xdg_mime_mime_type_equal (mime_types[n], match->mime_type))
+               mime_types[n] = NULL;
+           }
+       }
+    }
+
+  if (mime_type == NULL)
+    {
+      for (n = 0; n < n_mime_types; n++)
+       {
+         if (mime_types[n])
+           mime_type = mime_types[n];
+       }
+    }
+  
+  if (result_prio)
+    *result_prio = prio;
+
+  return mime_type;
+}
+
+static void
+_xdg_mime_update_mime_magic_extents (XdgMimeMagic *mime_magic)
+{
+  XdgMimeMagicMatch *match;
+  int max_extent = 0;
+
+  for (match = mime_magic->match_list; match; match = match->next)
+    {
+      XdgMimeMagicMatchlet *matchlet;
+
+      for (matchlet = match->matchlet; matchlet; matchlet = matchlet->next)
+       {
+         int extent;
+
+         extent = matchlet->value_length + matchlet->offset + matchlet->range_length;
+         if (max_extent < extent)
+           max_extent = extent;
+       }
+    }
+
+  mime_magic->max_extent = max_extent;
+}
+
+static XdgMimeMagicMatchlet *
+_xdg_mime_magic_matchlet_mirror (XdgMimeMagicMatchlet *matchlets)
+{
+  XdgMimeMagicMatchlet *new_list;
+  XdgMimeMagicMatchlet *tmp;
+
+  if ((matchlets == NULL) || (matchlets->next == NULL))
+    return matchlets;
+
+  new_list = NULL;
+  tmp = matchlets;
+  while (tmp != NULL)
+    {
+      XdgMimeMagicMatchlet *matchlet;
+
+      matchlet = tmp;
+      tmp = tmp->next;
+      matchlet->next = new_list;
+      new_list = matchlet;
+    }
+
+  return new_list;
+
+}
+
+static void
+_xdg_mime_magic_read_magic_file (XdgMimeMagic *mime_magic,
+                                FILE         *magic_file)
+{
+  XdgMimeMagicState state;
+  XdgMimeMagicMatch *match = NULL; /* Quiet compiler */
+
+  state = XDG_MIME_MAGIC_SECTION;
+
+  while (state != XDG_MIME_MAGIC_EOF)
+    {
+      switch (state)
+       {
+       case XDG_MIME_MAGIC_SECTION:
+         match = _xdg_mime_magic_match_new ();
+         state = _xdg_mime_magic_parse_header (magic_file, match);
+         if (state == XDG_MIME_MAGIC_EOF || state == XDG_MIME_MAGIC_ERROR)
+           _xdg_mime_magic_match_free (match);
+         break;
+       case XDG_MIME_MAGIC_MAGIC:
+         state = _xdg_mime_magic_parse_magic_line (magic_file, match);
+         if (state == XDG_MIME_MAGIC_SECTION ||
+             (state == XDG_MIME_MAGIC_EOF && match->mime_type))
+           {
+             match->matchlet = _xdg_mime_magic_matchlet_mirror (match->matchlet);
+             _xdg_mime_magic_insert_match (mime_magic, match);
+           }
+         else if (state == XDG_MIME_MAGIC_EOF || state == XDG_MIME_MAGIC_ERROR)
+           _xdg_mime_magic_match_free (match);
+         break;
+       case XDG_MIME_MAGIC_ERROR:
+         state = _xdg_mime_magic_parse_error (magic_file);
+         break;
+       case XDG_MIME_MAGIC_EOF:
+       default:
+         /* Make the compiler happy */
+         assert (0);
+       }
+    }
+  _xdg_mime_update_mime_magic_extents (mime_magic);
+}
+
+void
+_xdg_mime_magic_read_from_file (XdgMimeMagic *mime_magic,
+                               const char   *file_name)
+{
+  FILE *magic_file;
+  char header[12];
+
+  magic_file = fopen (file_name, "r");
+
+  if (magic_file == NULL)
+    return;
+
+  if (fread (header, 1, 12, magic_file) == 12)
+    {
+      if (memcmp ("MIME-Magic\0\n", header, 12) == 0)
+        _xdg_mime_magic_read_magic_file (mime_magic, magic_file);
+    }
+
+  fclose (magic_file);
+}
diff --git a/xdgmime/src/xdgmimemagic.h b/xdgmime/src/xdgmimemagic.h
new file mode 100644 (file)
index 0000000..35c8039
--- /dev/null
@@ -0,0 +1,57 @@
+/* -*- mode: C; c-file-style: "gnu" -*- */
+/* xdgmimemagic.h: Private file.  Datastructure for storing the magic files.
+ *
+ * More info can be found at http://www.freedesktop.org/standards/
+ *
+ * Copyright (C) 2003  Red Hat, Inc.
+ * Copyright (C) 2003  Jonathan Blandford <jrb@alum.mit.edu>
+ *
+ * Licensed under the Academic Free License version 2.0
+ * Or under the following terms:
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __XDG_MIME_MAGIC_H__
+#define __XDG_MIME_MAGIC_H__
+
+#include <unistd.h>
+#include "xdgmime.h"
+typedef struct XdgMimeMagic XdgMimeMagic;
+
+#ifdef XDG_PREFIX
+#define _xdg_mime_glob_read_from_file             XDG_RESERVED_ENTRY(glob_read_from_file)
+#define _xdg_mime_magic_new                       XDG_RESERVED_ENTRY(magic_new)
+#define _xdg_mime_magic_read_from_file            XDG_RESERVED_ENTRY(magic_read_from_file)
+#define _xdg_mime_magic_free                      XDG_RESERVED_ENTRY(magic_free)
+#define _xdg_mime_magic_get_buffer_extents        XDG_RESERVED_ENTRY(magic_get_buffer_extents)
+#define _xdg_mime_magic_lookup_data               XDG_RESERVED_ENTRY(magic_lookup_data)
+#endif
+
+
+XdgMimeMagic *_xdg_mime_magic_new                (void);
+void          _xdg_mime_magic_read_from_file     (XdgMimeMagic *mime_magic,
+                                                 const char   *file_name);
+void          _xdg_mime_magic_free               (XdgMimeMagic *mime_magic);
+int           _xdg_mime_magic_get_buffer_extents (XdgMimeMagic *mime_magic);
+const char   *_xdg_mime_magic_lookup_data        (XdgMimeMagic *mime_magic,
+                                                 const void   *data,
+                                                 size_t        len,
+                                                 int          *result_prio,
+                                                 const char   *mime_types[],
+                                                 int           n_mime_types);
+
+#endif /* __XDG_MIME_MAGIC_H__ */
diff --git a/xdgmime/src/xdgmimeparent.c b/xdgmime/src/xdgmimeparent.c
new file mode 100644 (file)
index 0000000..511bbac
--- /dev/null
@@ -0,0 +1,219 @@
+/* -*- mode: C; c-file-style: "gnu" -*- */
+/* xdgmimealias.c: Private file.  Datastructure for storing the hierarchy.
+ *
+ * More info can be found at http://www.freedesktop.org/standards/
+ *
+ * Copyright (C) 2004  Red Hat, Inc.
+ * Copyright (C) 2004  Matthias Clasen <mclasen@redhat.com>
+ *
+ * Licensed under the Academic Free License version 2.0
+ * Or under the following terms:
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "xdgmimeparent.h"
+#include "xdgmimeint.h"
+#include <stdlib.h>
+#include <stdio.h>
+#include <assert.h>
+#include <string.h>
+#include <fnmatch.h>
+
+#ifndef        FALSE
+#define        FALSE   (0)
+#endif
+
+#ifndef        TRUE
+#define        TRUE    (!FALSE)
+#endif
+
+typedef struct XdgMimeParents XdgMimeParents;
+
+struct XdgMimeParents
+{
+  char *mime;
+  char **parents;
+  int n_parents;
+};
+
+struct XdgParentList
+{
+  struct XdgMimeParents *parents;
+  int n_mimes;
+};
+
+XdgParentList *
+_xdg_mime_parent_list_new (void)
+{
+  XdgParentList *list;
+
+  list = malloc (sizeof (XdgParentList));
+
+  list->parents = NULL;
+  list->n_mimes = 0;
+
+  return list;
+}
+
+void         
+_xdg_mime_parent_list_free (XdgParentList *list)
+{
+  int i;
+  char **p;
+
+  if (list->parents)
+    {
+      for (i = 0; i < list->n_mimes; i++)
+       {
+         for (p = list->parents[i].parents; *p; p++)
+           free (*p);
+
+         free (list->parents[i].parents);
+         free (list->parents[i].mime);
+       }
+      free (list->parents);
+    }
+  free (list);
+}
+
+static int
+parent_entry_cmp (const void *v1, const void *v2)
+{
+  return strcmp (((XdgMimeParents *)v1)->mime, ((XdgMimeParents *)v2)->mime);
+}
+
+const char **
+_xdg_mime_parent_list_lookup (XdgParentList *list,
+                             const char    *mime)
+{
+  XdgMimeParents *entry;
+  XdgMimeParents key;
+
+  if (list->n_mimes > 0)
+    {
+      key.mime = (char *)mime;
+      key.parents = NULL;
+
+      entry = bsearch (&key, list->parents, list->n_mimes,
+                      sizeof (XdgMimeParents), &parent_entry_cmp);
+      if (entry)
+        return (const char **)entry->parents;
+    }
+
+  return NULL;
+}
+
+void
+_xdg_mime_parent_read_from_file (XdgParentList *list,
+                                const char    *file_name)
+{
+  FILE *file;
+  char line[255];
+  int i, alloc;
+  XdgMimeParents *entry;
+
+  file = fopen (file_name, "r");
+
+  if (file == NULL)
+    return;
+
+  /* FIXME: Not UTF-8 safe.  Doesn't work if lines are greater than 255 chars.
+   * Blah */
+  alloc = list->n_mimes + 16;
+  list->parents = realloc (list->parents, alloc * sizeof (XdgMimeParents));
+  while (fgets (line, 255, file) != NULL)
+    {
+      char *sep;
+      if (line[0] == '#')
+       continue;
+
+      sep = strchr (line, ' ');
+      if (sep == NULL)
+       continue;
+      *(sep++) = '\000';
+      sep[strlen (sep) -1] = '\000';
+      entry = NULL;
+      for (i = 0; i < list->n_mimes; i++)
+       {
+         if (strcmp (list->parents[i].mime, line) == 0)
+           {
+             entry = &(list->parents[i]);
+             break;
+           }
+       }
+      
+      if (!entry)
+       {
+         if (list->n_mimes == alloc)
+           {
+             alloc <<= 1;
+             list->parents = realloc (list->parents, 
+                                      alloc * sizeof (XdgMimeParents));
+           }
+         list->parents[list->n_mimes].mime = strdup (line);
+         list->parents[list->n_mimes].parents = NULL;
+         entry = &(list->parents[list->n_mimes]);
+         list->n_mimes++;
+       }
+
+      if (!entry->parents)
+       {
+         entry->n_parents = 1;
+         entry->parents = malloc ((entry->n_parents + 1) * sizeof (char *));
+       }
+      else
+       {
+         entry->n_parents += 1;
+         entry->parents = realloc (entry->parents, 
+                                   (entry->n_parents + 2) * sizeof (char *));
+       }
+      entry->parents[entry->n_parents - 1] = strdup (sep);
+      entry->parents[entry->n_parents] = NULL;
+    }
+
+  list->parents = realloc (list->parents, 
+                          list->n_mimes * sizeof (XdgMimeParents));
+
+  fclose (file);  
+  
+  if (list->n_mimes > 1)
+    qsort (list->parents, list->n_mimes, 
+           sizeof (XdgMimeParents), &parent_entry_cmp);
+}
+
+
+void         
+_xdg_mime_parent_list_dump (XdgParentList *list)
+{
+  int i;
+  char **p;
+
+  if (list->parents)
+    {
+      for (i = 0; i < list->n_mimes; i++)
+       {
+         for (p = list->parents[i].parents; *p; p++)
+           printf ("%s %s\n", list->parents[i].mime, *p);
+       }
+    }
+}
+
+
diff --git a/xdgmime/src/xdgmimeparent.h b/xdgmime/src/xdgmimeparent.h
new file mode 100644 (file)
index 0000000..b564f41
--- /dev/null
@@ -0,0 +1,51 @@
+/* -*- mode: C; c-file-style: "gnu" -*- */
+/* xdgmimeparent.h: Private file.  Datastructure for storing the hierarchy.
+ *
+ * More info can be found at http://www.freedesktop.org/standards/
+ *
+ * Copyright (C) 2004  Red Hat, Inc.
+ * Copyright (C) 200  Matthias Clasen <mclasen@redhat.com>
+ *
+ * Licensed under the Academic Free License version 2.0
+ * Or under the following terms:
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __XDG_MIME_PARENT_H__
+#define __XDG_MIME_PARENT_H__
+
+#include "xdgmime.h"
+
+typedef struct XdgParentList XdgParentList;
+
+#ifdef XDG_PREFIX
+#define _xdg_mime_parent_read_from_file        XDG_RESERVED_ENTRY(parent_read_from_file)
+#define _xdg_mime_parent_list_new              XDG_RESERVED_ENTRY(parent_list_new)
+#define _xdg_mime_parent_list_free             XDG_RESERVED_ENTRY(parent_list_free)
+#define _xdg_mime_parent_list_lookup           XDG_RESERVED_ENTRY(parent_list_lookup)
+#define _xdg_mime_parent_list_dump             XDG_RESERVED_ENTRY(parent_list_dump)
+#endif
+
+void          _xdg_mime_parent_read_from_file (XdgParentList *list,
+                                              const char    *file_name);
+XdgParentList *_xdg_mime_parent_list_new       (void);
+void           _xdg_mime_parent_list_free      (XdgParentList *list);
+const char   **_xdg_mime_parent_list_lookup    (XdgParentList *list,
+                                               const char    *mime);
+void           _xdg_mime_parent_list_dump      (XdgParentList *list);
+
+#endif /* __XDG_MIME_PARENT_H__ */