Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-03 08:22:06

0001 // $Id: TSQLImportClient.cxx,v 1.1.1.1 2004/02/18 20:58:02 dave Exp $
0002 //*-- Author : Valeriy Onuchin 21/03/2001
0003 
0004 /////////////////////////////////////////////////////////////////////
0005 //
0006 // TSQLImportClient
0007 //
0008 /////////////////////////////////////////////////////////////////////
0009 
0010 #include <RDBC/TSQLUrl.h>
0011 #include <RDBC/TSQL.h>
0012 #include <RDBC/TSQLImportClient.h>
0013 #include <TString.h>
0014 #include <TSystem.h>
0015 #include <stdlib.h>
0016 #include <stdio.h>
0017 #include <TUrl.h>
0018 #include <TSocket.h>
0019 
0020 /////////////////////////////////////////////////////////////////////
0021 //___________________________________________________________________
0022 TSQLImportClient::TSQLImportClient(const TString& url)
0023 {
0024    // ctor.
0025 
0026    fUrl = new TSQLUrl(url);
0027    fException = 0;
0028    fStatus = 0;
0029    TString host(fUrl->GetHost().Data());
0030    TString str(fUrl->GetFile().Data());
0031    fLocalFile = str;
0032 
0033    if(host=="localhost")  {
0034       fMustDeleteLocalFile = kFALSE;
0035    } else {
0036       fMustDeleteLocalFile = kTRUE;
0037       fLocalFile = Form("/tmp/%s%d",gSystem->BaseName(fUrl->GetFile().Data()),gSystem->GetPid());
0038       GET(url);   // download 
0039    }
0040 
0041    fSkipLines = 1; // default , first line is a header describes the columns
0042 }
0043 
0044 //___________________________________________________________________
0045 TSQLImportClient::~TSQLImportClient()
0046 {
0047    // dtor.
0048 
0049    Clean();
0050 }
0051 
0052 //___________________________________________________________________
0053 void TSQLImportClient::Clean()
0054 {
0055    //
0056 
0057    if(fMustDeleteLocalFile) {
0058       gSystem->Unlink(fLocalFile.Data());
0059    }
0060    if(fException) delete fException;
0061    if(fUrl) delete fUrl; 
0062 }
0063 
0064 //___________________________________________________________________
0065 TString Validate(const TString& str)
0066 {
0067    // internal use static func.
0068    // 
0069    // - Does validation of string as coulmn names/types, very primitive so far.
0070    // - Returns corrected column string 
0071 
0072    TString ret = str.Strip(TString::kBoth);
0073    Int_t spidx = 0;
0074    const char* s = ret.Data();
0075    
0076    if(s[0]=='\"' || s[strlen(s)-1]=='\"' ) {
0077       TString quote('\"');
0078       ret.ReplaceAll(quote,"");
0079       goto exit;
0080    }
0081    if( ret.IsNull() ||
0082       ((ret.Length()==1) && !isalpha(s[0]) ) ) return "wrong format";
0083 
0084    for (Ssiz_t i = 0; i < ret.Length(); i++) {
0085       if( !isalnum(s[i]) && !isspace(s[i]) && 
0086          s[i]!=')' && s[i]!='(' && s[i]!=',' &&
0087          s[i]!='_' && s[i]!='-' ) {
0088          return "wrong format";
0089       }
0090       if(isspace(s[i])) spidx = i;
0091    }
0092    
0093 exit:
0094    if(!spidx) ret += " TEXT NOT NULL";  
0095    return ret;   
0096 }
0097 
0098 //___________________________________________________________________
0099 void TSQLImportClient::GET(const TString& url)
0100 {
0101    // Download url into local temporary file
0102  
0103    TString str;
0104    const Int_t buflen=8192;
0105    static char buf[buflen];
0106 
0107    TString filename = url;
0108    filename.ReplaceAll(" ","");
0109 
0110    TUrl u(filename);
0111  
0112    TSocket s(u.GetHost(), u.GetPort());
0113 
0114    if (!s.IsValid()) {
0115       fStatus = HTTP_FORBIDDEN;
0116       return;
0117    }
0118 
0119    TString msg = Form("GET %s HTTP/1.0\015\012\015\012", u.GetFile());
0120    s.SendRaw(msg.Data(), msg.Length());
0121 
0122    while(s.RecvRaw(buf, buflen)>0) {         
0123       str += buf; 
0124       memset(buf,0,buflen);
0125    }
0126    s.Close();
0127 
0128    // cutoff HTTP header
0129    Int_t idx;
0130    idx = str.Index("\015\012\015\012");
0131    if(idx!=kNPOS) str = str(idx+4,str.Length()-idx-4);
0132 
0133    FILE* fd = fopen(fLocalFile.Data(),"w");
0134    if(!fd) fStatus = HTTP_FORBIDDEN;
0135 
0136    idx = fwrite(str.Data(),str.Length(),1,fd);
0137    if(idx!=1) {
0138       fStatus = HTTP_FORBIDDEN;
0139    }
0140    fclose(fd);
0141    return;
0142 }
0143 
0144 //___________________________________________________________________
0145 Int_t TSQLImportClient::Init()
0146 {
0147    // - read first line from local file 
0148    // - determine column names and types
0149 
0150    //usused: Bool_t isValid = kTRUE;
0151    FILE* fd;
0152    TString str;
0153 
0154    fTableName = TString(gSystem->BaseName(fLocalFile.Data()));
0155    TString ext = strrchr(fTableName.Data(),'.');
0156 
0157    if(!ext.IsNull()) {
0158       Int_t pidx = fTableName.Index(ext.Data());
0159       if(pidx>1) {
0160          fTableName =  fTableName(0,pidx);
0161       }
0162 
0163       fTableName.ReplaceAll(".","_");
0164    }
0165 
0166    if(gSystem->AccessPathName(fLocalFile.Data())) {
0167       fStatus = HTTP_NOT_FOUND;
0168       str = "File ";
0169       str += fLocalFile + " not found";
0170       fException = new TSQLException(str,"",fStatus);
0171       return fStatus;
0172    }
0173 
0174    fd = fopen(fLocalFile.Data(),"r");
0175 
0176    if( !fd ) {
0177       fclose(fd);
0178       fStatus = HTTP_FORBIDDEN;
0179       str = "You don't have read permission to ";
0180       str += fLocalFile;
0181       fException = new TSQLException(str,"",fStatus);
0182       return fStatus;
0183    }
0184    
0185    const Int_t buflen=8192;
0186    char buf[buflen];
0187 
0188    fgets(buf,buflen,fd);  // read first line         
0189    str = buf;
0190 
0191    if(str.IsNull()) {
0192       fclose(fd); // empty file
0193       fStatus = HTTP_NOT_ACCEPTABLE;
0194       str = "File ";
0195       str += fLocalFile + " is empty";
0196       fException = new TSQLException(str,"",fStatus);
0197       return fStatus;
0198    }
0199    str.Chop(); // cut off \n
0200 
0201    TString tmp;
0202    Int_t i,k;
0203    Int_t ncols = 0;
0204    Bool_t wrongFormat = kFALSE;
0205 
0206    for( i=k=0; (i=str.Index(",",i))>0; k=i++ ) {
0207       ncols++;
0208       tmp = Validate(str(!k?0:k+1,!k?i:i-k-1));
0209       wrongFormat = wrongFormat || tmp.IsNull() || (tmp=="wrong format"); 
0210       if(!wrongFormat) fColumns +=  tmp + ",";
0211    } 
0212 
0213    ncols++;
0214    tmp = Validate(str(k+(ncols>1),str.Length())); // the rest of string
0215 
0216    wrongFormat = wrongFormat || (tmp=="wrong format"); 
0217    if(!wrongFormat)  fColumns += tmp;
0218    else {
0219       fColumns = "";
0220       for(i=1; i<ncols; i++) fColumns += Form("C%d TEXT NOT NULL,",i);
0221       fColumns += Form("C%d TEXT NOT NULL",ncols);
0222       fSkipLines = 0;
0223    }
0224 
0225    fclose(fd);
0226    return fStatus = HTTP_OK;
0227 }