OSDN Git Service

(none)
[gikonavigoeson/gikonavi.git] / RoundData.pas
index f8f35d0..90ebfd6 100644 (file)
@@ -12,14 +12,15 @@ type
 
        TRoundList = class(TObject)
        private
+        FOldFileRead: Boolean;
                FBoardList: TList;
                FItemList: TList;
                function GetCount(RoundType: TGikoRoundType): Integer;
                function GetRoundItem(Index: Integer; RoundType: TGikoRoundType): TRoundItem;
                function ParseRoundBoardLine(Line: string): TRoundItem;
-        function ParseRoundThreadLine(Line: string; var BoardList : TStringList): TRoundItem;
+        function ParseRoundThreadLine(Line: string): TRoundItem;
         function ParseOldRoundBoardLine(Line: string): TRoundItem;
-        function ParseOldRoundThreadLine(Line: string; var BoardList : TStringList): TRoundItem;
+        function ParseOldRoundThreadLine(Line: string): TRoundItem;
        public
                RoundNameList: TStringList;
 
@@ -29,18 +30,22 @@ type
                function Add(ThreadItem: TThreadItem): Integer; overload;
                procedure Delete(Board: TBoard); overload;
                procedure Delete(ThreadItem: TThreadItem); overload;
+        procedure Delete(URL: string; RoundType: TGikoRoundType); overload;
                procedure Clear;
                function Find(Board: TBoard): Integer; overload;
                function Find(ThreadItem: TThreadItem): Integer; overload;
+        function Find(URL: string; RoundType: TGikoRoundType): Integer; overload;
                property Count[RoundType: TGikoRoundType]: Integer read GetCount;
+        property OldFileRead: Boolean read FOldFileRead;
                property Items[Index: integer; RoundType: TGikoRoundType]: TRoundItem read GetRoundItem;
                procedure SetRoundName(Board: TBoard; RoundName: string); overload;
                procedure SetRoundName(ThreadItem: TThreadItem; RoundName: string); overload;
 
-        //procedure ConvertRoundFile; //bata44\88È\91O\82ÌRoundfile\82ð\95Ï\8a·\82·\82é
                procedure LoadRoundBoardFile;
         procedure LoadRoundThreadFile;
                procedure SaveRoundFile;
+
+        procedure URLReplace(oldURLs: TStringList; newURLs :TStringList);
        end;
 
        TRoundItem = class(TObject)
@@ -48,17 +53,19 @@ type
 //             FBBSType: TGikoBBSType;
                FRoundName: string;
                FRoundType: TGikoRoundType;
-    FItem                      : TObject;
+    //Item                     : TObject;
     FURL                       : string;
                FBoardTitle: string;
                FThreadTitle: string;
                FFileName: string;
                FBoolData: Boolean;             //\82¢\82ë\82¢\82ë\8eg\82¤\82å\82£
        public
-//             property BBSType: TGikoBBSType read FBBSType write FBBSType;
+
+       constructor Create;
+    //property BBSType: TGikoBBSType read FBBSType write FBBSType;
                property RoundName: string read FRoundName write FRoundName;
                property RoundType: TGikoRoundType read FRoundType write FRoundType;
-    property Item : TObject read FItem write FItem;
+    //property Item : TObject read FItem write FItem;
     property URL : string read FURL write FURL;
                property BoardTitle: string read FBoardTitle write FBoardTitle;
                property ThreadTitle: string read FThreadTitle write FThreadTitle;
@@ -74,7 +81,12 @@ const
        ROUND_BOARD_FILENAME: string = 'RoundBoard.2ch';        //\82 \82Æ\82ÅBoardGroup\82Ö\88Ú\93®
        ROUND_ITEM_FILENAME: string  = 'RoundItem.2ch';         //\93¯\8fã
        ROUND_INDEX_VERSION: string = '2.00';
-
+    ERROR_BOARD_FILENAME: string = 'ErrorBoard.2ch'; //Error\8ds\82ð\95Û\8aÇ\82·\82é
+    ERROR_ITEM_FILENAME: string = 'ErrorItem.2ch'; //Error\8ds\82ð\95Û\8aÇ\82·\82é
+constructor TRoundItem.Create;
+begin
+       inherited Create;
+end;
 constructor TRoundList.Create;
 begin
        inherited;
@@ -83,6 +95,7 @@ begin
        RoundNameList := TStringList.Create;
        RoundNameList.Sorted := True;
        RoundNameList.Duplicates := dupIgnore;
+    FOldFileRead := false;
 end;
 
 destructor TRoundList.Destroy;
@@ -91,7 +104,7 @@ begin
        Clear;
        FBoardList.Free;
        FItemList.Free;
-       inherited;
+       //inherited;
 end;
 
 function TRoundList.Add(Board: TBoard): Integer;
@@ -105,7 +118,7 @@ begin
                Item := TRoundItem.Create;
 //             Item.BBSType := gbt2ch; //\82Æ\82è\82 \82¦\82¸
                Item.RoundType := grtBoard;
-       Item.Item := Board;
+//     Item.Item := Board;
         Item.URL := Board.URL;
                Item.BoardTitle := Board.Title;
                Item.ThreadTitle := '';
@@ -126,7 +139,7 @@ begin
                Item := TRoundItem.Create;
 //             Item.BBSType := gbt2ch; //\82Æ\82è\82 \82¦\82¸
                Item.RoundType := grtItem;
-               Item.Item := ThreadItem;
+//             Item.Item := ThreadItem;
         Item.URL := Threaditem.URL;
                Item.BoardTitle := ThreadItem.ParentBoard.Title;
                Item.ThreadTitle := ThreadItem.Title;
@@ -139,12 +152,11 @@ end;
 procedure TRoundList.Delete(Board: TBoard);
 var
        idx: Integer;
-       Item: TRoundItem;
+//     Item: TRoundItem;
 begin
        idx := Find(Board);
        if idx <> -1 then begin
-               Item := TRoundItem(FBoardList[idx]);
-               Item.Free;
+               TRoundItem(FBoardList[idx]).Free;
                FBoardList.Delete(idx);
        end;
 end;
@@ -152,12 +164,11 @@ end;
 procedure TRoundList.Delete(ThreadItem: TThreadItem);
 var
        idx: Integer;
-       Item: TRoundItem;
+//     Item: TRoundItem;
 begin
        idx := Find(ThreadItem);
        if idx <> -1 then begin
-               Item := TRoundItem(FItemList[idx]);
-               Item.Free;
+               TRoundItem(FItemList[idx]).Free;
                FItemList.Delete(idx);
        end;
 end;
@@ -167,13 +178,17 @@ var
        i: Integer;
 begin
        for i := FBoardList.Count - 1 downto 0 do begin
-               TRoundItem(FBoardList[i]).Free;
+        if FBoardList[i] <> nil then
+                       TRoundItem(FBoardList[i]).Free;
                FBoardList.Delete(i);
        end;
+    FBoardList.Capacity := FBoardList.Count;
        for i := FItemList.Count - 1 downto 0 do begin
-               TRoundItem(FItemList[i]).Free;
+        if FItemList[i] <> nil then
+                       TRoundItem(FItemList[i]).Free;
                FItemList.Delete(i);
        end;
+    FItemList.Capacity := FItemList.Count;
 end;
 
 function TRoundList.Find(Board: TBoard): Integer;
@@ -207,6 +222,64 @@ begin
                end;
        end;
 end;
+function TRoundList.Find(URL: string; RoundType: TGikoRoundType): Integer;
+var
+       i: Integer;
+       Item: TRoundItem;
+begin
+       Result := -1;
+    if RoundType = grtItem then begin
+               for i := 0 to FItemList.Count - 1 do begin
+                       Item := TRoundItem(FItemList[i]);
+                       if Item.FRoundType <> RoundType then Continue;
+                       if Item.FURL = URL then begin
+                               Result := i;
+                               Exit;
+                       end;
+               end;
+    end else begin
+        for i := 0 to FBoardList.Count - 1 do begin
+                       Item := TRoundItem(FBoardList[i]);
+                       if Item.FRoundType <> RoundType then Continue;
+                       if Item.FURL = URL then begin
+                               Result := i;
+                               Exit;
+                       end;
+               end;
+    end;
+end;
+procedure TRoundList.Delete(URL: string; RoundType: TGikoRoundType);
+var
+       idx: Integer;
+       Item: TRoundItem;
+    board: TBoard;
+    threadItem: TThreadItem;
+begin
+       idx := Find(URL, RoundType);
+       if idx <> -1 then begin
+
+        if RoundType = grtBoard then begin
+                       Item := TRoundItem(FBoardList[idx]);
+                       Item.Free;
+                       FBoardList.Delete(idx);
+               board := BBSsFindBoardFromURL(URL);
+            if board <> nil then begin
+               board.Round := False;
+               board.RoundName := '';
+            end;
+        end else begin
+                       Item := TRoundItem(FItemList[idx]);
+                       Item.Free;
+                       FItemList.Delete(idx);
+
+            threadItem := BBSsFindThreadFromURL(URL);
+            if threadItem <> nil then begin
+                   threadItem.Round := false;
+               threadItem.RoundName := '';
+            end;
+        end;
+       end;
+end;
 
 procedure TRoundList.SetRoundName(Board: TBoard; RoundName: string);
 var
@@ -257,74 +330,138 @@ var
        i: Integer;
        sl: TStringList;
        FileName: string;
+    errorSl: TStringList;
+    errorFileName: string;
        Item: TRoundItem;
+    delCount: Integer;
 begin
        sl := TStringList.Create;
+    errorSl := TStringList.Create;
+       errorSl.Duplicates := dupIgnore;
        try
                //\83{\81[\83h\8f\84\89ñ\83t\83@\83C\83\8b\93Ç\82Ý\8d\9e\82Ý
                FileName := GikoSys.GetConfigDir + ROUND_BOARD_FILENAME;
+        //\83G\83\89\81[\8ds\95Û\91\83t\83@\83C\83\8b\93Ç\82Ý\8d\9e\82Ý
+        errorFileName := GikoSys.GetConfigDir + ERROR_BOARD_FILENAME;
                if FileExists(FileName) then begin
                        sl.LoadFromFile(FileName);
+            if FileExists(errorFileName) then begin
+               try
+                       errorSl.LoadFromFile(errorFileName);
+                except
+                end;
+            end;
+            //Item := TRoundItem.Create;
+            delCount := 0;
             //\82P\8ds\96Ú\82Í\83o\81[\83W\83\87\83\93
                        if sl[0] = ROUND_INDEX_VERSION then begin
                                for i := 1 to sl.Count - 1 do begin
-                                       Item := ParseRoundBoardLine(sl[i]);
-                                       FBoardList.Add(Item);
-                                       RoundNameList.Add(Item.RoundName);
+                                       Item := ParseRoundBoardLine(sl[i - delCount]);
+                    if Item <> nil then begin
+                                               FBoardList.Add(Item);
+                                               RoundNameList.Add(Item.RoundName);
+                    end else begin
+                       errorSl.Add( sl[i - delCount] );
+                        sl.Delete(i- delCount);
+                        Inc(delCount);
+                    end;
                                end;
             end else begin
-                               for i := 1 to sl.Count - 1 do begin
-                                       Item := ParseOldRoundBoardLine(sl[i]);
-                                       FBoardList.Add(Item);
-                                       RoundNameList.Add(Item.RoundName);
-                               end;
+                if FOldFileRead then begin  //\83M\83R\83i\83r\96{\91Ì\82ª\83{\81[\83h\83t\83@\83C\83\8b\82ð\82æ\82Ý\82Æ\82Á\82½\8cã\82\82á\82È\82¢\82Æ\83N\83\89\83b\83V\83\85\82·\82é\82Ì\82Å
+                                       for i := 1 to sl.Count - 1 do begin
+                                               Item := ParseOldRoundBoardLine(sl[i - delCount]);
+                        if Item <> nil then begin
+                                                       FBoardList.Add(Item);
+                                                       RoundNameList.Add(Item.RoundName);
+                        end else begin
+                               errorSl.Add( sl[i- delCount] );
+                               sl.Delete(i- delCount);
+                            Inc(delCount);
+                        end;
+                                       end;
+                end else
+                       FOldFileRead := true;
             end;
                end;
+        if errorSl.Count > 0 then
+               errorSl.SaveToFile(errorFileName);
        finally
+       errorSl.Free;
                sl.Free;
        end;
 end;
 procedure TRoundList.LoadRoundThreadFile;
 var
        i: Integer;
-    j: Integer;
+//    j: Integer;
        sl: TStringList;
        FileName: string;
+    errorSl: TStringList;
+    errorFileName: string;
        Item: TRoundItem;
-    boardList : TStringList;
+    delCount: Integer;
+//    boardList : TStringList;
 begin
-    boardList := TStringList.Create;
-    boardList.Duplicates := dupIgnore;
+//    boardList := TStringList.Create;
+//    boardList.Duplicates := dupIgnore;
+    errorSl := TStringList.Create;
+       errorSl.Duplicates := dupIgnore;
        sl := TStringList.Create;
        try
                //\83X\83\8c\8f\84\89ñ\83t\83@\83C\83\8b\93Ç\82Ý\8d\9e\82Ý
                FileName := GikoSys.GetConfigDir + ROUND_ITEM_FILENAME;
+        //\83G\83\89\81[\8ds\95Û\91\83t\83@\83C\83\8b\93Ç\82Ý\8d\9e\82Ý
+        errorFileName := GikoSys.GetConfigDir + ERROR_ITEM_FILENAME;
                if FileExists(FileName) then begin
                        sl.LoadFromFile(FileName);
+            if FileExists(errorFileName) then begin
+               try
+                       errorSl.LoadFromFile(errorFileName);
+                except
+                end;
+            end;
+            //Item := TRoundItem.Create;
+            delCount := 0;
                        //\82P\8ds\96Ú\82Í\83o\81[\83W\83\87\83\93
             if sl[0] = ROUND_INDEX_VERSION then begin
                                for i := 1 to sl.Count - 1 do begin
-                                       Item := ParseRoundThreadLine(sl[i], boardList);
-                                       FItemList.Add(Item);
-                                       RoundNameList.Add(Item.RoundName);
-                               end;
+                                       Item := ParseRoundThreadLine(sl[i - delCount]);
+                    if Item <> nil then begin
+                                               FItemList.Add(Item);
+                                               RoundNameList.Add(Item.RoundName);
+                                       end else begin
+                        errorSl.Add(sl[i - delCount]);
+                        sl.Delete(i - delCount);
+                        Inc(delCount);
+                    end;
+                end;
             end else begin
+                LoadRoundBoardFile;
                 for i := 1 to sl.Count - 1 do begin
-                                       Item := ParseOldRoundThreadLine(sl[i], boardList);
-                                       FItemList.Add(Item);
-                                       RoundNameList.Add(Item.RoundName);
+                                       Item := ParseOldRoundThreadLine(sl[i - delCount]);
+                    if Item <> nil then begin
+                                               FItemList.Add(Item);
+                                               RoundNameList.Add(Item.RoundName);
+                    end else begin
+                                               errorSl.Add(sl[i - delCount]);
+                        sl.Delete(i - delCount);
+                        Inc(delCount);
+                    end;
                                end;
             end;
+//             j := boardList.Count - 1;
+//         while j >= 0 do begin
+//                     GikoSys.ReadSubjectFile( BBSsFindBoardFromURL( boardList[j] ) );
+//                 boardList.Delete(j);
+//             Dec(j);
+//             end;
+            if errorSl.Count > 0 then
+               errorSl.SaveToFile(errorFileName);
                end;
-        j := boardList.Count - 1;
-        while j >= 0 do begin
-               GikoSys.ReadSubjectFile( BBSsFindBoardFromURL( boardList[j] ) );
-            boardList.Delete(j);
-               Dec(j);
-        end;
        finally
+        errorSl.Free;
                sl.Free;
-        boardList.Free;
+//        boardList.Free;
        end;
 end;
 procedure TRoundList.SaveRoundFile;
@@ -377,42 +514,50 @@ begin
     Result.RoundType := grtBoard;
     for i := 0 to 2 do begin
        s := GikoSys.GetTokenIndex(Line, #1, i);
-        case i of
+        try
+               case i of
                0:
-               begin
-                       Result.URL := s;
-               Result.Item := BBSsFindBoardFromURL( s );
-               end;
-            1: Result.BoardTitle := s;
-            2: Result.RoundName := s;
+               begin
+                               Result.URL := s;
+                       end;
+               1: Result.BoardTitle := s;
+               2: Result.RoundName := s;
+               end;
+        except
+               Result := nil;
+            Exit;
         end;
     end;
 end;
 
-function TRoundList.ParseRoundThreadLine(Line: string; var BoardList : TStringList): TRoundItem;
+function TRoundList.ParseRoundThreadLine(Line: string): TRoundItem;
 var
        s: string;
        i: Integer;
-    threadItem: TThreadItem;
+//    threadItem: TThreadItem;
 begin
     Result := TRoundItem.Create;
        Result.RoundType := grtItem;
     for i := 0 to 4 do begin
        s := GikoSys.GetTokenIndex(Line, #1, i);
-        case i of
-               0:
-               begin
-                       Result.URL := s;
-                threadItem := BBSsFindThreadFromURL( s );
-                if threadItem <> nil then begin
-                       Result.Item := threadItem;
-                       BoardList.Add( threadItem.ParentBoard.URL );
+        try
+            case i of
+                0:
+                begin
+                    Result.URL := s;
+                    //threadItem := BBSsFindThreadFromURL( s );
+                    //if threadItem <> nil then begin
+                    //    BoardList.Add( threadItem.ParentBoard.URL );
+                    //end;
                 end;
-               end;
-            1: Result.BoardTitle := s;
-            2: Result.FileName := s;
-            3: Result.ThreadTitle := s;
-                       4: Result.RoundName := s;
+                1: Result.BoardTitle := s;
+                2: Result.FileName := s;
+                3: Result.ThreadTitle := s;
+                4: Result.RoundName := s;
+            end;
+        except
+               Result := nil;
+            Exit;
         end;
     end;
 end;
@@ -421,61 +566,114 @@ function TRoundList.ParseOldRoundBoardLine(Line: string): TRoundItem;
     var
     i: Integer;
        s: string;
-       buf: string;
     board: TBoard;
-    threadItem: TThreadItem;
 begin
        Result := TRoundItem.Create;
     Result.ThreadTitle := '';
     Result.FileName := '';
     Result.RoundType := grtBoard;
-    for i := 0 to 1 do begin
+    for i := 0 to 2 do begin
        s := GikoSys.GetTokenIndex(Line, #1, i);
-        case i of
-               0:
-               begin
-               Result.BoardTitle := s;
-               board := BBSs[ 0 ].FindBBSID( s );
-                       Result.URL := board.URL;
-               Result.Item := BBSsFindBoardFromURL( Result.URL );
-               end;
-            1: Result.RoundName := s;
+        try
+               case i of
+                       0:
+                       begin
+                       board := BBSs[ 0 ].FindBBSID( s );
+                    if board <> nil then begin
+                                       Result.URL := board.URL;
+                    end else begin
+                       raise Exception.Create('\82±\82Ì\8f\84\89ñ\82Í\93Ç\82Ý\8d\9e\82ß\82È\82¢\82æ\81i\91½\95ª\8aO\95\94\94Â\81j');
+                    end;
+                       end;
+               1: Result.FBoardTitle := s;
+               2: Result.RoundName := s;
+               end;
+        except
+               Result := nil;
+            Exit;
         end;
     end;
 end;
 
-function TRoundList.ParseOldRoundThreadLine(Line: string; var BoardList : TStringList): TRoundItem;
+function TRoundList.ParseOldRoundThreadLine(Line: string): TRoundItem;
     var
     i: Integer;
        s: string;
        buf: string;
     board: TBoard;
-    threadItem: TThreadItem;
+//    threadItem: TThreadItem;
+    bbsID: string;
 begin
        Result := TRoundItem.Create;
     Result.RoundType := grtItem;
-    for i := 0 to 3 do begin
+    for i := 0 to 4 do begin
        s := GikoSys.GetTokenIndex(Line, #1, i);
-        case i of
-               0: Result.BoardTitle := s;
-            1:
-               begin
-               Result.FileName := s;
-                board := BBSs[ 0 ].FindBoardFromTitle(Result.BoardTitle);
-                if board <> nil then begin
-                    BoardList.Add(board.URL);
-                    buf := Copy(board.GetSendURL,1,LastDelimiter('/', board.GetSendURL)-1);
-                                       Result.URL := buf + '/read.cgi/'+ board.BBSID+ '/' +ChangeFileExt(s,'') + '/l50';
-                    threadItem := BBSsFindThreadFromURL(Result.URL);
-                    if threadItem <> nil then begin
-                       Result.Item := threadItem;
+        try
+               case i of
+               0: bbsID := s;
+                   1: Result.BoardTitle := s;
+               2:
+                       begin
+                       Result.FileName := s;
+                       board := BBSs[ 0 ].FindBBSID(bbsID);
+                   if board <> nil then begin
+                       buf := Copy(board.GetSendURL,1,LastDelimiter('/', board.GetSendURL)-1);
+                                               Result.URL := buf + '/read.cgi/'+ board.BBSID+ '/' +ChangeFileExt(s,'') + '/l50';
+                   end else begin
+                       raise Exception.Create('\82±\82Ì\8f\84\89ñ\82Í\93Ç\82Ý\8d\9e\82ß\82È\82¢\82æ');
                     end;
-                end;
+                   end;
+               3: Result.ThreadTitle := s;
+                   4: Result.RoundName := s;
+               end;
+        except
+               Result := nil;
+            break;
+        end;
+    end;
+end;
+procedure  TRoundList.URLReplace(oldURLs: TStringList; newURLs :TStringList);
+var
+       i: Integer;
+    j: Integer;
+    tempString: string;
+    tmpURL: string;
+    oldHost: string;
+    oldBoardName: string;
+    newHost: string;
+    newBoardName: string;
+begin
+    if oldURLs.Count <> newURLs.Count then
+       Exit;
+    //\82±\82±\82©\82ç\81ABoard\82ÌURL\82Ì\95Ï\8dX
+    for j :=0 to oldURLs.Count - 1 do begin
+               for i :=0 to FBoardList.Count - 1 do begin
+               if TRoundItem(FBoardList[i]).FURL = oldURLs[i] then
+               TRoundItem(FBoardList[i]).FURL := newURLs[i];
+       end;
+    end;
+    //\82±\82±\82Ü\82Å\81ABoard\82ÌURL\82Ì\95Ï\8dX
+
+    //\82±\82±\82©\82ç\81AThread\82ÌURL\82Ì\95Ï\8dX
+    //\96Ê\93|\82¾\82¯\82Çthread\82Í\82»\82ê\82¼\82êURL\82ð\83`\83\83\83b\83N\82µ\82È\82ª\82ç\82â\82Á\82Ä\82©\82È\82«\82á\82¢\82¯\82È\82¢\81B
+    for i := 0 to oldURLs.Count - 1 do begin
+        tmpURL                         := Copy(oldURLs[i], 1, Length(oldURLs[i]) -1);
+        oldHost                        := Copy(tmpURL, 1, LastDelimiter('/', tmpURL) );
+        oldBoardName    := Copy(tmpURL, LastDelimiter('/', tmpURL), Length(tmpURL) ) + '/';
+        tmpURL                         := Copy(newURLs[i], 1, Length(newURLs[i]) -1);
+        newHost                        := Copy(tmpURL, 1, LastDelimiter('/', tmpURL) );
+        newBoardName    := Copy(tmpURL, LastDelimiter('/', tmpURL), Length(tmpURL) ) + '/';
+
+        for j := 0 to FItemList.Count - 1 do begin
+            tempString := TRoundItem(FItemList[j]).FURL;
+            if ( AnsiPos(oldBoardName, tempString) <> 0 ) and ( AnsiPos(oldHost, tempString ) <> 0 ) then begin
+               tempString := StringReplace(tempString, oldHost, newHost,[]);
+                TRoundItem(FItemList[j]).FURL := tempString;
             end;
-            2: Result.ThreadTitle := s;
-            3: Result.RoundName := s;
         end;
     end;
+    //\82±\82±\82Ü\82Å\81AThread\82ÌURL\82Ì\95Ï\8dX
+
 end;
 
 end.