From: maritus Date: Fri, 9 Feb 2007 12:56:05 +0000 (+0000) Subject: Added sqlite3 bookmarks X-Git-Tag: 0.4~298 X-Git-Url: http://vcs.maemo.org/git/?a=commitdiff_plain;h=c9e0f92301d6093cfcfe5e73362a4cd09760a899;p=mdictionary Added sqlite3 bookmarks git-svn-id: file:///svnroot/mdictionary/trunk@69 5bde0345-f819-0410-ac75-e5045f9217cc --- diff --git a/src/bookmarks/sql3/COPYING b/src/bookmarks/sql3/COPYING new file mode 100644 index 0000000..fb6319b --- /dev/null +++ b/src/bookmarks/sql3/COPYING @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 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. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, 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 or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +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 give any other recipients of the Program a copy of this License +along with the Program. + +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 Program or any portion +of it, thus forming a work based on the Program, 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) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +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 Program, 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 Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) 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; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, 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 executable. However, as a +special exception, the source code 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. + +If distribution of executable or 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 counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program 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. + + 5. 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 Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program 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 to +this License. + + 7. 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 Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program 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 Program. + +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. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program 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. + + 9. The Free Software Foundation may publish revised and/or new versions +of the 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 Program +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 Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, 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 + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "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 PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. 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 PROGRAM 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 PROGRAM (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 PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), 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 Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. 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. + + + Copyright (C) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; 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. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. \ No newline at end of file diff --git a/src/bookmarks/sql3/Makefile b/src/bookmarks/sql3/Makefile new file mode 100644 index 0000000..744e9f2 --- /dev/null +++ b/src/bookmarks/sql3/Makefile @@ -0,0 +1,48 @@ +COMPILER = gcc +DEBUG = -Wall -g +SOURCES = ./src +INCLUDE = include +INCLUDE_ENGINE = ../../../include +LIBS = `pkg-config --libs glib-2.0 gmodule-2.0` -lsqlite3 +FLAGS = `pkg-config --cflags glib-2.0 gmodule-2.0` -I${INCLUDE} -I${INCLUDE_ENGINE} ${DEBUG} +BINARIES = bin +FINAL = ../../../bin/ws_bookmark.so + + +${FINAL}: ${BINARIES}/bookmark.o ${SOURCES}/engine_bookmark.c + @gcc -shared -fPIC ${FLAGS} ${LIBS} ${BINARIES}/bookmark.o -o ${FINAL} + +${BINARIES}/bookmark.o: ${SOURCES}/engine_bookmark.c + @gcc -c -fPIC -o ${BINARIES}/bookmark.o ${SOURCES}/engine_bookmark.c ${FLAGS} + +clean: + @-rm -f ${BINARIES}/* + @-rm -f ${FINAL} + @echo -e -n "Project's 'BookmarkEngine' directories has been cleaned.\n" + +cleandata: + @-rm -f ws_bookmarks-journal + @-rm -f ws_bookmarks + + +data: ${SOURCES}/testMakeDatabase.c + @echo -e -n "Compiling testDatabase...\n" + @gcc -o ${SOURCES}/testDatabase ${SOURCES}/testMakeDatabase.c ${FLAGS} ${LIBS} + +datarun: data + @run-standalone.sh src/testDatabase + +test: test.o + @echo "Linking test program..." + @gcc ${LIBS} ${DEBUG} -o ${SOURCES}/test ${BINARIES}/test.o + @echo -e "Building test program finished.\n" + +test.o: + @echo "Compiling test program..." + @gcc ${FLAGS} ${DEBUG} -c -o ${BINARIES}/test.o ${SOURCES}/test.c + +check: + @echo "Running test program:" + @run-standalone.sh ${SOURCES}/test + + \ No newline at end of file diff --git a/src/bookmarks/sql3/include/engine_bookmark.h b/src/bookmarks/sql3/include/engine_bookmark.h new file mode 100644 index 0000000..ecea43f --- /dev/null +++ b/src/bookmarks/sql3/include/engine_bookmark.h @@ -0,0 +1,210 @@ +/******************************************************************************* +This file is part of WhiteStork. + +WhiteStork is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +WhiteStork 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 General Public License for more details. + +You should have received a copy of the GNU General Public License +along with WhiteStork; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +Copyright 2006 ComArch S.A. +*******************************************************************************/ +#ifndef _DICTIONARY_ENGINE_BOOKMARK +#define _DICTIONARY_ENGINE_BOOKMARK + +#ifdef __cplusplus + extern "C" { +#endif + +//______________________________________________________________________________ +// ***************************************************************************** +//************************************************************* HEADERS SECTION: +//------------------------------------------------------------------------------ +// headers with unix types/functions - only for timers +#include +#include +#include +#include +#include +//------------------------------------------------------------------------------ +// header with GLIB definitions/functions/types +#include +//------------------------------------------------------------------------------ +// header wit engine API +#include "dictionary_engine.h" +//------------------------------------------------------------------------------ +// header wit sqlite 2.x API +#include +//------------------------------------------------------------------------------ + +//______________________________________________________________________________ +// ***************************************************************************** +//********************************************************* DEFINITIONS SECTION: +//------------------------------------------------------------------------------ +// definitions for timer function - flag telling if we want to start or stop +// timing +#define TIMER_START TRUE +#define TIMER_STOP FALSE +//------------------------------------------------------------------------------ +// definitions of version and format which engine handles +static const gchar* DIC_ENG_VERSION = "0.1"; +static const gchar* DIC_ENG_FORMAT = "Users' Bookmarks"; +static const gchar* DIC_ENG_DESCRIPTION = "This module handles users' bookmarks."; + +//------------------------------------------------------------------------------ +// macro for "printing" gboolean statement - "TRUE" or "FALSE" +#define PRINT_STATE(state) ( (state) ? "TRUE" : "FALSE" ) +//------------------------------------------------------------------------------ + + +//______________________________________________________________________________ +// ***************************************************************************** +//****************************************** DATA STRUCTURE DEFINITIONS SECTION: +//------------------------------------------------------------------------------ +/** \brief Internal data structure of XDXF Engine. + */ +struct _BookData { + sqlite3* db; + + gchar* dict_path; + EngineStatus last_error; + gboolean auto_free; + + cb_progress cb_progress_caching; + gpointer cb_progress_caching_data; + gdouble cb_progress_caching_seed; + + cb_progress cb_progress_word_list; + gpointer cb_progress_word_list_data; + gdouble cb_progress_word_list_seed; + + cb_progress cb_progress_word_trans; + gpointer cb_progress_word_trans_data; + gdouble cb_progress_word_trans_seed; + + cb_word_list cb_search_word_list; + gpointer cb_search_word_list_data; + + cb_word_translation cb_search_word_trans; + gpointer cb_search_word_trans_data; +}; +typedef struct _BookData BookData; +//------------------------------------------------------------------------------ + + +//______________________________________________________________________________ +// ***************************************************************************** +//************************************************ ADDITIONAL FUNCTIONS SECTION: +//------------------------------------------------------------------------------ +// returning concrete part of file +//static gchar* read_file_part(FilePart* part, GnomeVFSHandle* file); +//------------------------------------------------------------------------------ +// convert string to proper path name (no filename, no "/" at the ned, file +// exist) +static gchar* string_to_path(gchar** string); +//------------------------------------------------------------------------------ +// tells if file is in XDXF format (file should exist) +//static gboolean is_xdxf_file(gchar* file); +//------------------------------------------------------------------------------ +// start/stop timers - returnet -1.0 if we start or seconds passed from start +// if we want to stop timer +static double timer(gboolean start, gchar* message); +//------------------------------------------------------------------------------ + + +//______________________________________________________________________________ +// ***************************************************************************** +//****************************************************** MAIN FUNCTIONS SECTION: +//------------------------------------------------------------------------------ +gboolean bm_engine_add_word(Engine* engine, + gchar* word, + gchar* translation); +//------------------------------------------------------------------------------ +gboolean bm_engine_remove_word(Engine* engine, + gchar* word); +//------------------------------------------------------------------------------ +gchar* bm_engine_get_lang_from(Engine* engine); +//------------------------------------------------------------------------------ +gchar* bm_engine_get_lang_to(Engine* engine); +//------------------------------------------------------------------------------ +gchar* bm_engine_get_title(Engine* engine); +//------------------------------------------------------------------------------ +gchar* bm_engine_get_icon_path(Engine* engine); + + + +//------------------------------------------------------------------------------ +// implementation of dict_eng_module_check(module,location) function +gboolean bm_engine_check(gchar* location); +//------------------------------------------------------------------------------ +// implementation of dict_eng_module_get_description(module) function +gchar* bm_engine_description(); +//------------------------------------------------------------------------------ +// implementation of dict_eng_module_get_format(module) function +gchar* bm_engine_format(); +//------------------------------------------------------------------------------ +// implementation of dict_eng_module_get_version(module) function +gchar* bm_engine_version(); +//------------------------------------------------------------------------------ +// implementation of dict_eng_module_create(module,location,flags) and +// dict_eng_module_create_ext(module,location,flags) functions +Engine* bm_engine_create(gchar* location, + EngineOptimizationFlag flags, + cb_progress progress_handler, + gpointer progress_data, + gdouble seed); +//------------------------------------------------------------------------------ +// implementation of dict_eng_destroy(engine) function +void bm_engine_close(Engine* engine); +//------------------------------------------------------------------------------ +// implementation of dict_eng_get_location(engine) function +gchar* bm_engine_location(Engine* engine); +//------------------------------------------------------------------------------ +// implementation of dict_eng_optimize(engine) function +void bm_engine_optimize(Engine* engine); +//------------------------------------------------------------------------------ +// implementation of dict_eng_is_optimized( engine ) function +gboolean bm_engine_is_optimized(Engine* engine); +//------------------------------------------------------------------------------ +// implementation of dict_eng_set_auto_free(engine, state) function +void bm_engine_set_auto_free(Engine* engine, gboolean state); +//------------------------------------------------------------------------------ +// implementation of dict_eng_set_callback(engine,signal,c_handler,data) +// function +gpointer bm_engine_set_callback(Engine* engine, + gchar* event, + gpointer c_handler, + gpointer user_data); +//------------------------------------------------------------------------------ +// implementation of dict_eng_set_progress_seed(engine, signal, val) function +void bm_engine_set_progress_seed(Engine* engine, + gchar* signal, + gdouble seed); +//------------------------------------------------------------------------------ +// implementation ofdict_eng_search_word_list(engine,pattern) function +void bm_engine_search_word_list(Engine* engine, gchar* pattern); +//------------------------------------------------------------------------------ +// implementation of dict_eng_search_word_translation(engine,word) function +void bm_engine_search_word_translation(Engine* engine, gchar* word); +//------------------------------------------------------------------------------ +// implementation of dict_eng_get_last_state(engine) function +EngineStatus bm_engine_status(Engine* engine); +//------------------------------------------------------------------------------ +// implementation of dict_eng_state_message(error) function +gchar* bm_engine_status_message(EngineStatus error); +//------------------------------------------------------------------------------ +// implementation of engine_global_functions(void) function +EngineModule engine_global_functions(); + +#ifdef __cplusplus +} +#endif +#endif /* #ifndef _DICTIONARY_ENGINE_STARDICT */ diff --git a/src/bookmarks/sql3/src/engine_bookmark.c b/src/bookmarks/sql3/src/engine_bookmark.c new file mode 100644 index 0000000..a3cba68 --- /dev/null +++ b/src/bookmarks/sql3/src/engine_bookmark.c @@ -0,0 +1,1020 @@ +/****************************************************************************** +This file is part of WhiteStork. + +WhiteStork is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +WhiteStork 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 General Public License for more details. + +You should have received a copy of the GNU General Public License +along with WhiteStork; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +Copyright 2006 ComArch S.A. +******************************************************************************/ + +// header with data structure and function definition for XDXF engine. +// Also Engine API. +#include +//------------------------------------------------------------------------------ + +#ifndef NOLOGS +#include +#include +#include +#endif + +#define LOGS g_debug + +/*inline void LOGS(gchar* frm, ...) { +#ifndef NOLOGS + //g_printf(frm); +#endif +}*/ + +//============================================================================== +//============================================================================== +//============================================================================== +//-------------------------------------- FUNCTION FROM API 0.2 !!! +//============================================================================== +int get_id(void* data,int n,char** argv,char** names) { + int len = strlen(&(argv[0][0])); + memcpy(data,&(argv[0][0]), len); + gchar* tmp = (gchar*)data; + tmp[len] = 0; + return 0; +} + +gboolean bm_engine_add_word(Engine* engine, + gchar* word, + gchar* translation) { + gint sql_res = 0; + gchar* err = NULL; + gboolean result = TRUE; + gchar* sql = NULL; + + LOGS("Bookmark/%s->%s() called. Param\nEngine at address: %p\n" + "word: %s\ntranslation address: %p\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + engine, + word, + translation + ); + g_assert(engine != NULL); + g_assert(word != NULL); + g_assert(translation != NULL); + + // start timer for this function + timer(TIMER_START, (gchar*)(gchar*)__FUNCTION__); + + BookData* data = (BookData*)(engine->engine_data); + sql = sqlite3_mprintf( + "INSERT INTO translations VALUES(NULL, '%q', '%q');", + word, translation); + sql_res = sqlite3_exec(data->db, sql, NULL, NULL, &err); + if(err || sql_res!=0) { + LOGS("Error while adding translation for word %s \nreason:%s\n\n", word, err); + result = FALSE; + } + sqlite3_free(sql); sql = NULL; + + timer(TIMER_STOP, (gchar*)(gchar*)__FUNCTION__); + return result; +} +//------------------------------------------------------------------------------ +gboolean bm_engine_remove_word(Engine* engine, gchar* word) { + gint sql_res = 0; + gchar* err = NULL; + gboolean result = TRUE; + + LOGS("Bookmark/%s->%s() called. Param\nEngine at address: %p\n" + "word: %s\n",(gchar*)__FILE__,(gchar*)__FUNCTION__,engine,word); + g_assert(engine != NULL); + g_assert(word != NULL); + timer(TIMER_START, (gchar*)(gchar*)__FUNCTION__); + + BookData* data = (BookData*)(engine->engine_data); + gchar* sql = sqlite3_mprintf( + "DELETE FROM translations WHERE word LIKE '%q';", + word); + sql_res = sqlite3_exec(data->db, sql, NULL, NULL, &err); + if(err || sql_res!=0) { + LOGS("Error while deleting \'%s\' <-> reason:\n%s\n",word,err); + sqlite3_free(sql); sql = NULL; + return FALSE; + } + sqlite3_free(sql); sql = NULL; + + timer(TIMER_STOP, (gchar*)(gchar*)__FUNCTION__); + return result; +} +//------------------------------------------------------------------------------ +gchar* bm_engine_get_lang_from(Engine* engine) { + LOGS("Bookmark/%s->%s() called.\n",(gchar*)__FILE__,(gchar*)__FUNCTION__); + gchar* result = g_strdup("any"); + LOGS("Bookmark/%s->%s() return string=%s\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + result + ); + return result; +} +//------------------------------------------------------------------------------ +gchar* bm_engine_get_lang_to(Engine* engine) { + LOGS("Bookmark/%s->%s() called.\n",(gchar*)__FILE__,(gchar*)__FUNCTION__); + gchar* result = g_strdup("any"); + LOGS("Bookmark/%s->%s() return string=%s\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + result + ); + return result; +} +//------------------------------------------------------------------------------ +gchar* bm_engine_get_title(Engine* engine) { + LOGS("Bookmark/%s->%s() called.\n",(gchar*)__FILE__,(gchar*)__FUNCTION__); + gchar* result = g_strconcat(g_get_user_name(),"s' bookmarks",NULL); + LOGS("Bookmark/%s->%s() return string=%s\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + result + ); + return result; +} +//------------------------------------------------------------------------------ +gchar* bm_engine_get_icon_path(Engine* engine) { + LOGS("Bookmark/%s->%s() called.\n",(gchar*)__FILE__,(gchar*)__FUNCTION__); + gchar* result = g_strdup("/usr/share/pixmaps/ws_eng_bookmark_icon.png"); + LOGS("Bookmark/%s->%s() return string=%s\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + result + ); + return result; +} + +//============================================================================== +//============================================================================== +//============================================================================== +//-------------------------------------- FUNCTION TO WRITE (NOT IMPLEMENTED YET) +//============================================================================== + +//------------------------------------------------------------------------------ +// searching word by concrete engine +void bm_engine_search_word_translation(Engine* engine, gchar* word) +{ + LOGS("Bookmark/%s->%s() called.\n-->PARAM:engine at adress=%p\n" + "-->PARAM:word=\'%s\'\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + engine, + word + ); + g_assert(engine != NULL); + g_assert(word != NULL); + // start timer for this function + timer(TIMER_START, (gchar*)(gchar*)__FUNCTION__); + BookData* data = (BookData*)(engine->engine_data); + gchar* tmp = g_utf8_strdown(word,-1); + //sqlite3_vm* query; + gchar* sql = NULL; + gchar* tran = NULL; + sqlite3_stmt *query;//ppStmt + const char *pzTail; +/* + sql = g_strconcat("SELECT * FROM translations WHERE " + "word_id=(SELECT id FROM words WHERE word=\'", + tmp, + "\');", + NULL); +*/ + + /*sql = g_strconcat("SELECT * FROM translations WHERE word LIKE \'", + tmp, + "\';", + NULL);*/ + + sql = sqlite3_mprintf( + "SELECT * FROM translations WHERE word LIKE '%q';", + tmp); + + g_debug("Query text: %s", sql); + gint sql_res = 0; + // const gchar* end; + //gchar* err = NULL; + sql_res = sqlite3_prepare( + data->db,/* Database handle */ + (const gchar *)sql,/* SQL statement, UTF-8 encoded */ + -1,/* Length of zSql in bytes. */ + &query,/* OUT: Statement handle */ + &pzTail/* OUT: Pointer to unused portion of zSql */ + ); + if (sql_res != SQLITE_OK) + { + LOGS("Error while compiling query:\n%s\nreason: %d\n",sql, sql_res); + sqlite3_free(sql); sql = NULL; + data->cb_search_word_trans(NULL, NULL, + data->cb_search_word_trans_data, + ENGINE_NO_ERROR); + return; + } + gint status = 0; + gboolean first = TRUE; + do { + first = TRUE; + do { + if(!first) sleep(1); + first = FALSE; + status = sqlite3_step(query); + if(status == SQLITE_ROW) { + if (tran != NULL){ + tran = g_strconcat(tran,"

", + sqlite3_column_text(query, 2), + NULL); + }else{ + tran = g_strdup_printf("%s

", + sqlite3_column_text(query, 2)); + } + LOGS("Translation found :\n\"%s\"\n",tran); + + } + } while((status == SQLITE_BUSY) && (status != SQLITE_ERROR)); + } while((status == SQLITE_ROW) && (status != SQLITE_ERROR)); + sqlite3_finalize(query); + + + timer(TIMER_STOP,(gchar*)(gchar*)__FUNCTION__); + timer(TIMER_START,"callback for returning word's translation START"); + // calling callback for word translation + + data->cb_search_word_trans(tran, word, data->cb_search_word_trans_data, + ENGINE_NO_ERROR); + sqlite3_free(sql); sql = NULL; + timer(TIMER_STOP,"callback for returning word's translation END"); + if(data->auto_free) { + LOGS("Bookmark/%s->%s() deleting all dynamic data because " + "AUTO_FREE=TRUE\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__ + ); + g_free(tran); + } + tran = NULL; +} + +//------------------------------------------------------------------------------ +void bm_engine_close(Engine* engine) +{ + LOGS("Bookmark/%s->%s() called.\n-->PARAM: engine adress=%p\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + engine); + g_assert(engine != NULL); + + BookData* data = (BookData*)(engine->engine_data); + sqlite3_close(data->db); + + LOGS("Bookmark/%s->%s() engine at adress=%p is deleted.\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + engine); + g_free(engine); + engine = NULL; +} +//------------------------------------------------------------------------------ + +Engine* bm_engine_create(gchar* location, + EngineOptimizationFlag auto_cache, + cb_progress progress_handler, + gpointer progress_data, + gdouble seed) +{ + LOGS("Bookmark/%s->%s() called.\n" + "-->PARAM:location=\'%s\'\n" + "-->PARAM:auto_cache=%d\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + location, + (guint)auto_cache + ); + timer(TIMER_START,(gchar*)(gchar*)__FUNCTION__); + + gchar* tmp = g_strdup(location); + string_to_path(&tmp); + + Engine* result = (Engine*)g_try_malloc(sizeof(Engine)); + result->engine_location = bm_engine_location; + result->engine_is_optimized = bm_engine_is_optimized; + result->engine_optimize = bm_engine_optimize; + result->engine_search_word_list = bm_engine_search_word_list; + result->engine_search_word_translation = + bm_engine_search_word_translation; + result->engine_close = bm_engine_close; + result->engine_status = bm_engine_status; + result->engine_error_message = bm_engine_status_message; + result->engine_set_callback = bm_engine_set_callback; + result->engine_set_progress_seed = bm_engine_set_progress_seed; + result->engine_set_auto_free = bm_engine_set_auto_free; + // 0.2 API: + result->engine_add_word = bm_engine_add_word; + result->engine_remove_word = bm_engine_remove_word; + result->engine_get_lang_from = bm_engine_get_lang_from; + result->engine_get_lang_to = bm_engine_get_lang_to; + result->engine_get_title = bm_engine_get_title; + result->engine_get_icon_path = bm_engine_get_icon_path; + + + BookData* data = (BookData*)g_try_malloc(sizeof(BookData)); + result->engine_data = (gpointer)data; + + LOGS("Bookmark/%s->%s() opening file...\'%s\'.\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + location + ); + gchar* tmp2 = g_strconcat(tmp,"/ws_bookmarks3",NULL); + //gchar* err; + gint res = sqlite3_open(tmp2,&data->db); + g_free(tmp2); tmp2 = NULL; + if(res != SQLITE_OK) { + LOGS("Bookmark/%s->%s() opening bookmark file failed.%s\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + sqlite3_errmsg(data->db) + ); + //g_free(err); + g_free(data); + g_free(result); + result = NULL; + } + else { + LOGS("Bookmark/%s->%s()opening dictionary file successed.\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__ + ); + data->dict_path = g_strdup(tmp); + data->cb_progress_caching = progress_handler; + data->cb_progress_caching_data = progress_data; + data->cb_progress_caching_seed = seed; + data->cb_progress_word_list = NULL; + data->cb_progress_word_list_data = NULL; + data->cb_progress_word_list_seed = 0.01; + data->cb_progress_word_trans = NULL; + data->cb_progress_word_trans_data = NULL; + data->cb_progress_word_trans_seed = 0.01; + + data->cb_search_word_list = NULL; + data->cb_search_word_list_data = NULL; + + data->cb_search_word_trans = NULL; + data->cb_search_word_trans_data = NULL; + + data->auto_free = FALSE; + // there is no cache mechanizm in bookmarks + } + g_free(tmp); tmp = NULL; + + timer(TIMER_STOP,(gchar*)(gchar*)__FUNCTION__); + LOGS("Bookmark/%s->%s() returned Engine at adress=%p\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + result + ); + return result; +} +//------------------------------------------------------------------------------ + + + +static gboolean is_Bookmark_file(gchar* file) { + LOGS("Bookmark/%s->%s() called.\n\ + -->PARAM:file=\'%s\'\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + file + ); + gchar* err = NULL; + sqlite3* tmp = NULL; + gint result = sqlite3_open(file, &tmp); + if(result != SQLITE_OK) { + LOGS("Wrong file! Not a sqlite database.\n"); + //g_free(err); + sqlite3_close(tmp); + return FALSE; + } + + + gchar sql[] = "SELECT COUNT(word) FROM translations WHERE word=\'.\';"; + result = sqlite3_exec(tmp, sql, NULL, NULL, &err); + if(err || result!=0) { + LOGS("Wrong database! Not a bookmark.\n"); + g_free(err); + sqlite3_close(tmp); + return FALSE; + } + sqlite3_close(tmp); + return TRUE; +} +//------------------------------------------------------------------------------ + + +void bm_engine_optimize(Engine* engine) +{ + LOGS("Bookmark/%s->%s() called for engine at adress=%p\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + engine + ); + LOGS("Unsupported optimization mechanizm for this engine!\n"); + LOGS("Bookmark/%s->%s()'s work finished.\n",(gchar*)__FILE__,(gchar*)__FUNCTION__); +} +//------------------------------------------------------------------------------ +gboolean bm_engine_check(gchar* location) +{ + LOGS("Bookmark/%s->%s() called.\n-->PARAM:location=\'%s\'\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + location + ); + timer(TIMER_START,(gchar*)(gchar*)__FUNCTION__); + gboolean result = TRUE; + gchar* filepath = g_strdup(location); + gchar* tmp = NULL; + + string_to_path(&filepath); + if (filepath == NULL) { + result = FALSE; + LOGS("Bookmark/%s->%s() location \'%s\' is not a proper " + "path!\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + location + ); + } + else { + tmp = g_strconcat(filepath,"/ws_bookmarks3",NULL); + g_free(filepath); + filepath = tmp; + tmp = NULL; + + LOGS("Bookmark/%s->%s() finnal file to check is: %s\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + filepath + ); + if (!g_file_test(filepath, G_FILE_TEST_IS_REGULAR)) { + LOGS("Bookmark/%s->%s() file \'%s\' does not exists!\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + filepath + ); + result = FALSE; + }; + }; + if (result != FALSE) { + result = is_Bookmark_file(filepath); + }; + + g_free(filepath); + timer(TIMER_STOP,(gchar*)(gchar*)__FUNCTION__); + LOGS("Bookmark/%s->%s() returned bool statement=%s.\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + PRINT_STATE(result) + ); + return result; +} + +//------------------------------------------------------------------------------ +gboolean bm_engine_is_optimized(Engine* engine) +{ + LOGS("Bookmark/%s->%s() called.\n-->PARAM: engine adress=%p\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + engine + ); + g_assert(engine != NULL); + gboolean result = FALSE; + LOGS("Bookmark/%s->%s() returned bool statement=%s.\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + PRINT_STATE(result) + ); + return result; +} +//------------------------------------------------------------------------------ + +void bm_engine_search_word_list(Engine* engine, gchar* pattern) +{ + LOGS("Bookmark/%s->%s() called. Searching words list\n" + "-->PARAM:engine at adress=%p\n" + "-->PARAM:pattern=\"%s\"\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + engine, + pattern + ); + g_assert(engine != NULL); + g_assert(pattern != NULL); + + timer(TIMER_START,(gchar*)(gchar*)__FUNCTION__); + BookData* data = (BookData*)(engine->engine_data); + if(data->cb_search_word_list == NULL) { + LOGS("Bookmark/%s->%s() callback for Word List not set. " + "Searching aborted.\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__ + ); + timer(TIMER_STOP,(gchar*)(gchar*)__FUNCTION__); + return; + }; + + + gchar* tmp = g_utf8_strdown(pattern,-1); + sqlite3_stmt *query;//ppStmt + const char *pzTail; + gchar* sql = NULL; + gchar* tran = NULL; + g_strstrip(tmp); + if ((int)(tmp[0]) == 42 && tmp[1] == '\0') //asterix? + { + sql = sqlite3_mprintf("SELECT DISTINCT word FROM translations;"); + } + else + { + sql = sqlite3_mprintf( + "SELECT DISTINCT word FROM translations WHERE word LIKE'%q%%';", tmp); + } + /*sql = g_strconcat("SELECT word FROM translations WHERE " + "word LIKE \'",tmp,"\%\';",NULL);*/ + LOGS("QUERY: %s\n",sql); + gint sql_res = 0; + sql_res = sqlite3_prepare( + data->db,/* Database handle */ + (const gchar *)sql,/* SQL statement, UTF-8 encoded */ + -1,/* Length of zSql in bytes. */ + &query,/* OUT: Statement handle */ + &pzTail/* OUT: Pointer to unused portion of zSql */ + ); + if (sql_res != SQLITE_OK) + { + LOGS("Error while compiling query:\n%s\nreason:%d\n",sql, + sql_res); + sqlite3_free(sql); sql = NULL; + return; + } + gint status = 0; + gboolean first = TRUE; + GArray* result = g_array_new(TRUE, TRUE, sizeof(gchar*) ); + do { + first = TRUE; + do { + if(!first) sleep(1); + first = FALSE;; + /*status = sqlite3_step( + query, + &nCol, + &values, + &head + );*/ + status = sqlite3_step(query); + if(status == SQLITE_ERROR) { + LOGS("Error while making next step\n"); + sqlite3_free(sql); sql = NULL; + data->cb_search_word_list(NULL , NULL, + data->cb_search_word_list_data, ENGINE_NO_ERROR); + return; + }; + /*if(status == SQLITE_ROW) { + LOGS("Fount new word: \"%s\"\n",&(values[0][0])); + gchar* tmp = g_strdup((gchar*)&(values[0][0])); + g_array_append_val(result, tmp ); + }*/ + if(status == SQLITE_ROW) { + gchar* tmp = g_strdup( + sqlite3_column_text(query, 0)); + LOGS("Found new word: \"%s\"\n", tmp); + g_array_append_val(result, tmp); + } + } while((status == SQLITE_BUSY) && (status != SQLITE_ERROR)); + } while((status == SQLITE_ROW) && (status != SQLITE_ERROR)); + sqlite3_finalize(query); + timer(TIMER_STOP,(gchar*)(gchar*)__FUNCTION__); + + + timer(TIMER_START,"callback for returning words LIST START"); + // calling callback for word translation + + data->cb_search_word_list(result , pattern, + data->cb_search_word_list_data, ENGINE_NO_ERROR); + + timer(TIMER_STOP,"callback for returning word LIST END"); + if(data->auto_free) { + LOGS("Bookmark/%s->%s() deleting all dynamic data because " + "AUTO_FREE=TRUE\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__ + ); + g_free(tran); + } +} + +//============================================================================== +//============================================================================== +//============================================================================== +//---------------------------------------------------------- COMPLETED FUNCTIONS +//============================================================================== +// global functions +EngineModule engine_global_functions() +{ + LOGS("Bookmark/%s->%s() called.\n",(gchar*)__FILE__,(gchar*)__FUNCTION__); + EngineModule* result = g_try_new(EngineModule, 1); + result->engine_check = bm_engine_check; + result->engine_description = bm_engine_description; + result->engine_format = bm_engine_format; + result->engine_version = bm_engine_version; + result->engine_create = bm_engine_create; + LOGS("Bookmark/%s->%s()returned EngineModule at adress=%p.\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + result + ); + return *result; +} +//------------------------------------------------------------------------------ +// for macro: dict_eng_status_message(error) +gchar* bm_engine_status_message(EngineStatus error) +{ + LOGS("Bookmark/%s->%s() called.\n",(gchar*)__FILE__,(gchar*)__FUNCTION__); + switch (error) { + case ENGINE_NO_ERROR: + return "No error."; + case ENGINE_WRONG_FILE: + return "File which You are trying to use is wrong type."; + case ENGINE_COULDNT_READ: + return "Could not read from file."; + case ENGINE_NO_FILE: + return "There is no such a file."; + case ENGINE_OUT_OF_MEMORY: + return "There were no enough memory for this action."; + default: + return "Wrong engine's status identifier!"; + } +} +//------------------------------------------------------------------------------ +// for macro: dict_eng_module_get_version(module) +gchar* bm_engine_version() +{ + LOGS("Bookmark/%s->%s() called.\n",(gchar*)__FILE__,(gchar*)__FUNCTION__); + gchar* result = g_strdup(DIC_ENG_VERSION); + LOGS("Bookmark/%s->%s() return string=%s\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + result + ); + return result; +} +//------------------------------------------------------------------------------ +// for macro: dict_eng_module_get_format(module) +gchar* bm_engine_format() +{ + LOGS("Bookmark/%s->%s() called.\n",(gchar*)__FILE__,(gchar*)__FUNCTION__); + gchar* result = g_strdup(DIC_ENG_FORMAT); + LOGS("Bookmark/%s->%s() return string=%s\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + result + ); + return result; +} +//------------------------------------------------------------------------------ +// for macro: dict_eng_module_get_description(module) +gchar* bm_engine_description() +{ + LOGS("Bookmark/%s->%s() called.\n",(gchar*)__FILE__,(gchar*)__FUNCTION__); + gchar* result = g_strdup(DIC_ENG_DESCRIPTION); + LOGS("Bookmark/%s->%s() return string=%s\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + result + ); + return result; +} +//------------------------------------------------------------------------------ +// for macro: dict_eng_get_location(engine) +gchar* bm_engine_location(Engine* engine) +{ + LOGS("Bookmark/%s->%s() called.\n-->PARAM: engine adress=%p\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + engine + ); + g_assert(engine != NULL); + BookData* data = (BookData*)(engine->engine_data); + + gchar* result; + if(data->auto_free) { + result = data->dict_path; + } + else { + result = g_strdup(data->dict_path); + } + + LOGS("Bookmark/%s->%s() returned string=%s\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + result + ); + return result; +} +//------------------------------------------------------------------------------ +// for macro: dict_eng_set_auto_free(engine, state) +void bm_engine_set_auto_free(Engine* engine, gboolean state) +{ + LOGS("Bookmark/%s->%s() called.\n" + "-->PARAM:engine at adress=%p\n" + "-->PARAM:state=%s\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + engine, + PRINT_STATE(state) + ); + g_assert(engine != NULL); + BookData* data = (BookData*)(engine->engine_data); + + data->auto_free = state; + LOGS("Bookmark/%s->%s() Current auto_free is %s\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + PRINT_STATE(data->auto_free) + ); +} +//------------------------------------------------------------------------------ +// for macro: dict_eng_get_last_status(engine) +EngineStatus bm_engine_status(Engine* engine) +{ + LOGS("Bookmark/%s->%s() called.\n" + "-->PARAM:engine at adress=%p\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + engine + ); + BookData* data = (BookData*)(engine->engine_data); + LOGS("Bookmark/%s->%s() returned error code: %d\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + (gint)(data->last_error) + ); + return data->last_error; +} +//------------------------------------------------------------------------------ +// for macro: dict_eng_set_progress_seed(engine, signal, val) +void bm_engine_set_progress_seed(Engine* engine, gchar* signal, gdouble seed) { + LOGS("Bookmark/%s->%s() called.\n",(gchar*)__FILE__,(gchar*)__FUNCTION__); + BookData* data = (BookData*)(engine->engine_data); + if(g_ascii_strcasecmp(signal,ENGINE_PROGRESS_OPTIMIZING_SIGNAL) == 0) { + data->cb_progress_caching_seed = seed; + LOGS("Bookmark/%s->%s() sets new seed=%0.2f for for signal " + "\"%s\".\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + seed, + signal + ); + } + else { + LOGS("Bookmark/%s->%s() unsupported signalfor progress: %s.\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + signal + ); + }; +} +//------------------------------------------------------------------------------ +// for macro: dict_eng_set_callback(engine,signal,c_handler,data) +gpointer bm_engine_set_callback(Engine* engine, + gchar* signal, + gpointer c_handler, + gpointer user_data) +{ + LOGS("Bookmark/%s->%s() called.\n",(gchar*)__FILE__,(gchar*)__FUNCTION__); + g_assert(engine != NULL); + g_assert(signal != NULL); + g_assert(c_handler != NULL); + BookData* data = (BookData*)(engine->engine_data); + if(g_ascii_strcasecmp(signal,ENGINE_PROGRESS_OPTIMIZING_SIGNAL) == 0) { + gpointer result = data->cb_progress_caching; + data->cb_progress_caching = c_handler; + data->cb_progress_caching_data = user_data; + LOGS("Bookmark/%s->%s() sets handler for signal \"%s\".\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + signal + ); + LOGS("Bookmark/%s->%s() Function at adress = %d.\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + (guint)c_handler + ); + LOGS("Bookmark/%s->%s() Data at adress = %d.\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + (guint)user_data + ); + return result; + } + else if(g_ascii_strcasecmp(signal, ENGINE_WORD_LIST_SIGNAL) == 0) { + gpointer result = data->cb_search_word_list; + data->cb_search_word_list = c_handler; + data->cb_search_word_list_data = user_data; + LOGS("Bookmark/%s->%s() sets handler for signal \"%s\".\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + signal + ); + LOGS("Bookmark/%s->%s() Function at adress = %d.\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + (guint)c_handler + ); + LOGS("Bookmark/%s->%s() Data at adress = %d.\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + (guint)user_data + ); + return result; + } + else if(g_ascii_strcasecmp(signal, + ENGINE_WORD_TRANSLATION_SIGNAL) == 0) { + gpointer result = data->cb_search_word_trans; + data->cb_search_word_trans = c_handler; + data->cb_search_word_trans_data = user_data; + LOGS("Bookmark/%s->%s() sets handler for signal \"%s\".\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + signal + ); + LOGS("Bookmark/%s->%s() Function at adress = %d.\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + (guint)c_handler + ); + LOGS("Bookmark/%s->%s() Data at adress = %d.\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + (guint)user_data + ); + return result; + } + else { + g_warning("Bookmark/%s->%s() unsupported signal: %s.\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + signal + ); + return NULL; + } +} + + + + + + +//============================================================================== +//============================================================================== +//============================================================================== +//---------------------------------------------------------- HELPFULLY FUNCTIONS +//============================================================================== + +//------------------------------------------------------------------------------ +static gchar* string_to_path(gchar** string) { + LOGS("Bookmark/%s->%s() called.\n\ + -->PARAM:string=\'%s\'\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + string[0] + ); + gchar* arg = string[0]; + gchar* new = NULL; + // cleaning from leading and trailing whitespaces + g_strstrip(arg); + // add current directory if this is not absolute directory + if (!g_path_is_absolute(arg)) { + gchar* tmp = g_get_current_dir(); + new = g_strconcat(tmp,"/",arg,NULL); + g_free(arg); arg = new; new = NULL; + }; + // this is not a directory + if (!g_file_test(arg, G_FILE_TEST_IS_DIR)) { + // if this is wrong filepath, string was wrong + if (!g_file_test(arg, G_FILE_TEST_IS_REGULAR)) { + g_free(arg); + new = NULL; + } + //if this is a file, remove filename + else + { + new = g_path_get_dirname (arg); + g_free(arg); + } + } + // this is a directory + else { + // remove suffix "/" if neded... + if (g_str_has_suffix(arg,"/") ) { + new = g_path_get_dirname (arg); + g_free(arg); + } + else { + new = arg; + } + }; + // now in new should be proper filepath, if not, string was wrong + if (!g_file_test(new, G_FILE_TEST_IS_DIR)) { + // if that directory does not exist, passed string wasn't proper + g_free(new); + new = NULL; + }; + // replace string under passed address + string[0] = new; + LOGS("Bookmark/%s->%s() returned string=\'%s\'\n", + (gchar*)__FILE__, + (gchar*)__FUNCTION__, + string[0] + ); + return new; +} +//------------------------------------------------------------------------------ +static double timer(gboolean start, gchar* message) +{ + static GArray* stack = NULL; + static gboolean first_run = TRUE; + static struct timeval actual_time; + static struct timeval last_time; + static struct timeval result; + static double seconds = 0.0; + if(first_run) { + first_run = FALSE; + stack = g_array_new(TRUE, TRUE, sizeof(struct timeval)); + }; + + if (start) { + LOGS("Bookmark->%s() start timer for function '%s()'.\n", + (gchar*)__FUNCTION__, + message + ); + g_array_prepend_val(stack, actual_time); + gettimeofday(&g_array_index(stack, struct timeval, 0),NULL); + return -1.0; + } + // we just want to end some timer - print some information about + // working time; + else { + gettimeofday(&actual_time,NULL); + last_time = g_array_index(stack, struct timeval, 0); + g_array_remove_index(stack, 0); + + if (actual_time.tv_usec < last_time.tv_usec) { + int nsec = (last_time.tv_usec - actual_time.tv_usec) / + (1000000 + 1); + last_time.tv_usec -= 1000000 * nsec; + last_time.tv_sec += nsec; + } + if (actual_time.tv_usec - last_time.tv_usec > 1000000) { + int nsec = (last_time.tv_usec - actual_time.tv_usec) / + 1000000; + last_time.tv_usec += 1000000 * nsec; + last_time.tv_sec -= nsec; + } + result.tv_sec = actual_time.tv_sec - last_time.tv_sec; + result.tv_usec = actual_time.tv_usec - last_time.tv_usec; + seconds = (((double)(result.tv_usec)) / 1e6) + + ((double)(result.tv_sec)); + + LOGS("Bookmark->%s() function \'%s()\' was working for: %g " + "[s] or %ld [us].\n", + (gchar*)__FUNCTION__, + message, + seconds, + ((long)(result.tv_sec*1e6)+(result.tv_usec)) + ); + // stack is empty so we delete everything + if(stack->len == 0) + { + g_array_free(stack, TRUE); + first_run = TRUE; + } + } + return seconds; +} +//------------------------------------------------------------------------------ + + + diff --git a/src/bookmarks/sql3/src/test.c b/src/bookmarks/sql3/src/test.c new file mode 100644 index 0000000..b89a9a7 --- /dev/null +++ b/src/bookmarks/sql3/src/test.c @@ -0,0 +1,144 @@ +/******************************************************************************* +This file is part of WhiteStork. + +WhiteStork is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +WhiteStork 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 General Public License for more details. + +You should have received a copy of the GNU General Public License +along with WhiteStork; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +Copyright 2006 ComArch S.A. +*******************************************************************************/ +#include +#include +#include +#include + + +getting_additional get_functions; // additinal functions for concrete module (e.g. XDXF) + +void print_list(GArray* list, gchar* pattern, gpointer user_data, EngineStatus error) { + printf((gchar*)user_data,pattern); + int i = 0; + while(g_array_index(list, gchar*, i) != NULL) + { + printf(" %d. : %s\n",i+1,g_array_index(list, gchar*, i)); + i++; + } + printf("--------------------------------------------------\n"); +} + +void print_translation(gchar* translation, gchar* word, gpointer user_data, EngineStatus error) { + printf((gchar*)user_data,word); + printf("%s\n\nTRANSLATION ENDS.\n",translation); +} + +void caching_progress(gdouble value, gpointer user_data, EngineStatus error) { + printf((gchar*)user_data,value); +} + +int main(int argc, char** argv) +{ + char* nameApp = "StarDictEngine test: "; + printf("%sStarting test program of module: dictionary_engine.\n",nameApp); + + + gchar* current_directory = g_get_current_dir(); + printf("%sCurrent directory: %s\n",nameApp,current_directory); + gchar* library_to_path = g_strconcat(current_directory, "/ws_bookmark.so", NULL); + printf("%sEngine library should be in location:\n\t%s\n",nameApp,library_to_path); + + GModule *library = g_module_open(library_to_path, G_MODULE_BIND_LAZY); + if(!library) { + printf("%sLoading module failed. \nReason: %s\n",nameApp,g_module_error()); + return 1; + }; + + //dict_eng_module_get_global_functions(library,get_functions); + g_module_symbol ( (library),_GLOBAL_FUNCTIONS_NAME_, (gpointer)&get_functions); + if(get_functions == NULL) { + printf("%sLoading function failed\n",nameApp); + return 2; + } + else printf("%sLoading function OK\n",nameApp); + + EngineModule module = get_functions(); + printf("Module description: %s\n",dict_eng_module_get_description(module)); + Engine* sd; + + gboolean is_compatible = dict_eng_module_check(module,"/home/str/whitestork/engines/bookmark/ws_bookmarks"); + if(is_compatible == TRUE) + { + printf("Location is compatible with enigne!\n"); + } + else { + printf("Location is not compatible with enigne!\n"); + return 1; + } + + //printf("%sCheck OK. Module description: %s\n", nameApp, dict_eng_module_get_description(module)); + + //xdxf = dict_eng_module_create_ext(module, "/home/lukasz/MyDocs/repo/WhiteStork/trunk/engine/bin" , ENGINE_CREATE, caching_progress, "Current progress of caching is: %0.2f.\n", 0.03 ); + //home/stranger/whitestork/engine0.2/ +// xdxf = dict_eng_module_create(module, "/home/stranger/whitestork/engine0.2/" , ENGINE_NO); +// dict_eng_set_callback(xdxf, ENGINE_PROGRESS_CACHING, caching_progress, "Current progress of caching is: %0.2f.\n"); +// dict_eng_set_progress_seed(xdxf, ENGINE_PROGRESS_CACHING, 0.02); + +/* if(dict_eng_is_optimized(xdxf) == FALSE) + { + printf("Dictionary has no cache!\nCreating cache file....\n"); + dict_eng_optimize(xdxf); + } + else { + printf("Dictionary has already cache file!\n"); + }*/ + sd = dict_eng_module_create(module, + "/home/str/whitestork/engines/bookmark/ws_bookmarks" , + ENGINE_CREATE); + printf("Lokacja: %s\n",dict_eng_get_location(sd)); + + dict_eng_set_callback(sd, + ENGINE_WORD_LIST_SIGNAL, + print_list, + "Word list matches to pattern: %s\n" + ); + dict_eng_set_callback(sd, + ENGINE_WORD_TRANSLATION_SIGNAL , + print_translation, + "Translation for word\'%s\':\n"); + + dict_eng_search_word_list(sd,"12"); + //dict_eng_search_word_translation(sd,"1 word"); + + dict_eng_remove_word(sd, "stranger"); + dict_eng_remove_word(sd, "gandzia"); + dict_eng_remove_word(sd, "lukas"); + dict_eng_add_word(sd, "stranger", "autor pluginu tego wlasnie to jest!"); + dict_eng_add_word(sd, "gandzia", "rulez"); + dict_eng_add_word(sd, "lukas", "pawlik"); + dict_eng_remove_word(sd, "stranger"); + + dict_eng_search_word_translation(sd,"stranger"); + + dict_eng_add_word(sd, "stranger", "autor pluginu tego wlasnie to jest!"); + dict_eng_search_word_translation(sd,"stranger"); + dict_eng_search_word_list(sd,"gandzia"); + + printf("Lang FROM:%s\n",dict_eng_get_lang_from(sd)); + printf(" Lang TO:%s\n",dict_eng_get_lang_to(sd)); + printf(" Title:%s\n",dict_eng_get_title(sd)); + printf("Icon path:%s\n",dict_eng_get_icon_path(sd)); + + dict_eng_destroy(sd); + printf("%sClosed.\n",nameApp); + return 0; +} + diff --git a/src/bookmarks/sql3/src/testMakeDatabase.c b/src/bookmarks/sql3/src/testMakeDatabase.c new file mode 100644 index 0000000..7ca65e1 --- /dev/null +++ b/src/bookmarks/sql3/src/testMakeDatabase.c @@ -0,0 +1,135 @@ +/******************************************************************************* +This file is part of WhiteStork. + +WhiteStork is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +WhiteStork 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 General Public License for more details. + +You should have received a copy of the GNU General Public License +along with WhiteStork; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +Copyright 2006 ComArch S.A. +*******************************************************************************/ + +#include +#include +#include + +const guint records_number = 1000; + +// ---------------------------------------------------------------------------------- +sqlite* database_make(gchar* path) { + gchar* buffer = NULL; + sqlite* result = NULL; + // creatingh database + result = sqlite_open(path, + 0600, + &buffer); + // check if everything was okey + if(!result) { + g_printf("Error while trying to create database:\n%s\n",buffer); + g_free(buffer); buffer = NULL; + return NULL; + }; + // return pointer to database + return result; +} +// ---------------------------------------------------------------------------------- +gint database_add_test_table(sqlite* db) { + gint result = 0; + //gchar sql[] = "CREATE TABLE words(id INTEGER PRIMARY KEY, word TEXT UNIQUE);"; + gchar sql2[] = "CREATE TABLE translations(id INTEGER PRIMARY KEY, word TEXT, translation TEXT);"; + gchar* err = NULL; +/* + result = sqlite_exec(db, sql, NULL, NULL, &err); + if(err || result!=0) { + g_printf("Error while creating \'words\' table:\n%s\n",err); + return 1; + } +*/ + result = sqlite_exec(db, sql2, NULL, NULL, &err); + if(err || result!=0) { + g_printf("Error while creating \'translations\' table:\n%s\n",err); + return 2; + } + + return result; +} +// ---------------------------------------------------------------------------------- +gint database_add_test_record(sqlite* db, guint i) { + gint result = 0; + gchar* sql = NULL; + gchar* err = NULL; + gint last_id = 0; + gint word_count = 1; + gchar buffer[] = " "; + gchar last_char[] = " "; + gchar tran[] = "this is a translation number 10.000 for word 10.000 word <-----"; + g_sprintf(buffer,"%d word",i); +/* + sql = g_strconcat("INSERT INTO words VALUES(NULL,\'",buffer,"\');", NULL); + result = sqlite_exec(db, sql, NULL, NULL, &err); + if(err || result!=0) { + g_printf("Error while trying to add new word %d:\n%s\n",i,err); + g_free(sql); + return 1; + } + g_free(sql); + last_id = sqlite_last_insert_rowid(db); + g_sprintf(last_char,"%d",last_id); +*/ + if(i == 5000) { + word_count = 5; + g_printf("Adding more records for word - test\n"); + } + gint j; + for(j=0; j