Skip to content

Instantly share code, notes, and snippets.

@tj
Created October 26, 2015 19:09
Show Gist options
  • Save tj/7ae3565b33dfe98d4b75 to your computer and use it in GitHub Desktop.
Save tj/7ae3565b33dfe98d4b75 to your computer and use it in GitHub Desktop.
{ type: 'Program',
body:
[ { type: 'FunctionDeclaration',
id: { type: 'Identifier', name: 'main' },
params: [],
kind: { type: 'Identifier', name: 'int' },
body:
{ type: 'BlockStatement',
body:
[ { type: 'VariableStatement',
declarations:
[ { type: 'VariableDeclaration',
id: { type: 'Identifier', name: 'path' },
kind: null } ],
init:
{ type: 'Literal',
value: 'logs.json' } },
{ type: 'VariableStatement',
declarations:
[ { type: 'VariableDeclaration',
id: { type: 'Identifier', name: 'fp' },
kind: { type: 'Identifier', name: 'file' } } ],
init:
{ type: 'CallExpression',
callee: { type: 'Identifier', name: 'fopen' },
args:
[ { type: 'Identifier', name: 'path' },
{ type: 'Literal', value: 'r' } ] } },
{ type: 'IfStatement',
test:
{ type: 'BinaryExpression',
operator: '==',
left: { type: 'Identifier', name: 'fp' },
right: { type: 'Literal', value: null } },
consequent:
{ type: 'BlockStatement',
body:
[ { type: 'ExpressionStatement',
expression:
{ type: 'CallExpression',
callee: { type: 'Identifier', name: 'printf' },
args:
[ { type: 'Literal', value: 'error opening %s' },
{ type: 'Identifier', name: 'path' } ] } },
{ type: 'ExpressionStatement',
expression:
{ type: 'CallExpression',
callee: { type: 'Identifier', name: 'exit' },
args: [ { type: 'Literal', value: 1 } ] } } ] },
alternate: null },
{ type: 'VariableStatement',
declarations:
[ { type: 'VariableDeclaration',
id: { type: 'Identifier', name: 'line' },
kind: { type: 'Identifier', name: 'string' } } ],
init: { type: 'Literal', value: null } },
{ type: 'VariableStatement',
declarations:
[ { type: 'VariableDeclaration',
id: { type: 'Identifier', name: 'len' },
kind: { type: 'Identifier', name: 'size_t' } } ],
init: null },
{ type: 'VariableStatement',
declarations:
[ { type: 'VariableDeclaration',
id: { type: 'Identifier', name: 'read' },
kind: { type: 'Identifier', name: 'ssize_t' } } ],
init: null },
{ type: 'VariableStatement',
declarations:
[ { type: 'VariableDeclaration',
id: { type: 'Identifier', name: 'total' },
kind: { type: 'Identifier', name: 'ssize_t' } } ],
init: null },
{ type: 'WhileStatement',
test: { type: 'Literal', value: 1 },
body:
{ type: 'BlockStatement',
body:
[ { type: 'ExpressionStatement',
expression:
{ type: 'AssignmentExpression',
operator: '=',
left: { type: 'Identifier', name: 'read' },
right:
{ type: 'CallExpression',
callee: { type: 'Identifier', name: 'getline' },
args:
[ { type: 'Identifier', name: 'line', addr: true },
{ type: 'Identifier', name: 'len', addr: true },
{ type: 'Identifier', name: 'fp' } ] } } },
{ type: 'IfStatement',
test:
{ type: 'BinaryExpression',
operator: '==',
left: { type: 'Identifier', name: 'read' },
right:
{ type: 'UnaryExpression',
operator: '-',
arg: { type: 'Literal', value: 1 },
prefix: true } },
consequent: { type: 'BreakStatement', label: null } },
{ type: 'ExpressionStatement',
expression:
{ type: 'AssignmentExpression',
operator: '+=',
left: { type: 'Identifier', name: 'total' },
right: { type: 'Identifier', name: 'read' } } },
{ type: 'ExpressionStatement',
expression:
{ type: 'CallExpression',
callee: { type: 'Identifier', name: 'printf' },
args:
[ { type: 'Literal', value: '\r %zd' },
{ type: 'Identifier', name: 'total' } ] } } ] } },
{ type: 'ReturnStatement', arg: { type: 'Literal', value: 0 } } ] } } ] }
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int
main () {
FILE * fp = fopen("logs.json", "r");
if (fp == NULL) {
printf("error opening %s", path);
exit(1);
}
char * line = NULL;
size_t len;
ssize_t read;
ssize_t total;
while (1) {
read = getline(&line, &len, fp);
if (read == -1) break;
total += read;
printf("\r %zd", total);
}
return 0;
}
fn main() int {
let fp file = fopen("logs.json", "r")
if fp == null {
printf("error opening %s", path)
exit(1)
}
let line string = null
let len size_t
let read ssize_t
let total ssize_t
for {
read = getline(&line, &len, fp)
break if read == -1
total += read
printf("\r %zd", total)
}
return 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment