c - 如何从 gtktextview/gtksourceview 解析 C 中的文本

标签 c gtk gtksourceview

我正在使用 gtk+-2.0 和 gtksourceview-2.0 用 C 编写一个文本编辑器。我正在尝试对从缓冲区中提取的数据运行一些测试。目前,我正在移动一对迭代器,然后获取由这些迭代器分隔的文本 block 。然后使用 strstr、strchr、...检查此字符串。但是,这个移动迭代器、然后读取数据、然后执行测试的过程非常繁琐。

我还提取了整个文档(使用前面描述的过程)到 gchar *。处理这些数据会更容易吗?我尝试对这些数据使用 fgets 和 getline,但编译器提示:

warning: passing argument 3 of ‘fgets’ from incompatible pointer type /usr/include/stdio.h:624:14: note: expected ‘struct FILE * __restrict__’ but argument is of type ‘gchar *’

warning: passing argument 1 of ‘getline’ from incompatible pointer type /usr/include/stdio.h:671:20: note: expected ‘char ** __restrict__’ but argument is of type ‘gchar *’

我试过了:

  int bytes_read;
  int nbytes = 100;
  char *my_string;
  GtkTextIter start;
  GtkTextIter end;

  wholeDoc = gtk_text_buffer_get_text(tbuffer,&start,&end,TRUE);

  my_string = (char *) malloc (nbytes + 1);
  bytes_read = getline (&my_string, &nbytes, wholeDoc);

但出现以下错误:

warning: pointer targets in passing argument 2 of ‘getline’ differ in signedness /usr/include/stdio.h:671:20: note: expected ‘size_t * __restrict__’ but argument is of type ‘int *’

我正在寻找一种检查方法,例如,通过简单地从循环索引中减去一个来检查前一行,就像每一行都是数组的一个元素一样。我怎样才能把数据放到这个表格中?请耐心等待,我正在学习。谢谢。

最佳答案

这是一个如何从 GtkTexBuffer 中获取行的简单示例。

  GtkTextIter start_iter, next_iter;
  gchar *text;

  gtk_text_buffer_get_iter_at_offset (source_buffer, &start_iter, 0);

  next_iter = start_iter;
  while (gtk_text_iter_forward_line (&next_iter))
    {    
      text = gtk_text_iter_get_text (&start_iter, &next_iter);

      // line processing

      g_free (text);
      start_iter = next_iter;
    }    

有关更多详细信息,请阅读 documentation的 GtkTextIter。

关于c - 如何从 gtktextview/gtksourceview 解析 C 中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10626825/

相关文章:

c++ - 为了使用 Glib::wrap,必须 Glib::init()'ed 什么?

c - 为什么宏值为0而不是1?

c++ - 将数组从 C/C++ 传递到 Fortran 并返回计算后的数组

windows - 是否有适用于 Windows 的 Glade3 安装程序?

c# - 使用 FileChooserDialog GtkSharp 解析文件路径

c - gtk_source_mark_attributes_render_icon 的第二个参数

c - C中字符串压缩的ascii输出

objective-c - OpenGL 在 Mac、Objective-C 上什么都不画

c++ - 编译 gobject-introspection-1.44.0 时获取错误列表

c - 如何保存 gtk 笔记本选项卡以供稍后 session 使用