[BACK]Return to taka_util.rr CVS log [TXT][DIR] Up to [local] / OpenXM / src / asir-contrib / packages / src

File: [local] / OpenXM / src / asir-contrib / packages / src / taka_util.rr (download)

Revision 1.26, Tue Aug 10 04:51:19 2021 UTC (2 years, 9 months ago) by takayama
Branch: MAIN
CVS Tags: HEAD
Changes since 1.25: +2 -1 lines

util_v and util_index ignore the letter _ at the beginning.  util_index(_v_12) returns [_v,[1,2]]

/* $OpenXM: OpenXM/src/asir-contrib/packages/src/taka_util.rr,v 1.26 2021/08/10 04:51:19 takayama Exp $ */

/*&usage begin: util_filter(Comm,InS)
 example: util_filter("java Http unkei:8091","Sqrt[2]");
 end:
*/
def taka_util_filter(Comm,InS,Opt) {
  Commfile = ".openxm-tmp.util.filter.commfile."+rtostr(get_pid());
  Infile   = ".openxm-tmp.util.filter.infile."+rtostr(get_pid());
  Outfile  = ".openxm-tmp.util.filter.outfile."+rtostr(get_pid());
  /* BUG: Opt is ignored for now.
      example:
     util_filter(Comm,InS|env=[["CLASSPATH","/usr/local/java/lib"]])
  */
  C = "#!/bin/sh\n"+
      Comm+" <"+Infile+" "+
      " > "+Outfile+"\n";
  /* print(C); */

  Fp = open_file(Commfile,"w");
  if (Fp < 0) return 0;
  In = strtoascii(C);
  N = length(In);
  for (I=0; I<N; I++) {
    put_byte(Fp,In[I]);
  }
  close_file(Fp);
  shell("chmod +x "+Commfile);

  Fp = open_file(Infile,"w");
  if (Fp < 0) return 0;
  In = strtoascii(InS);
  N = length(In);
  for (I=0; I<N; I++) {
    put_byte(Fp,In[I]);
  }
  close_file(Fp);

  shell("./"+Commfile);

  A=taka_util_load_file_as_a_string(Outfile);
  shell("rm -f "+Commfile+" "+Infile+" "+Outfile);
  return A;
}

def taka_util_load_file_as_a_string(Fname) {
  Fp = open_file(Fname,"r");
  if (Fp < 0) return 0;
  Str = [];
  while ((C=get_byte(Fp)) > 0) {
    Str=cons(C,Str);
  }
  close_file(Fp);
  Str = reverse(Str);
  return asciitostr(Str);
}
def taka_util_write_string_to_a_file(Fname,Str) {
  Fp = open_file(Fname,"w");
  if (Fp < 0) return 0;
  put_byte(Fp,Str);
  return close_file(Fp);
}

/* From fb/src/a2tfb/tfb2asir.rr */
def taka_util_find_substr(W,S) {
  if (type(W)==7) W=strtoascii(W);
  if (type(S)==7) S=strtoascii(S);
  N1 = length(W); N2 = length(S);
  for (J=0; J<N2-N1+1; J++) {
    for  (I=0; I<N1; I++) {
      if (W[I] != S[J+I]) break;
      if (I == N1-1) return J;
    }
  }
  return -1;
}

def taka_util_part(S,P,Q) {
  N=length(S)-1; if(N<Q) Q=N;
  if (Q < P) return [];
  R = newvect(Q-P+1);
  for (I=0; I<=Q-P; I++) {
    R[I] = S[I+P];
  }
  return vtol(R);
}

def taka_util_split(L,N) {
  Len=length(L);
  return [taka_util_part(L,0,N-1),taka_util_part(L,N,Len-1)];
}

def taka_util_remove_cr(S) {
  R = newvect(length(S));
  for (I=0; I<length(S); I++) {
    if (S[I] < 0x20) {
       R[I] = 0x20;
    }else{
       R[I] = S[I];
    }
  }
  return vtol(R);
}

def taka_util_v(V,L) {
  return taka_indexed_variable(V,L);  /* taka_plucker.rr */
}
def taka_util_index(V) {
  /* return taka_plucker_index(V); */
  S = rtostr(V);
  S = strtoascii(S);
  Id = [];
  I = 0;
  Name = [];  NF=0;
  while (S[I] == 95) { Name=cons(S[I],Name); I++;}
  while (I < length(S)) {
    if ((!NF) && (S[I] != 95)) Name=cons(S[I],Name);
    if (S[I] == 95) {  /* 95 == '_' */
      NF = 1;
      J = 0; I++;
      while ( I<length(S) && S[I] != 95) {
        if ((S[I] >= 0x30) && (S[I] <= 0x39)) {
          J = J*10+eval_str(asciitostr([S[I]]));
        }
        I++;
      }
      Id=cons(J,Id);
      I--;
    }
    I++;
  }
  Name = reverse(Name);
  Name = asciitostr(Name);
  return [Name,newvect(length(Id),reverse(Id))];
}

/*
  example: taka_util_find_and_replace([1,2],[5,1,2,5,1,2,1,2],[22,23,24]); 
*/
def taka_util_find_and_replace(Old,S,Snew) {
  ReturnStr=0;
  if (type(S)==7) {S=strtoascii(S); ReturnStr=1;}
  if (type(Old)==7) {Old=strtoascii(Old); ReturnStr=1;}
  if (type(Snew)==7) {Snew=strtoascii(Snew); ReturnStr=1;}
  N = length(S);
  Nold = length(Old); 
  Nnew = length(Snew);
  NewSize = N*2+1;
  New = newvect(NewSize);
  K = 0;
  for (I=0; I<N; I++) {
    Found = 0;
    for (J=I; (J<Nold+I) && (J<N); J++) {
      if (S[J] != Old[J-I]) {
         break;
      }
      if (J-I == Nold-1) {
        Found = 1;
      }
    }
    if (Found) {
      /* print("Found at I="+rtostr(I)); */
      I += Nold-1;
      for (P=0; P<Nnew; P++) {
        New[K] = Snew[P];
        K++;
        if (K > NewSize-1) {
           New2 = newvect(NewSize*2);
           for (Q=0; Q<NewSize; Q++) {
             New2[Q] = New[Q];
           }
           New = New2; NewSize *= 2;
        }
      }
    }else{
        New[K] = S[I]; 
        K++;
        if (K > NewSize-1) {
           New2 = newvect(NewSize*2);
           for (Q=0; Q<NewSize; Q++) {
             New2[Q] = New[Q];
           }
           New = New2; NewSize *= 2;
        }
    }    
  }
  Ans = newvect(K);
  for (I=0; I<K; I++) {
    Ans[I] = New[I];
  }
  if (ReturnStr) {
    return asciitostr(vtol(Ans));
  }else{
    return vtol(Ans);
  }
}

def taka_util_timing(Q) {
  if (type(Q) != 17) {
    return "Error: the argument should be in the form quote([cmd1, cmd2, ...]) ";
  }
  T0=time();
  R = eval_quote(Q);
  T1=time();
  R1 = ["cpu",T1[0]-T0[0],"gc",T1[1]-T0[1],"memory",T1[2]-T0[2],
        "real",T1[3]-T0[3]];
  return [R,R1];
}

def tk_util_file_exists(Fname) { return access(Fname); }

def tk_util_docdir() {
  DocumentTop=0;
  if (sysinfo()[0] == "windows") {
    DocumentTop2=getenv("ASIR_ROOTDIR")+"\\doc\\asir-doc-top.html";
    DocumentTop1=getenv("APPDATA")+"\\OpenXM\\doc\\asir-doc-top.html";
    DocumentTop2 = DocumentTop2;
    DocumentTop1 = DocumentTop1;
  }else{
    DocumentTop2=getenv("OpenXM_HOME")+"/doc/asir-doc-top.html";
    OpenXM_tmp=getenv("OpenXM_tmp");
    if (OpenXM_tmp == 0) OpenXM_tmp=getenv("HOME")+"/.OpenXM_tmp";
    DocumentTop1=OpenXM_tmp+"/OpenXM/doc/asir-doc-top.html";
  }
  if (tk_util_file_exists(DocumentTop1)) {
    DocumentTop = DocumentTop1;
  }else if (tk_util_file_exists(DocumentTop2)) {
    DocumentTop = DocumentTop2;
  }
  return DocumentTop;
}

def tk_util_find_start() {
  if (type(getopt(find_browser)) > 0) Browser=1;
  else Browser=0;
  if (sysinfo()[0] == "windows")  { Start = "start \"asirgui\""; return Start; }
  if (sysinfo()[0] == "macosx")  { Start = "open"; return Start; }
  Names = ["/usr/local/bin/gnome-open","/usr/bin/gnome-open"];
  if (Browser) {
    Names = append(Names,
		   ["/usr/local/bin/chrome","/usr/bin/chrome",
		    "/usr/local/bin/firefox","/usr/bin/firefox",
		    "/usr/local/bin/iceweasel","/usr/bin/iceweasel",
		    "/usr/local/bin/epiphany","/usr/bin/epiphany"]);
  }
  for (I=0; I<length(Names); I++) {
    if (tk_util_file_exists(Names[I])) return Names[I];
  }
  return 0;
}
def  tk_util_helph() {
  if (type(getopt(browser)) > 0) {
    Start=rtostr(getopt(browser));
  }else{
    Start = tk_util_find_start(|find_browser=1);
  }
  if (Start == 0) { error("Command to start a browser is not found. Specify the name of the browser by helph(|browser=Your_browser_name);"); return(-1); }
  DocumentTop = tk_util_docdir();
  if (DocumentTop == 0) { 
    error("Document directry is not found.");  return(-1);
  } 
  Com = Start+"  "+ "\""+DocumentTop+"\"";
  if (sysinfo()[0] != "windows") Com += " &";
  return shell(Com); 
}

/*
When {S} is a string by the ShiftJIS code and {S} contains dame-moji with respect to / or \,
it returns [a non-zero number, the string].
example:
  T = [0x5c,0xe4,0x5c,0x41,0x42]$
  T2=asciitostr(T)$
  util_damepathq(T2);
*/
def tk_util_damepathq(S) {
  if (type(getopt(checkspace))>0) Checkspace=1;
  else Checkspace=0;
  S=rtostr(S);
  S=strtoascii(S);
  Slash = 0x2f;
  BackSlash = 0x5c;
  N=length(S);
  Dame=0;
  for (I=0; I<N; I++) {
    if (((S[I] == Slash) || (S[I] == BackSlash)) && (I>0)) {
      C=S[I-1];
      if (((C>=0x81) && (C<=0x9f)) || ((C>=0xe0) && (C<=0xee)) || ((C>=0xfa) && (C<=0xfc))) {
         Dame=I; break;
      }
    }
    if (Checkspace && ( S[I] == 0x20)) { Dame=(I==0?1:I); break; }
  }
  if (Dame) T=asciitostr([S[Dame-1],S[Dame]]); else T=0;
  return [Dame, T];
}


Loaded_taka_util=1$
end$