Rev 379 | Rev 383 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 379 | Rev 380 | ||
---|---|---|---|
Line 834... | Line 834... | ||
834 | /****************************************************************************************************************************************************/ |
834 | /****************************************************************************************************************************************************/ |
835 | /* Function: s16 fseek_(File_t *, s32 *, u8) */ |
835 | /* Function: s16 fseek_(File_t *, s32 *, u8) */ |
836 | /* */ |
836 | /* */ |
837 | /* Description: This function sets the pointer of the stream relative to the position */ |
837 | /* Description: This function sets the pointer of the stream relative to the position */ |
838 | /* specified by origin (SEEK_SET, SEEK_CUR, SEEK_END) */ |
838 | /* specified by origin (SEEK_SET, SEEK_CUR, SEEK_END) */ |
839 | /* Returnvalue: Is 0 if seek was successful */ |
839 | /* Returnvalue: Is 0 if seek was successful */ |
840 | /****************************************************************************************************************************************************/ |
840 | /****************************************************************************************************************************************************/ |
841 | s16 fseek_(File_t *file, s32 offset, s16 origin) |
841 | s16 fseek_(File_t *file, s32 offset, s16 origin) |
842 | { |
842 | { |
843 | s32 fposition = 0; |
843 | s32 fposition = 0; |
844 | s16 retvalue = 1; |
844 | s16 retvalue = 1; |
Line -... | Line 845... | ||
- | 845 | ||
- | 846 | // the byteindex within a sector |
|
- | 847 | u32 byte_index = 0; |
|
- | 848 | // the sectorindex within a cluster |
|
- | 849 | u32 sector_index = 0; |
|
- | 850 | // the index of the cluster within the clusterchain inside the fat |
|
- | 851 | u32 cluster_index = 0; |
|
- | 852 | ||
845 | 853 | // check if the partition is valid |
|
846 | if((!Partition.IsValid) || (file == NULL)) return(retvalue); |
854 | if((!Partition.IsValid) || (file == NULL)) return(retvalue); |
- | 855 | //...................................................... |
|
847 | switch(origin) |
856 | if(origin == SEEK_SET) // Fileposition relative to the beginning of the file. |
848 | { |
- | |
849 | case SEEK_SET: // Fileposition relative to the beginning of the file. |
857 | { |
850 | fposition = 0; |
858 | fposition = 0; |
- | 859 | } |
|
851 | break; |
860 | //...................................................... |
- | 861 | else if(origin == SEEK_END) // Fileposition relative to the end of the file. |
|
852 | case SEEK_END: // Fileposition relative to the end of the file. |
862 | { |
853 | fposition = (s32)file->Size; |
863 | fposition = (s32) file->Size; |
- | 864 | } |
|
854 | break; |
865 | //...................................................... |
855 | case SEEK_CUR: // Fileposition relative to the current position of the file. |
866 | else if(origin == SEEK_CUR) // Fileposition relative to the current position of the file. |
856 | default: |
867 | { |
857 | fposition = file->Position; |
- | |
858 | break; |
868 | fposition = (s32)file->Position; |
859 | } |
- | |
860 | - | ||
Line -... | Line 869... | ||
- | 869 | } |
|
- | 870 | ||
- | 871 | // calculate the specified fileposition according to the selected mode |
|
861 | fposition += offset; |
872 | fposition += offset; // Die Absolute Dateiposition welche durch fseek angesprungen werden soll. |
862 | 873 | // is the fileposition within the file? |
|
863 | if((fposition >= 0) && (fposition <= (s32)file->Size)) // is the pointer still within the file? |
874 | if((fposition >= 0) && (fposition <= (s32)file->Size)) // Ist die berechnete Dateiposition innerhalb der geƶffneten Datei? |
864 | { |
875 | { |
865 | // reset file position to start of the file |
876 | // initialize the filepointer |
866 | file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster; |
877 | file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster; |
867 | file->SectorOfCurrCluster = 0; |
878 | file->SectorOfCurrCluster = 0; |
- | 879 | file->ByteOfCurrSector = 0; |
|
868 | file->ByteOfCurrSector = 0; |
880 | file->Position = 0; |
869 | file->Position = 0; |
881 | // has the specified file at least one valid sector attached? |
870 | if(file->FirstSectorOfCurrCluster == SECTOR_UNDEFINED) return(retvalue); |
- | |
871 | while(file->Position < fposition) // repeat until the current position is less than target |
882 | if(file->FirstSectorOfCurrCluster == SECTOR_UNDEFINED) return(retvalue); |
872 | { |
883 | // calculate the absolute number of the sector wich contains the fileposition we are looking for |
873 | file->Position++; // increment file position |
884 | sector_index = (u32) ((u32)fposition >> 9); |
874 | file->ByteOfCurrSector++; // next byte in current sector |
- | |
875 | if(file->ByteOfCurrSector >= BYTES_PER_SECTOR) |
885 | // calculate the index of the cluster containing the specified sector |
876 | { |
886 | cluster_index = (u32) sector_index / Partition.SectorsPerCluster; |
877 | file->ByteOfCurrSector = 0; // reading at the beginning of new sector. |
887 | // the absolute sectornumber becomes relative to the beginning of the specified cluster |
878 | file->SectorOfCurrCluster++; // continue reading in next sector |
888 | sector_index = (sector_index % Partition.SectorsPerCluster); |
879 | if(file->SectorOfCurrCluster >= Partition.SectorsPerCluster) // if end of cluster is reached, the next datacluster has to be searched in the FAT. |
889 | // calculate the index of the byteposition the fileposition points to |
880 | { |
- | |
881 | if(GetNextCluster(file)) // Sets the clusterpointer of the file to the next datacluster. |
890 | byte_index = (u32) fposition % 512; |
882 | { |
- | |
883 | file->SectorOfCurrCluster = 0; |
891 | // parse the fat till the calculated cluster has been reached |
884 | } |
- | |
885 | else // the last cluster was allready reached |
892 | while(cluster_index--) GetNextCluster(file); |
886 | { |
893 | // set the filepointer to the specified sector and byteposition |
- | 894 | file->SectorOfCurrCluster = (u8) sector_index; |
|
887 | file->SectorOfCurrCluster--; // jump back to the last sector in the last cluster |
895 | file->ByteOfCurrSector = (u16) byte_index; |
- | 896 | // the fileposition now equals the filepointer |
|
888 | file->ByteOfCurrSector = BYTES_PER_SECTOR; // set ByteOfCurrSector one byte over sector end |
897 | file->Position = (u32)fposition; |
889 | } |
- | |
890 | } |
- | |
891 | } |
898 | // the specified fileposition has been reached |
892 | } |
- | |
893 | } |
899 | retvalue = 0; |
894 | if(file->Position == fposition) retvalue = 0; |
900 | } |
Line 895... | Line 901... | ||
895 | return(retvalue); |
901 | return(retvalue); |
Line 969... | Line 975... | ||
969 | /* */ |
975 | /* */ |
970 | /* Description: This function looks in the fat to find the next free cluster and appends it to the file. */ |
976 | /* Description: This function looks in the fat to find the next free cluster and appends it to the file. */ |
971 | /* */ |
977 | /* */ |
972 | /* Returnvalue: The function returns the appened cluster number or CLUSTER_UNDEFINED of no cluster was appended. */ |
978 | /* Returnvalue: The function returns the appened cluster number or CLUSTER_UNDEFINED of no cluster was appended. */ |
973 | /****************************************************************************************************************************************/ |
979 | /****************************************************************************************************************************************/ |
- | 980 | ||
974 | u16 AppendCluster(File_t *file) |
981 | u16 AppendCluster(File_t *file) |
975 | { |
982 | { |
976 | u16 last_cluster, new_cluster = CLUSTER_UNDEFINED; |
983 | u16 last_cluster, new_cluster = CLUSTER_UNDEFINED; |
977 | u32 fat_byte_offset, sector, byte; |
984 | u32 fat_byte_offset, sector, byte; |
978 | Fat16Entry_t * fat; |
985 | Fat16Entry_t * fat; |
- | 986 | // s8 text[64]; |
|
Line 979... | Line 987... | ||
979 | 987 | ||
Line 980... | Line 988... | ||
980 | if((!Partition.IsValid) || (file == NULL)) return(new_cluster); |
988 | if((!Partition.IsValid) || (file == NULL)) return(new_cluster); |
981 | 989 | ||
982 | new_cluster = FindNextFreeCluster(file); // the next free cluster found on the disk. |
990 | new_cluster = FindNextFreeCluster(file); // the next free cluster found on the disk. |
- | 991 | if(new_cluster != CLUSTER_UNDEFINED) |
|
- | 992 | { // A free cluster was found and can be added to the end of the file. |
|
- | 993 | // is there at least one cluster appended to the file? |
|
983 | if(new_cluster != CLUSTER_UNDEFINED) |
994 | if(file->FirstSectorOfLastCluster == CLUSTER_UNDEFINED) |
- | 995 | { |
|
- | 996 | fseek_(file, 0, SEEK_END); // jump to the end of the file |
|
984 | { // A free cluster was found and can be added to the end of the file. |
997 | // remember the first sector of the last cluster |
- | 998 | file->FirstSectorOfLastCluster = file->FirstSectorOfCurrCluster; |
|
- | 999 | last_cluster = SectorToFat16Cluster(file->FirstSectorOfCurrCluster); // determine current file cluster |
|
- | 1000 | } |
|
- | 1001 | else |
|
- | 1002 | { |
|
- | 1003 | last_cluster = SectorToFat16Cluster(file->FirstSectorOfLastCluster); // determine current file cluster |
|
985 | fseek_(file, 0, SEEK_END); // jump to the end of the file |
1004 | } |
986 | last_cluster = SectorToFat16Cluster(file->FirstSectorOfCurrCluster); // determine current file cluster |
1005 | |
987 | if(last_cluster != CLUSTER_UNDEFINED) |
1006 | if(last_cluster != CLUSTER_UNDEFINED) |
988 | { |
1007 | { |
989 | // update FAT entry of last cluster |
1008 | // update FAT entry of last cluster |
Line 1005... | Line 1024... | ||
1005 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) // save the modified sector to the FAT. |
1024 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) // save the modified sector to the FAT. |
1006 | { |
1025 | { |
1007 | Fat16_Deinit(); |
1026 | Fat16_Deinit(); |
1008 | return(0); |
1027 | return(0); |
1009 | } |
1028 | } |
- | 1029 | // now the new cluster appended to the fat is the last cluster |
|
- | 1030 | file->FirstSectorOfLastCluster = Fat16ClusterToSector(new_cluster); |
|
1010 | } |
1031 | } |
1011 | else // last cluster of the file is undefined |
1032 | else // last cluster of the file is undefined |
1012 | { // then the new cluster must be the first one of the file |
1033 | { // then the new cluster must be the first one of the file |
1013 | // and its cluster number must be set in the direntry |
1034 | // and its cluster number must be set in the direntry |
1014 | DirEntry_t * dir; |
1035 | DirEntry_t * dir; |
Line 1033... | Line 1054... | ||
1033 | Fat16_Deinit(); |
1054 | Fat16_Deinit(); |
1034 | return(CLUSTER_UNDEFINED); |
1055 | return(CLUSTER_UNDEFINED); |
1035 | } |
1056 | } |
1036 | // update file info |
1057 | // update file info |
1037 | file->FirstSectorOfFirstCluster = Fat16ClusterToSector(new_cluster); |
1058 | file->FirstSectorOfFirstCluster = Fat16ClusterToSector(new_cluster); |
- | 1059 | file->FirstSectorOfLastCluster = file->FirstSectorOfFirstCluster; |
|
1038 | file->Size = 0; |
1060 | file->Size = 0; |
1039 | file->Position = 0; |
1061 | file->Position = 0; |
1040 | } |
1062 | } |
1041 | // update file pointes |
1063 | // update file pointes |
1042 | file->FirstSectorOfCurrCluster = Fat16ClusterToSector(new_cluster); |
1064 | file->FirstSectorOfCurrCluster = Fat16ClusterToSector(new_cluster); |
Line 1044... | Line 1066... | ||
1044 | file->ByteOfCurrSector = 0; |
1066 | file->ByteOfCurrSector = 0; |
1045 | } |
1067 | } |
1046 | return(new_cluster); |
1068 | return(new_cluster); |
1047 | } |
1069 | } |
Line -... | Line 1070... | ||
- | 1070 | ||
1048 | 1071 | ||
1049 | /****************************************************************************************************************************************************/ |
1072 | /****************************************************************************************************************************************************/ |
1050 | /* Function: DirectoryEntryExist(s8 *, u8, u8, File_t *) */ |
1073 | /* Function: DirectoryEntryExist(s8 *, u8, u8, File_t *) */ |
1051 | /* */ |
1074 | /* */ |
1052 | /* Description: This function searches all possible dir entries until the file or directory is found or the end of the directory is reached */ |
1075 | /* Description: This function searches all possible dir entries until the file or directory is found or the end of the directory is reached */ |