Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add example

  • Loading branch information...
commit 1567d091ae682fb8c3159bf5ebbe4a98e4232baf 1 parent c2763ae
@jvirtanen authored
Showing with 77 additions and 2 deletions.
  1. +1 −0  .gitignore
  2. +13 −2 Makefile
  3. +63 −0 examples/yahoo-finance.c
View
1  .gitignore
@@ -1,2 +1,3 @@
*.o
/libfields.*
+/examples/yahoo-finance
View
15 Makefile
@@ -1,4 +1,5 @@
CC ?= gcc
+LD := $(CC)
PREFIX ?= /usr/local
@@ -46,6 +47,9 @@ ifeq ($(uname_S),Darwin)
SONAME := -install_name,$(SHARED_LIB_MAJOR)
endif
+OBJS += examples/yahoo-finance.o
+PROG := examples/yahoo-finance
+
V =
ifeq ($(strip $(V)),)
E := @echo
@@ -55,15 +59,18 @@ else
Q :=
endif
-all: test
+all: examples test
.PHONY: all
clean:
$(E) " CLEAN "
- $(Q) $(RM) $(LIB_OBJS) $(SHARED_LIB) $(STATIC_LIB)
+ $(Q) $(RM) $(LIB_OBJS) $(OBJS) $(PROG) $(SHARED_LIB) $(STATIC_LIB)
$(Q) cd python; $(MAKE) clean
.PHONY: clean
+examples: $(PROG)
+.PHONY: examples
+
install: $(SHARED_LIB) $(STATIC_LIB)
$(E) " INSTALL "
$(Q) mkdir -p $(PREFIX)/include $(PREFIX)/lib
@@ -87,6 +94,10 @@ $(STATIC_LIB): $(LIB_OBJS)
$(E) " ARCHIVE " $@
$(Q) $(AR) rcs $@ $^
+$(PROG): $(LIB_OBJS) $(OBJS)
+ $(E) " LINK " $@
+ $(Q) $(LD) $(LDFLAGS) -o $@ $^
+
%.o: %.c
$(E) " COMPILE " $@
$(Q) $(CC) $(CFLAGS) -c -o $@ $<
View
63 examples/yahoo-finance.c
@@ -0,0 +1,63 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "fields.h"
+
+/*
+ * Read tabular text data in the Yahoo! Finance Historical Prices format from
+ * the standard input, print adjusted close prices to the standard output.
+ *
+ * For testing, you can download the data for SPDR S&P 500 (SPY) at:
+ *
+ * http://finance.yahoo.com/q/hp?s=SPY
+ */
+
+static void die(const char *);
+
+int
+main(void)
+{
+ struct fields_reader *reader;
+ struct fields_record *record;
+
+ reader = fields_read_file(stdin, &fields_csv);
+ if (reader == NULL)
+ die("fields_read_file");
+
+ record = fields_record_alloc(&fields_csv);
+ if (record == NULL)
+ die("fields_record_alloc");
+
+ /* Skip the header. */
+ fields_reader_read(reader, record);
+
+ while (fields_reader_read(reader, record) == 0) {
+ struct fields_field date;
+ struct fields_field price;
+
+ /* Date, Open, High, Low, Close, Volume and Adj Close. */
+ if (fields_record_size(record) != 7)
+ die("fields_record_size");
+
+ /* Date and Adj Close. */
+ fields_record_field(record, 0, &date);
+ fields_record_field(record, 6, &price);
+
+ printf("%s\t%s\n", date.value, price.value);
+ }
+
+ if (fields_reader_error(reader) != 0)
+ die("fields_reader_error");
+
+ fields_record_free(record);
+ fields_reader_free(reader);
+
+ return 0;
+}
+
+static void
+die(const char *message)
+{
+ fprintf(stderr, "%s\n", message);
+ exit(EXIT_FAILURE);
+}
Please sign in to comment.
Something went wrong with that request. Please try again.