简体   繁体   中英

C to java file reading and printing

I have a C code which is the following output:

INPUT: ./includeCrawler -Itest s_01.c

OUTPUT: s_01.o: s_01.c i_60.h i_44.h i_46.h i_04.h i_51.h i_15.h i_33.h i_29.h i_16.h

S_01.c:
#include "i_60.h"
#include "i_44.h"
#include "i_46.h"
#include "i_04.h"
#include "i_51.h"
#include "i_15.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <sys/types.h> 
#include <signal.h>

i_60.c
#ifndef _I_60_H_
#define _I_60_H_

#include "i_33.h"
#include "i_04.h"
#include "i_29.h"

#include <stdio.h>

#endif /* _I_60_H_ */

Im trying to convert this into java whereby it reads in a file for eg s_01.c and it will print the above output. The file itself will read all the ih and output it. If there's dependencies such as if the program read s_01.c it will read the files inside s_01.c and it will read #include "i_60.h" inside it and then it will go to i_60.h and find more i's related stuff.. it will continue to do so until it cannot find anymore i's pathway.

Here is what i've done so far: I have successfully read for eg 1 file and print out all things related to i and then i threw it into a linkedlist.. However i do not know how to proceed to call each other files and then printout.

read: s_02.c 
Output:
[i_02.h]
[i_02.h, i_52.h]
[i_02.h, i_52.h]
[i_02.h, i_52.h]
[i_02.h, i_52.h, i_51.h]
[i_02.h, i_52.h, i_51.h]
[i_02.h, i_52.h, i_51.h]
[i_02.h, i_52.h, i_51.h]
[i_02.h, i_52.h, i_51.h, i_03.h]
[i_02.h, i_52.h, i_51.h, i_03.h, i_41.h]

Ok, I assume that you have implemented a function named readFile(String filename) and it will return an array(or maybe a linkedList, as you said) of filenames that it depends on.

What you can do is using a recursive procedure. BTW, you may use Set to hold the filenames rather than a linkedList.

// a set of filenames that have been read
HashSet<String> setOfProceededFilenames = new HashSet<String> ();

// recursively read the files
private void recursivelyReadFiles(String filename) {
    // we have defined String [] readFile(String filename)
    String [] dependencies = readFile(filename);

    // we only read those haven't been proceeded
    for (String dependentFile : dependencies) {
        if (!setOfProceededFilenames.contains(dependentFile)){
            // add this file to the set and read its dependencies
            setOfProceededFilenames.add(dependentFile);
            recursivelyReadFiles(dependentFile);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM