[英]C to java file reading and printing
I have a C code which is the following output: 我有一个C代码,其输出如下:
INPUT: ./includeCrawler -Itest s_01.c
输入:
./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.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. 我试图将其转换为java,从而它读取例如s_01.c的文件,并将输出上述输出。 The file itself will read all the ih and output it.
文件本身将读取所有ih并将其输出。 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.
如果存在依赖性,例如程序读取s_01.c,它将读取s_01.c中的文件,并且将读取其中的#include“ i_60.h”,然后将其转到i_60.h,并找到更多与i相关的东西。它会继续这样做,直到找不到我的路。
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. 这是我到目前为止所做的事情:我已经成功读取了例如1个文件,并打印了与i相关的所有内容,然后将其扔到了链表中。但是,我不知道如何继续互相调用文件并然后打印输出。
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. 好的,我假设您已经实现了一个名为readFile(String filename)的函数,它将返回其依赖的文件名数组(或您所说的链表)。
What you can do is using a recursive procedure. 您可以使用递归过程。 BTW, you may use Set to hold the filenames rather than a linkedList.
顺便说一句,您可以使用Set来保存文件名,而不是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);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.