Skip to content

Commit a9b1aaf

Browse files
First Commit
this is for the lukey boy
1 parent 0a35742 commit a9b1aaf

File tree

9 files changed

+379
-0
lines changed

9 files changed

+379
-0
lines changed

Makefile

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# $Id: Makefile,v 1.4 2016-09-21 16:56:20-07 - - $
2+
3+
GPP = g++ -std=gnu++14 -g -O0 -Wall -Wextra
4+
MKDEP = g++ -std=gnu++14 -MM
5+
6+
MKFILE = Makefile
7+
DEPFILE = Makefile.dep
8+
SOURCES = string_set.cpp main.cpp
9+
HEADERS = string_set.h
10+
OBJECTS = ${SOURCES:.cpp=.o}
11+
EXECBIN = oc
12+
SRCFILES = ${HEADERS} ${SOURCES} ${MKFILE}
13+
14+
all : ${EXECBIN}
15+
16+
${EXECBIN} : ${OBJECTS}
17+
${GPP} ${OBJECTS} -o ${EXECBIN}
18+
19+
%.o : %.cpp
20+
${GPP} -c $<
21+
22+
ci :
23+
cid + ${SRCFILES}
24+
25+
clean :
26+
-rm ${OBJECTS} ${DEPFILE}
27+
28+
spotless : clean
29+
- rm ${EXECBIN} Listing.ps Listing.pdf test.out
30+
31+
${DEPFILE} :
32+
${MKDEP} ${SOURCES} >${DEPFILE}
33+
34+
dep :
35+
- rm ${DEPFILE}
36+
${MAKE} --no-print-directory ${DEPFILE}
37+
38+
include ${DEPFILE}
39+
40+
test : ${EXECBIN}
41+
${EXECBIN} * * * >test.out 2>&1
42+
43+
lis : test
44+
mkpspdf Listing.ps ${SRCFILES} ${DEPFILE} test.out
45+
46+
again : ${SRCFILES}
47+
make --no-print-directory spotless dep ci test lis

cppstrtok.cpp

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// $Id: cppstrtok.cpp,v 1.8 2017-09-21 15:51:23-07 - - $
2+
3+
// Use cpp to scan a file and print line numbers.
4+
// Print out each input line read in, then strtok it for
5+
// tokens.
6+
7+
#include <string>
8+
using namespace std;
9+
10+
#include <errno.h>
11+
#include <libgen.h>
12+
#include <stdio.h>
13+
#include <stdlib.h>
14+
#include <string.h>
15+
#include <wait.h>
16+
17+
const string CPP = "/usr/bin/cpp -nostdinc";
18+
constexpr size_t LINESIZE = 1024;
19+
20+
// Chomp the last character from a buffer if it is delim.
21+
void chomp (char* string, char delim) {
22+
size_t len = strlen (string);
23+
if (len == 0) return;
24+
char* nlpos = string + len - 1;
25+
if (*nlpos == delim) *nlpos = '\0';
26+
}
27+
28+
// Print the meaning of a signal.
29+
static void eprint_signal (const char* kind, int signal) {
30+
fprintf (stderr, ", %s %d", kind, signal);
31+
const char* sigstr = strsignal (signal);
32+
if (sigstr != NULL) fprintf (stderr, " %s", sigstr);
33+
}
34+
35+
// Print the status returned from a subprocess.
36+
void eprint_status (const char* command, int status) {
37+
if (status == 0) return;
38+
fprintf (stderr, "%s: status 0x%04X", command, status);
39+
if (WIFEXITED (status)) {
40+
fprintf (stderr, ", exit %d", WEXITSTATUS (status));
41+
}
42+
if (WIFSIGNALED (status)) {
43+
eprint_signal ("Terminated", WTERMSIG (status));
44+
#ifdef WCOREDUMP
45+
if (WCOREDUMP (status)) fprintf (stderr, ", core dumped");
46+
#endif
47+
}
48+
if (WIFSTOPPED (status)) {
49+
eprint_signal ("Stopped", WSTOPSIG (status));
50+
}
51+
if (WIFCONTINUED (status)) {
52+
fprintf (stderr, ", Continued");
53+
}
54+
fprintf (stderr, "\n");
55+
}
56+
57+
58+
// Run cpp against the lines of the file.
59+
void cpplines (FILE* pipe, const char* filename) {
60+
int linenr = 1;
61+
char inputname[LINESIZE];
62+
strcpy (inputname, filename);
63+
for (;;) {
64+
char buffer[LINESIZE];
65+
char* fgets_rc = fgets (buffer, LINESIZE, pipe);
66+
if (fgets_rc == NULL) break;
67+
chomp (buffer, '\n');
68+
printf ("%s:line %d: [%s]\n", filename, linenr, buffer);
69+
// http://gcc.gnu.org/onlinedocs/cpp/Preprocessor-Output.html
70+
int sscanf_rc = sscanf (buffer, "# %d \"%[^\"]\"",
71+
&linenr, inputname);
72+
if (sscanf_rc == 2) {
73+
printf ("DIRECTIVE: line %d file \"%s\"\n", linenr, inputname);
74+
continue;
75+
}
76+
char* savepos = NULL;
77+
char* bufptr = buffer;
78+
for (int tokenct = 1;; ++tokenct) {
79+
char* token = strtok_r (bufptr, " \t\n", &savepos);
80+
bufptr = NULL;
81+
if (token == NULL) break;
82+
printf ("token %d.%d: [%s]\n",
83+
linenr, tokenct, token);
84+
}
85+
++linenr;
86+
}
87+
}
88+
89+
int main (int argc, char** argv) {
90+
const char* execname = basename (argv[0]);
91+
int exit_status = EXIT_SUCCESS;
92+
for (int argi = 1; argi < argc; ++argi) {
93+
char* filename = argv[argi];
94+
string command = CPP + " " + filename;
95+
printf ("command=\"%s\"\n", command.c_str());
96+
FILE* pipe = popen (command.c_str(), "r");
97+
if (pipe == NULL) {
98+
exit_status = EXIT_FAILURE;
99+
fprintf (stderr, "%s: %s: %s\n",
100+
execname, command.c_str(), strerror (errno));
101+
}else {
102+
cpplines (pipe, filename);
103+
int pclose_rc = pclose (pipe);
104+
eprint_status (command.c_str(), pclose_rc);
105+
if (pclose_rc != 0) exit_status = EXIT_FAILURE;
106+
}
107+
}
108+
return exit_status;
109+
}
110+

foo.oc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
line 1// $Id: foo.oc,v 1.1 2017-09-21 15:52:37-07 - - $
2+
__FILE__ __LINE__ __DATE__ __TIME__
3+
foo.oc, line 3.
4+
#include "foo1.oh"
5+
foo.oc, line 5.
6+
#include "foo2.oh"
7+
/* Comment */ on line 7
8+
FOO1 + FOO2;
9+
foo.oc, line 9, last line.

foo1.oh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// $Id: foo1.oh,v 1.1 2017-09-21 15:52:37-07 - - $
2+
__FILE__ __LINE__ __DATE__ __TIME__
3+
foo1.h, line 3.
4+
foo1.h, line 4.
5+
// Comment.
6+
foo1.h, line 6. /* Comment */ last line
7+
#define FOO1 "foo1"

main.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// $Id: main.cpp,v 1.2 2016-08-18 15:13:48-07 - - $
2+
3+
#include <string>
4+
using namespace std;
5+
6+
#include <assert.h>
7+
#include <stdio.h>
8+
#include <stdlib.h>
9+
#include <string.h>
10+
11+
#include "string_set.h"
12+
13+
int main (int argc, char** argv) {
14+
for (int i = 1; i < argc; ++i) {
15+
const string* str = string_set::intern (argv[i]);
16+
printf ("intern (\"%s\") returned %p->\"%s\"\n", /* eventually */
17+
argv[i], str, str->c_str()); /*change intern*/
18+
}
19+
string_set::dump (stdout);
20+
return EXIT_SUCCESS;
21+
}
22+

string_set.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// $Id: string_set.cpp,v 1.4 2016-09-21 16:56:20-07 - - $
2+
3+
#include <string>
4+
#include <unordered_set>
5+
using namespace std;
6+
7+
#include "string_set.h"
8+
9+
unordered_set<string> string_set::set;
10+
11+
string_set::string_set() {
12+
set.max_load_factor (0.5);
13+
}
14+
15+
const string* string_set::intern (const char* string) {
16+
auto handle = set.insert (string);
17+
return &*handle.first;
18+
}
19+
20+
void string_set::dump (FILE* out) {
21+
static unordered_set<string>::hasher hash_fn
22+
= string_set::set.hash_function();
23+
size_t max_bucket_size = 0;
24+
for (size_t bucket = 0; bucket < set.bucket_count(); ++bucket) {
25+
bool need_index = true;
26+
size_t curr_size = set.bucket_size (bucket);
27+
if (max_bucket_size < curr_size) max_bucket_size = curr_size;
28+
for (auto itor = set.cbegin (bucket);
29+
itor != set.cend (bucket); ++itor) {
30+
if (need_index) fprintf (out, "hash[%4zu]: ", bucket); // hash
31+
else fprintf (out, " %4s ", "");
32+
need_index = false;
33+
const string* str = &*itor;
34+
fprintf (out, "%22zu %p->\"%s\"\n", hash_fn(*str),
35+
str, str->c_str());
36+
}
37+
}
38+
// print out hash table
39+
fprintf (out, "load_factor = %.3f\n", set.load_factor());
40+
fprintf (out, "bucket_count = %zu\n", set.bucket_count());
41+
fprintf (out, "max_bucket_size = %zu\n", max_bucket_size);
42+
}
43+
44+
// 30: %4zu -- zu returns value from sizeof and 4 makes it line up nice
45+
// 30: prints bucket instead of hashed value

string_set.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// $Id: string_set.h,v 1.2 2016-08-18 15:13:48-07 - - $
2+
3+
#ifndef __STRING_SET__
4+
#define __STRING_SET__
5+
6+
#include <string>
7+
#include <unordered_set>
8+
using namespace std;
9+
10+
#include <stdio.h>
11+
12+
struct string_set {
13+
string_set();
14+
static unordered_set<string> set; // hash
15+
static const string* intern (const char*);
16+
static void dump (FILE*);
17+
};
18+
19+
#endif
20+

test.out

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
==27485== Memcheck, a memory error detector
2+
==27485== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
3+
==27485== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
4+
==27485== Command: cppstrtok foo.oc
5+
==27485==
6+
command="/usr/bin/cpp -nostdinc foo.oc"
7+
foo.oc:line 1: [# 1 "foo.oc"]
8+
DIRECTIVE: line 1 file "foo.oc"
9+
foo.oc:line 1: [# 1 "<built-in>"]
10+
DIRECTIVE: line 1 file "<built-in>"
11+
foo.oc:line 1: [# 1 "<command-line>"]
12+
DIRECTIVE: line 1 file "<command-line>"
13+
foo.oc:line 1: [# 1 "foo.oc"]
14+
DIRECTIVE: line 1 file "foo.oc"
15+
foo.oc:line 1: [line 1]
16+
token 1.1: [line]
17+
token 1.2: [1]
18+
foo.oc:line 2: ["foo.oc" 2 "Sep 21 2017" "15:52:44"]
19+
token 2.1: ["foo.oc"]
20+
token 2.2: [2]
21+
token 2.3: ["Sep]
22+
token 2.4: [21]
23+
token 2.5: [2017"]
24+
token 2.6: ["15:52:44"]
25+
foo.oc:line 3: [foo.oc, line 3.]
26+
token 3.1: [foo.oc,]
27+
token 3.2: [line]
28+
token 3.3: [3.]
29+
foo.oc:line 4: [# 1 "foo1.oh" 1]
30+
DIRECTIVE: line 1 file "foo1.oh"
31+
foo.oc:line 1: []
32+
foo.oc:line 2: ["foo1.oh" 2 "Sep 21 2017" "15:52:44"]
33+
token 2.1: ["foo1.oh"]
34+
token 2.2: [2]
35+
token 2.3: ["Sep]
36+
token 2.4: [21]
37+
token 2.5: [2017"]
38+
token 2.6: ["15:52:44"]
39+
foo.oc:line 3: [foo1.h, line 3.]
40+
token 3.1: [foo1.h,]
41+
token 3.2: [line]
42+
token 3.3: [3.]
43+
foo.oc:line 4: [foo1.h, line 4.]
44+
token 4.1: [foo1.h,]
45+
token 4.2: [line]
46+
token 4.3: [4.]
47+
foo.oc:line 5: []
48+
foo.oc:line 6: [foo1.h, line 6. last line]
49+
token 6.1: [foo1.h,]
50+
token 6.2: [line]
51+
token 6.3: [6.]
52+
token 6.4: [last]
53+
token 6.5: [line]
54+
foo.oc:line 7: [# 5 "foo.oc" 2]
55+
DIRECTIVE: line 5 file "foo.oc"
56+
foo.oc:line 5: [foo.oc, line 5.]
57+
token 5.1: [foo.oc,]
58+
token 5.2: [line]
59+
token 5.3: [5.]
60+
foo.oc:line 6: [# 1 "foo2.oh" 1]
61+
DIRECTIVE: line 1 file "foo2.oh"
62+
foo.oc:line 1: []
63+
foo.oc:line 2: ["foo2.oh" 2 "Sep 21 2017" "15:52:44"]
64+
token 2.1: ["foo2.oh"]
65+
token 2.2: [2]
66+
token 2.3: ["Sep]
67+
token 2.4: [21]
68+
token 2.5: [2017"]
69+
token 2.6: ["15:52:44"]
70+
foo.oc:line 3: [foo2.h, line 3.]
71+
token 3.1: [foo2.h,]
72+
token 3.2: [line]
73+
token 3.3: [3.]
74+
foo.oc:line 4: [foo2.h, line 4.]
75+
token 4.1: [foo2.h,]
76+
token 4.2: [line]
77+
token 4.3: [4.]
78+
foo.oc:line 5: []
79+
foo.oc:line 6: [foo2.h, line 6. last line]
80+
token 6.1: [foo2.h,]
81+
token 6.2: [line]
82+
token 6.3: [6.]
83+
token 6.4: [last]
84+
token 6.5: [line]
85+
foo.oc:line 7: [# 7 "foo.oc" 2]
86+
DIRECTIVE: line 7 file "foo.oc"
87+
foo.oc:line 7: [ on line 7]
88+
token 7.1: [on]
89+
token 7.2: [line]
90+
token 7.3: [7]
91+
foo.oc:line 8: ["foo1" + "foo2";]
92+
token 8.1: ["foo1"]
93+
token 8.2: [+]
94+
token 8.3: ["foo2";]
95+
foo.oc:line 9: [foo.oc, line 9, last line.]
96+
token 9.1: [foo.oc,]
97+
token 9.2: [line]
98+
token 9.3: [9,]
99+
token 9.4: [last]
100+
token 9.5: [line.]
101+
==27485==
102+
==27485== HEAP SUMMARY:
103+
==27485== in use at exit: 0 bytes in 0 blocks
104+
==27485== total heap usage: 3 allocs, 3 frees, 372 bytes allocated
105+
==27485==
106+
==27485== All heap blocks were freed -- no leaks are possible
107+
==27485==
108+
==27485== For counts of detected and suppressed errors, rerun with: -v
109+
==27485== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

test3.oc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// $Id: 03-test3.oc,v 1.2 2012-12-03 13:17:30-08 - - $
2+
3+
#include "oclib.oh"
4+
5+
int a = 3;
6+
int b = 8;
7+
int c = a + b;
8+
a = b + c;
9+
puti (a);
10+
putc ('\n');

0 commit comments

Comments
 (0)