Updated Spanish translation
[anjuta-git-plugin.git] / tagmanager / test_tm_buffer.c
blob62aacafd5f6e2c67940c7c44a7ac4ff6fcfea136
2 #include <stdio.h>
3 #include <limits.h>
4 #include <stdlib.h>
5 #include <string.h>
7 #include "general.h"
8 #include "entry.h"
9 #include "parse.h"
10 #include "read.h"
11 #define LIBCTAGS_DEFINED
12 #include "tm_work_object.h"
14 #include "tm_source_file.h"
15 #include "tm_tag.h"
17 #define TM_DEBUG
19 guint source_file_class_id = 0;
20 static TMSourceFile *current_source_file = NULL;
23 int tm_source_file_tags(const tagEntryInfo *tag)
25 if (NULL == current_source_file)
26 return 0;
27 if (NULL == current_source_file->work_object.tags_array)
28 current_source_file->work_object.tags_array = g_ptr_array_new();
29 g_ptr_array_add(current_source_file->work_object.tags_array,
30 tm_tag_new(current_source_file, tag));
32 // g_message ("adding new tag: %s", tag->name);
34 return TRUE;
38 gboolean tm_buffer_parse(TMSourceFile *source_file, unsigned char* buf, int buf_size)
40 const char *file_name;
41 gboolean status = TRUE;
43 if ((NULL == source_file) || (NULL == source_file->work_object.file_name))
45 g_warning("Attempt to parse NULL file");
46 return FALSE;
49 #ifdef TM_DEBUG
50 g_message("Parsing %s", source_file->work_object.file_name);
51 #endif
52 file_name = source_file->work_object.file_name;
53 if (NULL == LanguageTable)
55 initializeParsing();
56 installLanguageMapDefaults();
57 if (NULL == TagEntryFunction)
58 TagEntryFunction = tm_source_file_tags;
60 current_source_file = source_file;
61 if (LANG_AUTO == source_file->lang)
62 source_file->lang = getFileLanguage (file_name);
63 if (source_file->lang == LANG_IGNORE)
65 #ifdef TM_DEBUG
66 g_warning("ignoring %s (unknown language)\n", file_name);
67 #endif
69 else if (! LanguageTable [source_file->lang]->enabled)
71 #ifdef TM_DEBUG
72 g_warning("ignoring %s (language disabled)\n", file_name);
73 #endif
75 else
77 int passCount = 0;
78 while ((TRUE == status) && (passCount < 3))
80 if (source_file->work_object.tags_array)
81 tm_tags_array_free(source_file->work_object.tags_array, FALSE);
82 if (bufferOpen (buf, buf_size, file_name, source_file->lang))
84 if (LanguageTable [source_file->lang]->parser != NULL) {
85 LanguageTable [source_file->lang]->parser ();
87 else if (LanguageTable [source_file->lang]->parser2 != NULL) {
88 status = LanguageTable [source_file->lang]->parser2 (passCount);
90 g_message ("closing buffer");
91 bufferClose ();
93 else
95 g_warning("Unable to open %s", file_name);
96 return FALSE;
98 ++ passCount;
100 return TRUE;
102 return status;
107 int main (int argc, char** argv) {
109 TMWorkObject *source_file_wo;
110 FILE *fp;
111 int file_size, i;
112 unsigned char* buf;
114 if (argc < 2) {
115 g_message ("usage: %s <file.c>", argv[0]);
116 exit(-1);
119 source_file_wo = tm_source_file_new (argv[1], FALSE);
121 if ( (fp = fopen (argv[1], "rb")) == NULL) {
122 g_message ("cannot open file for buffering...");
123 exit (-1);
126 fseek (fp, 0, SEEK_END);
127 file_size = (int)ftell (fp);
129 fseek (fp, 0, SEEK_SET);
131 buf = (unsigned char*)calloc (file_size + 1, sizeof(unsigned char));
133 memset( buf, 0, file_size +1 );
135 fread (buf, file_size, sizeof(unsigned char), fp);
137 fclose(fp);
139 g_message ("file size is %d, buf %s", file_size, buf);
140 tm_buffer_parse (TM_SOURCE_FILE(source_file_wo), buf, file_size);
142 g_message ("done");
143 if (source_file_wo->tags_array == NULL) {
144 g_message ("no tags identified");
145 return 0;
147 g_message ("total tags discovered: %d", source_file_wo->tags_array->len);
149 /* gonna list the tags, just a test */
150 for (i=0; i < source_file_wo->tags_array->len; i++) {
151 TMTag* tag;
153 tag = (TMTag*)g_ptr_array_index(source_file_wo->tags_array, i);
154 g_message ("tag %d is: %s", i, tag->name);
156 return 0;