我正在使用gtk + -2.0 gtksourceview-2.0创建一个文本编辑器。我希望能够保存笔记本中的选项卡,以便在我关闭编辑器并在以后重新打开它之后使用。在gtk_notebook_ *下的devhelp中我看不到任何看起来很有希望的东西。我可以在数据库表中保存这些文件的路径,然后从数据库中读取表并在应用程序启动时创建选项卡,但这看起来有点笨拙。许多编辑都内置了这个功能。对于一个geany,但我知道还有其他的。
gtk可以吗?我对C比较新,有没有其他方法来存储这些路径(除了在数据库中)?感谢。
答案 0 :(得分:1)
GtkNotebook无法为您执行此操作,您必须编写一些代码来存储应用程序的状态,然后在应用程序启动时加载它。如果那是打开文件的路径列表,那很好。不确定为什么你认为这是“笨重”?该工具包不会神奇地了解您的应用程序的详细信息。
答案 1 :(得分:0)
我最初认为写入配置文件/从配置文件写入会很慢,我对数据库的写入有类似的想法。虽然提供了一些很好的建议:sqlite和配置文件解析器,但最终我决定以旧式方式在文本文件中写入/读取几行不会那么昂贵。
在将这些概念合并到我的编辑器之前,我将一个演示程序放在一起,我在下面提供了这个。随意批评这个程序,特别是在内存使用方面。该程序演示了以下步骤:
(如果我列出一个列表似乎弄乱了我的代码块)
1)检查配置文件是否存在,2)删除配置文件(如果存在),3)写入配置文件的路径,4)从配置文件中读取路径
/*
* Compile Command:
* gcc ledit_config.c -o ledit_config
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h> // system
#define NUM_TABS 10
char paths[NUM_TABS][200];
void write_config();
void read_config();
int main ()
{
write_config();
read_config();
}
void write_config()
{
char *config_file;
char temp[200];
int i=0;
/* change to the user's home directory (for fopen) */
(int)chdir(getenv("HOME"));
config_file = ".config/ledit/files";
/* populate paths array with random paths */
strcpy(paths[0], "~/Documents/code/glade/tutorial1/scratch_files/scratch.py");
strcpy(paths[4], "~/Documents/code/glade/tutorial1/scratch_files/scratch.c");
strcpy(paths[7], "~/Documents/code/glade/tutorial1/scratch_files/scratch.glade");
if (fopen(config_file, "r") == NULL) /* file does not exist */
{
system("mkdir -p $HOME/.config/ledit/");
FILE *fp;
fp=fopen(config_file, "w");
for(i = 0;i < NUM_TABS;++i)
{
strcpy(temp,paths[i]);
strcat(temp, "\n");
if (strlen(temp) > strlen("\n")) /* check to see if element actually contains more than just "\n" */
{
fprintf(fp, "%s",temp);
}
}
fclose(fp);
}
else /* file does exist */
{
system("rm $HOME/.config/ledit/files");
FILE *fp;
fp=fopen(config_file, "w");
for(i = 0;i < NUM_TABS;++i)
{
strcpy(temp,paths[i]);
strcat(temp, "\n");
if (strlen(temp) > strlen("\n")) /* check to see if element actually contains more than just "\n" */
{
fprintf(fp, "%s",temp);
}
}
fclose(fp);
}
}
void read_config()
{
char line[200];
char *config_file;
int i=0;
/* change to the user's home directory (for fopen) */
(int)chdir(getenv("HOME"));
config_file = ".config/ledit/files";
/* empty the paths array */
for(i = 0;i < NUM_TABS;++i)
strcpy(paths[i], "");
/* read the config file and poplulate array */
i = 0;
FILE* fp = fopen(config_file,"r");
while(fgets(line,sizeof(line),fp) != NULL)
{
strcpy(paths[i], line);
i++;
}
fclose(fp);
/* print out paths array for verification */
for(i = 0;i < NUM_TABS;++i)
printf("%s",paths[i]);
}