From e79f3c775cb1957e09716d7782d91da33fa8f0c8 Mon Sep 17 00:00:00 2001 From: epochqwert Date: Sat, 7 Feb 2015 04:33:50 -0600 Subject: The stuff I use to generate my anonet map. check out paths-to-edges. :> --- misc/svgmap/cached.sh | 11 ++++ misc/svgmap/clear_cache.sh | 2 + misc/svgmap/generate.sh | 60 ++++++++++++++++++++ misc/svgmap/paths-to-edges.c | 132 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 205 insertions(+) create mode 100755 misc/svgmap/cached.sh create mode 100755 misc/svgmap/clear_cache.sh create mode 100755 misc/svgmap/generate.sh create mode 100644 misc/svgmap/paths-to-edges.c (limited to 'misc') diff --git a/misc/svgmap/cached.sh b/misc/svgmap/cached.sh new file mode 100755 index 0000000..4c8c28e --- /dev/null +++ b/misc/svgmap/cached.sh @@ -0,0 +1,11 @@ +#!/bin/sh +/usr/pkg/sbin/nc 127.0.0.1 64777 > /var/cache/svgmap/data.new +if cmp /var/cache/svgmap/data.new \ + /var/cache/svgmap/data >/dev/null 2>&1;then + cat /var/cache/svgmap/output +else + cp /var/cache/svgmap/data.new /var/cache/svgmap/data + cat /var/cache/svgmap/data \ + | /var/www/libexec/svgmap/generate.sh \ + | tee /var/cache/svgmap/output +fi diff --git a/misc/svgmap/clear_cache.sh b/misc/svgmap/clear_cache.sh new file mode 100755 index 0000000..f95289f --- /dev/null +++ b/misc/svgmap/clear_cache.sh @@ -0,0 +1,2 @@ +#!/bin/sh +rm /var/cache/svgmap/* diff --git a/misc/svgmap/generate.sh b/misc/svgmap/generate.sh new file mode 100755 index 0000000..209f83b --- /dev/null +++ b/misc/svgmap/generate.sh @@ -0,0 +1,60 @@ +#!/bin/sh +export PATH=$PATH:/usr/pkg/bin:/usr/pkg/sbin:/usr/local/bin:/usr/local/sbin + +echo '' + +cat > /var/cache/svgmap/data.tmp + +nodes=$(cat /var/cache/svgmap/data.tmp \ + | /var/www/libexec/svgmap/paths-to-edges \ + | tr ' ' '\n' \ + | sort \ + | uniq ) + +nn=$(echo $nodes | tr ' ' '\n' | wc -l | tr -d ' ') + +scale=200 +xoff=250 +yoff=250 + +cat /var/cache/svgmap/data.tmp | /var/www/libexec/svgmap/paths-to-edges > /var/cache/svgmap/edges.list + +nsin="" +ncos="" +lsin="" +lcos="" +for i in $(seq 0 $nn);do + tmpsin="$(echo | awk "{ print ( ( sin( ( ${i}.0 / ${nn}.0) * 2.0 * atan2(0,-1) ) ) ) }")" + tmpcos="$(echo | awk "{ print ( ( cos( ( ${i}.0 / ${nn}.0) * 2.0 * atan2(0,-1) )) ) }")" + scale=200 + nsin="$nsin $(echo | awk "{print $scale * $tmpsin + $xoff}")" + ncos="$ncos $(echo | awk "{print $scale * $tmpcos + $yoff}")" + scale=180 + lsin="$lsin $(echo | awk "{print $scale * $tmpsin + $xoff}")" + lcos="$lcos $(echo | awk "{print $scale * $tmpcos + $yoff}")" +done + +for i in $nodes;do + node1n=$(echo $nodes | tr ' ' '\n' | grep -n '^'$i'$' | cut -d: -f1) + x="$(echo $nsin | cut '-d ' -f$node1n)" + y="$(echo $ncos | cut '-d ' -f$node1n)" + printf '' + printf '' "$i" + printf '' "$i" "$x" "$y" + printf '%s' "$x" "$y" "$x" "$y" "$i" + echo "" + cat /var/cache/svgmap/edges.list | grep "\b$i\b" | while read edge;do + node1=$(echo $edge | cut '-d ' -f1) + node2=$(echo $edge | cut '-d ' -f2) + node1n=$(echo $nodes | tr ' ' '\n' | grep -n '^'$node1'$' | cut -d: -f1) + node2n=$(echo $nodes | tr ' ' '\n' | grep -n '^'$node2'$' | cut -d: -f1) + x1="$(echo $lsin | cut '-d ' -f$node1n)" + y1="$(echo $lcos | cut '-d ' -f$node1n)" + x2="$(echo $lsin | cut '-d ' -f$node2n)" + y2="$(echo $lcos | cut '-d ' -f$node2n)" + printf '\n' "$x1" "$y1" "$x2" "$y2" + done + printf "\n" +done + +echo '' diff --git a/misc/svgmap/paths-to-edges.c b/misc/svgmap/paths-to-edges.c new file mode 100644 index 0000000..526f890 --- /dev/null +++ b/misc/svgmap/paths-to-edges.c @@ -0,0 +1,132 @@ +#include +#include +#include + +struct link { + char *a; + char *b; + int count; +}; + +struct link ** get_links(char ***data) { + int i,j,k=0; + for(i=0;data[i];i++) { + for(j=0;data[i][j];j++) { + k++;//this is overkill because I can't remove dedups that won't need + //new space allocated. + } + } + struct link **link=malloc(sizeof(struct link *) * k+1); //??? + memset(link,0,sizeof(struct link *) * k+1); + for(i=0;data[i];i++) { + for(j=0;data[i][j+1];j++) { + for(k=0;link[k];k++) { + if(!strcmp(link[k]->a,data[i][j]) && !strcmp(link[k]->b,data[i][j+1]) || + !strcmp(link[k]->b,data[i][j]) && !strcmp(link[k]->a,data[i][j+1])) { + break; + } + } + if(!link[k]) { + link[k]=malloc(sizeof(struct link)); + link[k]->a=strdup(data[i][j]); + link[k]->b=strdup(data[i][j+1]); + link[k]->count=1; + } else { + link[k]->count++; + } + } + } + return link; +} + +char *recurse_readline(int chars); + +char **recurse_readfile(int lines) { + char **malloced; + char *line=recurse_readline(0); + if(feof(stdin)) { + malloced=malloc(sizeof(char *) * (lines+1)); + malloced[lines]=0; + return malloced; + } + malloced=recurse_readfile(lines+1); + malloced[lines]=line; + return malloced; +} + +char *recurse_readline(int chars) { + char *malloced; + short in=fgetc(stdin); + if(in == '\n' || in == -1) { + malloced=malloc(chars+2); + malloced[chars]='\n'; + malloced[chars+1]=0; + return malloced; + } + malloced=recurse_readline(chars+1); + malloced[chars]=in; + return malloced; +} + +//this is the function failing on full-size data. +char ***lines_to_data(char **lines) { + char ***data; + int i,j,k,n,m; + for(i=0;lines[i];i++); + data=malloc(sizeof(char **) * i+1); + data[i]=0; + + for(i=0;lines[i];i++) { + k=0; + for(j=0;lines[i][j];j++) { + if(lines[i][j]==' ' || lines[i][j] == '\n' || lines[i][j] == '\0') k++; + } + data[i]=malloc(sizeof(char *) * k+1); + data[i+1]=0; + data[i][k]=0; + } +/* in this for loop somewhere. */ + for(i=0;lines[i];i++) { + k=0; + n=0; + for(j=0;lines[i][j];j++) { + if(lines[i][j]==' ' || lines[i][j]=='\n' || lines[i][j]=='\0') { + data[i][n]=strndup(lines[i]+k,j-k); + data[i][n][j-k]=0; + n++; + k=j+1; + } + } + } + return data; +} + +int main() { + int i,j; + char **lines; + char ***data; + struct link **link; + lines=recurse_readfile(0); +/* for(i=0;lines[i];i++) { + printf("lines[%d]=\"%s\"\n",i,lines[i]); + }*/ + data=lines_to_data(lines); +/* for(i=0;data[i];i++) { + for(j=0;data[i][j];j++) { + printf("data[%d][%d]='%s'\n",i,j,data[i][j]); + } + printf("\n"); + } +*/ + link=get_links(data); + //printf("//graph generated by paths-to-edges to DOT format\n"); + //printf("graph anonet {\n"); + for(i=0;link[i];i++) { + //printf("%s -> %s (%d)\n",link[i]->a,link[i]->b,link[i]->count); + //printf("%s -- %s;\n",link[i]->a,link[i]->b,link[i]->count); + printf("%s %s\n",link[i]->a,link[i]->b); + } + //printf("}\n"); + + return 0; +} -- cgit v1.2.3