shawind 发表于 2007-11-21 21:29:10

[分享]调用其他EXE

简单的增强一下我在用的文本编辑器的命令行功能。

//run.d

import std.process;
import std.path;
import std.windows.charset;
import std.string;

alias printf p;

void main(char[][] args)
{
    if (args.length == 2)
    {
      char[] filename;
      filename ~= addExt(getName(fromMBSz(args.ptr)),"exe");
      int i = system(toString(toMBSz(filename)));
      p("%s\n",toMBSz("\n运行的是 => " ~ filename));
    }
    else
    {
      p("%s\n",toMBSz("错误: 你没有输入参数,或者输入超过一个以上的参数!"));
      p("%s\n",toMBSz("例: 当输入 'run c:\\test\\test.d' 时调用 'c:\\test\\test.exe'"));
    }
}

rednaxela 发表于 2007-11-22 18:31:21

Java:
public class Program {
    public static void main( String[] args ) throws IOException {
      if ( 1 == args.length ) {
            String command = args;
            System.out.println("Running: " + command );
            Process p = Runtime.getRuntime( ).exec( command );
            
            InputStream is = process.getInputStream();
            InputStreamReader isr = new InputStreamReader( is );
            BufferedReader br = new BufferedReader( isr );
            String line;
   
            System.out.printf( "Output of running %s is:",
                Arrays.toString( args ) );
   
            while ( ( line = br.readLine( ) ) != null ) {
            System.out.println(line);
            }
      } else {
            System.err.println( "Error: missing argument or more than 1 argument is given" );
            System.err.println( "Usage: java Program command" );
      }
    }
}

C#:
public sealed class Program {
    public static void Main(string[] args) {
      if ( 1 == args.length ) {
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo.FileName = args;
            proc.StartInfo.Arguments = string.Empty;
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.RedirectStandardOutput = false;
            proc.Start();
            proc.WaitForExit();
      } else {
            System.Console.WriteLine( "Error: missing argument or more than 1 argument is given" );
            System.Console.WriteLine( "Usage: Program command" );
      }
    }
}

shawind 发表于 2007-11-22 20:41:31

D中为了适应中文目录或文件名,我转了两次码。
而java,c#的代码看起来是一步到位了。极度心理不平衡中。

Miliardo 发表于 2007-11-22 20:48:40

> <Shawind桑要是真对D有爱还是去扶持一个开源编译器吧。
我觉得DMD怕是争气不了了。

rednaxela 发表于 2007-11-22 21:55:24

引用第2楼shawind于2007-11-22 20:41发表的:
D中为了适应中文目录或文件名,我转了两次码。
而java,c#的代码看起来是一步到位了。极度心理不平衡中。
么办法,D继承了C的一些糟糕特性。例如说虽然源文件支持UTF-8,内部也主要是用UTF-8来处理字符,但在关键的对外接口上没怎么处理。不setLocale的话随便你怎么用中文都是乱码 =_=||

shawind 发表于 2007-11-22 22:45:11

引用第3楼Miliardo于2007-11-22 20:48发表的:
> <Shawind桑要是真对D有爱还是去扶持一个开源编译器吧。
我觉得DMD怕是争气不了了。
也不是真的有爱。本来我就是业余写了玩玩的,只要觉得顺眼,像SmallTalk,Object-c这类的冷门语言,也在守备范围内。D正好是看比较顺眼,仅此而已。
至于D的前途什么的,不是我应该去考虑的事。那是Walter Bright的事。
开源的gdc更不好用,特别是在写WIN GUI程序的时候,除非自己去写一个开源编译器。
但是,如果我有这么nb的本事,早就去写一个类似python或ruby的可编译的新语言了。
谁还用D...
未来的话,如果MS能稍微改善下.net的启动速度,我就和D拜拜,转去用IronRuby,或者IronPython了。
学D也不是没有什么收获,至少,我在D上学到了很多C/C++知识。难到这就是有心栽花花不开?

引用第4楼rednaxela于2007-11-22 21:55发表的:

么办法,D继承了C的一些糟糕特性。例如说虽然源文件支持UTF-8,内部也主要是用UTF-8来处理字符,但在关键的对外接口上没怎么处理。不setLocale的话随便你怎么用中文都是乱码 =_=||
用tango的话,就不会有这个问题了。可我看到tango库就怕,觉得与其学着去用tango库,还不如去学C#了。

rednaxela 发表于 2007-11-23 10:36:50

引用第5楼shawind于2007-11-22 22:45发表的:
但是,如果我有这么nb的本事,早就去写一个类似python或ruby的可编译的新语言了。
谁还用D...
It's not even worth trying...
有好些尝试把Python直接编译为本地代码的项目了,然而数据显示它们甚至比解释执行的Python还慢。This may sound counterintuitive, but it's the fact.

shawind 发表于 2007-11-23 10:59:18

上次看pypy项目的时候,它还号称编译后可以比解释快70倍左右。
就是这个东西太难用,至今没成功试用过一次。

这个,我所谓的“新语言”,也就是没有;{}符号,强格式书写,符合C99标准,和C代码二进制兼容的另一个c语言...当然这种小事对高手来说,最多也就是一天的时间。

rednaxela 发表于 2007-11-23 13:50:37

你看的资料的来源是?有兴趣看看。
PyPy本身是用RPython写的。RPython是Python的一个子集,可以说已经相当退化了——它的动态性被极度限制,因而可以生成高效的编译后本地代码。但将完整的Python编译为本地代码的尝试,在效率意义上说几乎都失败了;因为Python有内在的动态性,编译时信息极度匮乏,所以静态编译难以生成好的本地代码。PyPy项目中所实现的完整Python也仍然是解释的:一个字节码编译器将Python编译为字节码,一个字节码解释器解释执行字节码,和一个标准对象空间。

不过说到PyPy,它有一个LLVM的后端。这玩意相当有爱。

shawind 发表于 2007-11-23 17:26:02

http://programming.reddit.com/info/152lr/comments/c156v0

Just for those who want to check pypy out a bit:

First download pypy, then run the translatorshell in the bin/ directory.

$ python translatorshell.py
*snip*
>>> def ack(x,y):
...   if x == 0: return y + 1
...   elif y == 0: return ack(x - 1, 1)
...   else: return ack(x - 1, ack(x, y - 1))
...
>>> t = Translation(ack)
>>> t.annotate()
*snip*
>>> t.rtype()
*snip*
>>> ack_c = t.compile_c()
*snip*
>>> import time
>>> start = time.time(); ack(3,6); print time.time() - start
509
0.19878911972
>>> start = time.time(); ack_c(3,6); print time.time() - start
509
0.00290894508362
The C version is nearly 70 times faster than the pure python version.

I do know that this is not the usual real life bottleneck that you run across, but it does somehow show PyPy's potential.


页: [1] 2
查看完整版本: [分享]调用其他EXE