[gnome-builder] util: add IdeLineReader utility structure
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder] util: add IdeLineReader utility structure
- Date: Fri, 15 May 2015 23:30:19 +0000 (UTC)
commit 401dafe5dd9f5d49a449b3db8999ae6bbc406510
Author: Christian Hergert <christian hergert me>
Date: Fri May 15 16:00:26 2015 -0700
util: add IdeLineReader utility structure
It's pretty common to have to walk through a file a line at a time, so
lets make this more abstract. It's also helpful when you want to zero
line endings to turn a contiguous buffer into a bunch of smaller strings.
libide/Makefile.am | 2 +
libide/autotools/ide-makecache.c | 71 +++--------------------------
libide/util/ide-line-reader.c | 91 ++++++++++++++++++++++++++++++++++++++
libide/util/ide-line-reader.h | 41 +++++++++++++++++
4 files changed, 141 insertions(+), 64 deletions(-)
---
diff --git a/libide/Makefile.am b/libide/Makefile.am
index 348c717..95304a9 100644
--- a/libide/Makefile.am
+++ b/libide/Makefile.am
@@ -306,6 +306,8 @@ libide_1_0_la_SOURCES = \
util/ide-cairo.h \
util/ide-doc-seq.c \
util/ide-doc-seq.h \
+ util/ide-line-reader.c \
+ util/ide-line-reader.h \
util/ide-pango.c \
util/ide-pango.h \
util/ide-rgba.c \
diff --git a/libide/autotools/ide-makecache.c b/libide/autotools/ide-makecache.c
index 4d2cca5..cd6dbad 100644
--- a/libide/autotools/ide-makecache.c
+++ b/libide/autotools/ide-makecache.c
@@ -40,6 +40,7 @@
#include "ide-makecache.h"
#include "ide-makecache-target.h"
#include "ide-project.h"
+#include "ide-line-reader.h"
#include "ide-thread-pool.h"
#include "ide-vcs.h"
@@ -72,13 +73,6 @@ typedef struct
gchar *path;
} FileTargetsLookup;
-typedef struct
-{
- const gchar *contents;
- gsize length;
- gssize pos;
-} ReadLines;
-
G_DEFINE_TYPE (IdeMakecache, ide_makecache, IDE_TYPE_OBJECT)
EGG_DEFINE_COUNTER (instances, "IdeMakecache", "Instances", "The number of IdeMakecache")
@@ -92,60 +86,6 @@ enum {
static GParamSpec *gParamSpecs [LAST_PROP];
static void
-read_lines_init (ReadLines *rl,
- const gchar *contents,
- gsize length)
-{
- g_assert (rl);
- g_assert (contents);
-
- if (length < G_MAXSIZE)
- {
- rl->contents = contents;
- rl->length = length;
- rl->pos = 0;
- }
- else
- {
- rl->contents = NULL;
- rl->length = 0;
- rl->pos = 0;
- }
-}
-
-static const gchar *
-read_lines_next (ReadLines *rl,
- gsize *length)
-{
- const gchar *ret = NULL;
-
- g_assert (rl);
- g_assert (length);
-
- if ((rl->contents == NULL) || (rl->pos >= rl->length))
- {
- *length = 0;
- return NULL;
- }
-
- ret = &rl->contents [rl->pos];
-
- for (; rl->pos < rl->length; rl->pos++)
- {
- if (rl->contents [rl->pos] == '\n')
- {
- *length = &rl->contents [rl->pos] - ret;
- rl->pos++;
- return ret;
- }
- }
-
- *length = &rl->contents [rl->pos] - ret;
-
- return ret;
-}
-
-static void
file_flags_lookup_free (gpointer data)
{
FileFlagsLookup *lookup = data;
@@ -321,7 +261,7 @@ ide_makecache_get_file_targets_searched (GMappedFile *mapped,
g_autoptr(GRegex) regex = NULL;
const gchar *content;
const gchar *line;
- ReadLines rl = { 0 };
+ IdeLineReader rl;
gsize len;
gsize line_len;
@@ -359,9 +299,12 @@ ide_makecache_get_file_targets_searched (GMappedFile *mapped,
}
#endif
- read_lines_init (&rl, content, len);
+ if (len > G_MAXSSIZE)
+ IDE_RETURN (NULL);
+
+ ide_line_reader_init (&rl, (gchar *)content, len);
- while ((line = read_lines_next (&rl, &line_len)))
+ while ((line = ide_line_reader_next (&rl, &line_len)))
{
/*
* Keep track of "subdir = <dir>" changes so we know what directory
diff --git a/libide/util/ide-line-reader.c b/libide/util/ide-line-reader.c
new file mode 100644
index 0000000..690db7c
--- /dev/null
+++ b/libide/util/ide-line-reader.c
@@ -0,0 +1,91 @@
+/* ide-line-reader.c
+ *
+ * Copyright (C) 2015 Christian Hergert <christian hergert me>
+ *
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#define G_LOG_DOMAIN "ide-line-reader"
+
+#include <string.h>
+
+#include "ide-line-reader.h"
+
+void
+ide_line_reader_init (IdeLineReader *reader,
+ gchar *contents,
+ gssize length)
+{
+ g_assert (reader);
+
+ if (length < 0)
+ length = strlen (contents);
+
+ if ((contents != NULL) && (length <= G_MAXSSIZE))
+ {
+ reader->contents = contents;
+ reader->length = length;
+ reader->pos = 0;
+ }
+ else
+ {
+ reader->contents = NULL;
+ reader->length = 0;
+ reader->pos = 0;
+ }
+}
+
+/**
+ * ide_line_reader_next:
+ * @reader: the #IdeLineReader
+ * @length: a location for the length of the line in bytes.
+ *
+ * Moves forward to the beginning of the next line in the buffer. No changes to the buffer
+ * are made, and the result is a pointer within the string passed as @contents in
+ * ide_line_reader_init(). Since the line most likely will not be terminated with a NULL byte,
+ * you must provide @length to determine the length of the line.
+ *
+ * Returns: (transfer none): The beginning of the line within the buffer.
+ */
+gchar *
+ide_line_reader_next (IdeLineReader *reader,
+ gsize *length)
+{
+ gchar *ret = NULL;
+
+ g_assert (reader);
+ g_assert (length != NULL);
+
+ if ((reader->contents == NULL) || (reader->pos >= reader->length))
+ {
+ *length = 0;
+ return NULL;
+ }
+
+ ret = &reader->contents [reader->pos];
+
+ for (; reader->pos < reader->length; reader->pos++)
+ {
+ if (reader->contents [reader->pos] == '\n')
+ {
+ *length = &reader->contents [reader->pos] - ret;
+ reader->pos++;
+ return ret;
+ }
+ }
+
+ *length = &reader->contents [reader->pos] - ret;
+
+ return ret;
+}
diff --git a/libide/util/ide-line-reader.h b/libide/util/ide-line-reader.h
new file mode 100644
index 0000000..aa5d64d
--- /dev/null
+++ b/libide/util/ide-line-reader.h
@@ -0,0 +1,41 @@
+/* ide-line-reader.h
+ *
+ * Copyright (C) 2015 Christian Hergert <christian hergert me>
+ *
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef IDE_LINE_READER_H
+#define IDE_LINE_READER_H
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+typedef struct
+{
+ gchar *contents;
+ gsize length;
+ gssize pos;
+} IdeLineReader;
+
+void ide_line_reader_init (IdeLineReader *reader,
+ gchar *contents,
+ gssize length);
+gchar *ide_line_reader_next (IdeLineReader *reader,
+ gsize *length);
+
+G_END_DECLS
+
+#endif /* IDE_LINE_READER_H */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]