File: main.cpp

package info (click to toggle)
codelite 12.0+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 95,112 kB
  • sloc: cpp: 424,040; ansic: 18,284; php: 9,569; lex: 4,186; yacc: 2,820; python: 2,294; sh: 312; makefile: 51; xml: 13
file content (56 lines) | stat: -rw-r--r-- 1,383 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <errno.h>
#include <string.h>

#include "scope_optimizer.h"
char* loadFile(const char* fileName);

int main(int argc, char** argv)
{
    std::string output, localsScope;
    char* input = loadFile("../test.h");

    ::OptimizeScope(input, output, 41, localsScope);

    printf("Locals: %s\n", localsScope.c_str());
    printf("All   : %s\n", output.c_str());
    return 0;
}

//-------------------------------------------------------
// Help function
//-------------------------------------------------------
char* loadFile(const char* fileName)
{
    FILE* fp;
    long len;
    char* buf = NULL;

    fp = fopen(fileName, "rb");
    if(!fp) {
        printf("failed to open file 'test.h': %s\n", strerror(errno));
        return NULL;
    }

    // read the whole file
    fseek(fp, 0, SEEK_END); // go to end
    len = ftell(fp); // get position at end (length)
    fseek(fp, 0, SEEK_SET); // go to begining
    buf = (char*)malloc(len + 1); // malloc buffer

    // read into buffer
    long bytes = fread(buf, sizeof(char), len, fp);
    printf("read: %ld\n", bytes);
    if(bytes != len) {
        fclose(fp);
        printf("failed to read from file 'test.h': %s\n", strerror(errno));
		free(buf);
        return NULL;
    }

    buf[len] = 0; // make it null terminated string
    fclose(fp);
    return buf;
}