r2295 - alfs-POC/src

jhuntwork at linuxfromscratch.org jhuntwork at linuxfromscratch.org
Thu Mar 2 11:20:48 PST 2006


Author: jhuntwork
Date: 2006-03-02 12:20:47 -0700 (Thu, 02 Mar 2006)
New Revision: 2295

Added:
   alfs-POC/src/parser.cpp
Modified:
   alfs-POC/src/Makefile
Log:
Added initial version of the parser in C++

Modified: alfs-POC/src/Makefile
===================================================================
--- alfs-POC/src/Makefile	2006-02-21 15:55:06 UTC (rev 2294)
+++ alfs-POC/src/Makefile	2006-03-02 19:20:47 UTC (rev 2295)
@@ -1,17 +1,22 @@
 CC = gcc
-FLAGS = -Wall -Werror -pedantic -O2 -pipe -std=gnu99 -D_GNU_SOURCE
-BINARIES = parser alfs alfsd
+CXX = g++
+CFLAGS = -Wall -Werror -pedantic -O2 -pipe -std=gnu99 -D_GNU_SOURCE
+CXXFLAGS = -Wall -Werror -pedantic -pipe -D_GNU_SOURCE
+BINARIES = parser alfs alfsd cpp-parser
 
 # List 'clean' as a dependency for now so that it will always clean
 # before it builds.
 all: clean $(BINARIES)
 
 alf%:
-	$(CC) $(FLAGS) $@.c -o $@
+	$(CC) $(CFLAGS) $@.c -o $@
 
 parser: parser.c
-	$(CC) $(FLAGS) $@.c -o $@
+	$(CC) $(CFLAGS) $@.c -o $@
 
+cpp-parser: parser.cpp
+	$(CXX) $(CXXFLAGS) parser.cpp -o $@
+
 clean:
 	-rm -f $(BINARIES)
 

Added: alfs-POC/src/parser.cpp
===================================================================
--- alfs-POC/src/parser.cpp	                        (rev 0)
+++ alfs-POC/src/parser.cpp	2006-03-02 19:20:47 UTC (rev 2295)
@@ -0,0 +1,70 @@
+#include <iostream>
+#include <fstream>
+#include <string>
+using namespace std;
+
+int parse_file(string filename);
+
+int main (int argc, char **argv)
+{
+  /* Check that we have an input file */
+  if (argc != 2){
+        printf("Usage: %s [FILE]\n", argv[0]);
+        return(-1);
+  }
+
+  /* We've been given a file, let's start parsing */
+  string fn = argv[1];
+  if ((parse_file(fn)) == -1)
+        return(-1);
+
+  return(0);
+
+}
+
+int parse_file(string filename){
+
+  int loc, len;
+  string fn, parent, curline;
+  const char *dir, *file;
+  char wd[512];
+  ifstream fp;
+  
+  len = filename.length();
+
+  /* Isolate the path to the file from the actual filename. */
+  if ((loc = filename.find_last_of("/")) == -1)
+    fn = filename;  
+  else {
+    string path(filename, 0, loc);
+    fn = string(filename, loc+1, len-loc);
+    if ((loc = path.find_last_of("/")) != -1)
+       parent = string(path, 0, loc);
+    dir = path.c_str();
+    if ((chdir(dir)) == -1)
+	cerr << "Cannot change directories to " << path << endl;
+  }
+ 
+  /* Open the file for parsing. */ 
+  cout << "Parsing file: " << fn << endl;
+  file = fn.c_str();
+  fp.open(file);
+  if (!fp)
+    cerr << "Cannot open file: " << fn << endl;
+
+  /* Set the current working directory, in case we leave it,
+     and so we can get the name of the parent directory for
+     the file we're parsing */
+  getcwd(wd, 256);
+
+  /* Read one line at a time from the opened file
+     until the end of the file is reached */
+  while (!fp.eof()) {
+    getline(fp, curline);
+    cout << "Current line: " << curline << endl;	
+  }
+
+  fp.close();
+
+  return(0);
+}




More information about the alfs-log mailing list