home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 June / Chip_2002-06_cd1.bin / zkuste / wincom / download / mltwcx.1.0.0.8-src.cab / mltwcx-src / ArcItem.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-02-09  |  5.9 KB  |  316 lines

  1. #include "stdafx.h"
  2. #include "ArcItem.h"
  3.  
  4. #define iZspace(ch)((ch)==' ' || (ch)=='\t' || (ch)=='\r' || (ch)=='\n')
  5.  
  6. CArcItem::CArcItem()
  7. {
  8.     m_pNext=NULL;
  9.     m_szFilePath[0]=0;
  10.     m_iPackedSize=0;
  11.     m_iUnpackedSize=0;
  12.     m_iFileAttr=0;
  13.     m_iFileTime=0;
  14. }
  15.  
  16. CArcItem::~CArcItem()
  17. {
  18.     //delete m_pNext;
  19. }
  20.  
  21. #include <io.h>
  22.  
  23. int GetAttr(char *p)
  24. {
  25.     int iRet=0;
  26.  
  27.     strlwr(p);
  28.  
  29.     if(strchr(p,'a'))
  30.         iRet|=_A_ARCH;
  31.     if(strchr(p,'h'))
  32.         iRet|=_A_HIDDEN;
  33.     if(strchr(p,'s'))
  34.         iRet|=_A_SYSTEM;
  35.     if(strchr(p,'r'))
  36.         iRet|=_A_RDONLY;
  37.     if(strchr(p,'d'))
  38.         iRet|=_A_SUBDIR;
  39.  
  40.     return iRet;
  41. }
  42.  
  43. int GetYear(char *str)
  44. {
  45.     int iRet=atoi(str);
  46.     if(iRet<1900)
  47.         if(iRet<80)
  48.             iRet+=2000;
  49.         else
  50.             iRet+=1900;
  51.  
  52.     return iRet-1980;
  53. }
  54.  
  55. int GetHourModifier(char *str)
  56. {
  57.     strlwr(str);
  58.     if(strchr(str,'p'))
  59.         return 12;
  60.     return 0;
  61. }
  62.  
  63. int GetMonth(char *str)
  64. {
  65.     char aMonths[][4]={"Jan","Feb","Mar","Apr","May","Jun",
  66.                         "Jul","Aug","Sep","Oct","Nov","Dec"};
  67.     for(int i=0;i<12;i++)
  68.         if(!lstrcmpi(str,aMonths[i]))
  69.             return i+1;
  70.     return 0;
  71. }
  72.  
  73. int GetFormatFieldLength( const char *p)
  74. {
  75.     int iRet=0;
  76.     if(p)
  77.     {
  78.         char ch=*p;
  79.         switch(ch)
  80.         {
  81.         case 'n':case 'z':case 'p':
  82.         case 'd':case 't':case 'y':
  83.         case 'h':case 'H':case 'm':
  84.         case 's':case 'a':case 'T':
  85.         case 'e':
  86.         {
  87.             while(p && *p==ch){ p++; iRet++;}
  88.         }
  89.         default:
  90.             break;
  91.         }
  92.     }
  93.     return iRet;
  94. }
  95.  
  96. int CArcItem::ParseFormatLine( const char *pFmt,const char **ppBeg, CArcItem *pai,CArchiveDescription *pad)
  97. {
  98.     const char *pStr=*ppBeg;
  99.  
  100.     while(pFmt && *pFmt)
  101.     {
  102.         if(*pFmt=='*')
  103.         {
  104.             pFmt++;
  105.             while(pStr && *pStr && *pStr!=' ' && *pStr!='\n')pStr++;
  106.             if(pStr && *pStr=='\n')
  107.                 pStr++;
  108.             continue;
  109.         }
  110.  
  111.         /*if(*pFmt=='^')
  112.         {
  113.             pFmt++;
  114.             while(pStr && *pStr && *pStr!='\n' && *pStr!='\r')pStr++;
  115.             if(pStr && *pStr=='\n' && *pStr=='\r' )
  116.                 pStr++;
  117.             continue;
  118.         }*/
  119.  
  120.         int iFmtLen=GetFormatFieldLength(pFmt);
  121.  
  122.         if(iFmtLen)
  123.         {
  124.             int iStrLen=iFmtLen;
  125.             char *str=new char[iFmtLen+1];
  126.             strncpy(str,pStr,iFmtLen);
  127.             str[iFmtLen]=0;
  128.  
  129.             /*int iLen=0;
  130.             while(pStr[iLen] && pStr[iLen]!='\r')iLen++;
  131.             if(iLen<iStrLen)
  132.                 iStrLen=iLen;*/
  133.  
  134.             switch(*pFmt)
  135.             {
  136.             case 'z':
  137.                 pai->m_iUnpackedSize=atoi(str);
  138.                 break;
  139.             case 'p':
  140.                 pai->m_iPackedSize=atoi(str);
  141.                 break;
  142.             case 'y':
  143.                 pai->ftime.year=GetYear(str);
  144.                 break;
  145.             case 't':
  146.                 pai->ftime.month=atoi(str);
  147.                 break;
  148.             case 'T':
  149.                 pai->ftime.month=GetMonth(str);
  150.                 break;
  151.             case 'd':
  152.                 pai->ftime.day=atoi(str);
  153.                 break;
  154.             case 'h':
  155.                 pai->ftime.hour=atoi(str);
  156.                 break;
  157.             case 'H':
  158.                     pai->ftime.hour+=GetHourModifier(str);
  159.                     if(!(pai->ftime.hour % 12))
  160.                             pai->ftime.hour -= 12;
  161.                 break;
  162.             case 'm':
  163.                 pai->ftime.min=atoi(str);
  164.                 break;
  165.             case 's':
  166.                 pai->ftime.sec=atoi(str)/2;
  167.                 break;
  168.             case 'a':
  169.                 pai->m_iFileAttr=GetAttr(str);
  170.                 break;
  171.             case 'n':
  172.                 {
  173.                     LPCSTR lpZ=pad->String(TYPENAME_IDX);
  174.                     bool bAINSpecific=lpZ && !strcmp(lpZ,"AIN");
  175.  
  176.                     if(bAINSpecific)
  177.                     {
  178.                         int iLen=0;
  179.                         while(pStr[iLen] && pStr[iLen]!='\r')iLen++;
  180.                         
  181.                         if(iLen<=iFmtLen || pStr[iFmtLen]!=' ')
  182.                         {
  183.                             char *p=(char *)pStr;
  184.                             if(*p)
  185.                             {
  186.                                 int iTrimmedLen=iLen-(p-pStr);
  187.                                 if(iTrimmedLen<0)iTrimmedLen=0;
  188.                                 strncpy(pai->m_szFilePath,p,iTrimmedLen);
  189.                                 pai->m_szFilePath[iTrimmedLen]=0;
  190.                                 int i=iTrimmedLen-1;
  191.                                 while(iZspace(pai->m_szFilePath[i]))
  192.                                     pai->m_szFilePath[i--]=0;
  193.                             }
  194.                             else
  195.                                 pai->m_szFilePath[0]=0;
  196.                             iStrLen=iLen+2+iFmtLen;
  197.                             break;
  198.                         }
  199.                     }
  200.                     int iLen=0;
  201.                     while(str[iLen] && str[iLen]!='\r')iLen++;
  202.                     char *p=str;
  203.                     while(*p && iZspace(*p))p++;
  204.                     if(*p)
  205.                     {
  206.                         int iTrimmedLen=iLen-(p-str);
  207.                         if(iTrimmedLen<0)iTrimmedLen=0;
  208.                         strncpy(pai->m_szFilePath,p,iTrimmedLen);
  209.                         pai->m_szFilePath[iTrimmedLen]=0;
  210.                         int i=iTrimmedLen-1;
  211.                         while(iZspace(pai->m_szFilePath[i]))
  212.                             pai->m_szFilePath[i--]=0;
  213.                     }
  214.                     else
  215.                         pai->m_szFilePath[0]=0;
  216.                     iStrLen=iLen;
  217.                 }
  218.                 break;
  219.             case 'e':
  220.                 {
  221.                     char *p=str;
  222.                     while(*p && iZspace(*p))p++;
  223.                     int i=lstrlen(p)-1;
  224.                     while(iZspace(p[i]))p[i--]=0;
  225.                     if(lstrlen(p))
  226.                     {
  227.                         strcat(pai->m_szFilePath,".");
  228.                         strcat(pai->m_szFilePath,str);
  229.                     }
  230.                 }
  231.                 break;
  232.             }
  233.             delete[] str;
  234.             pFmt+=iFmtLen;
  235.             pStr+=iStrLen;
  236.         }
  237.         else
  238.         {
  239.             pFmt++;
  240.             pStr++;
  241.         }
  242.     }
  243.  
  244.     while(pStr && *pStr && *pStr!='\n')pStr++;
  245.     if(pStr && *pStr=='\n')pStr++;
  246.     *ppBeg=pStr;
  247.     return 0;
  248. }
  249.  
  250. CArcItem *CArcItem::LoadFromList(const char *pList,int iListLen,CArchiveDescription *pad)
  251. {
  252.     CArcItem *pListRoot=NULL;
  253.     CArcItem *pListEnd=NULL;
  254.     const char *pBeg=pList;
  255.     while(pBeg-pList<iListLen)
  256.     {
  257.         CArcItem *pai=new CArcItem();
  258.  
  259.         int iHF=pad->FormatHeight();
  260.  
  261.         if(!iHF)
  262.             break;
  263.  
  264.         for(int i=0;i<iHF;i++)
  265.             ParseFormatLine(pad->String(FORMAT_IDX+i),&pBeg, pai,pad);
  266.  
  267.         if(pad->SkipEmpty())
  268.             if(!strlen(pai->m_szFilePath))
  269.             {
  270.                 delete pai;
  271.                 continue;
  272.             }
  273.  
  274.         if(!pListRoot)
  275.         {
  276.             pListRoot=pai;
  277.             pListEnd=pListRoot;
  278.         }
  279.         else
  280.         {
  281.             pListEnd->m_pNext=pai;
  282.             pListEnd=pai;
  283.         }
  284.  
  285.         if(pad->ConverFromUnixPath())
  286.         {
  287.             char *p=pai->m_szFilePath;
  288.             for(;*p;p++)
  289.                 if(*p=='/')*p='\\';
  290.         }
  291.     }
  292.  
  293.   if(pListRoot && pad->SearchForUglyDirs())
  294.   {
  295.     for(CArcItem *p=pListRoot;p->m_pNext; p=p->m_pNext)
  296.     {
  297.       char *pName = p->m_szFilePath;
  298.       int nLen = strlen(pName);
  299.  
  300.       for(CArcItem *p2=pListRoot; p2->m_pNext; p2=p2->m_pNext)
  301.       {
  302.         char * pName2 = p2->m_szFilePath;
  303.         if((strlen(pName2) > nLen) && 
  304.               (pName2[nLen] == '\\') && 
  305.                 !strncmp(pName, pName2, nLen))
  306.         {
  307.           strcat(pName,"\\");
  308.           break;
  309.         }
  310.       }
  311.     }
  312.   }
  313.  
  314.     return pListRoot;
  315. }
  316.