MASA-Core
SpecialRow.cpp
Go to the documentation of this file.
00001 /*******************************************************************************
00002  *
00003  * Copyright (c) 2010-2015   Edans Sandes
00004  *
00005  * This file is part of MASA-Core.
00006  * 
00007  * MASA-Core is free software: you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation, either version 3 of the License, or
00010  * (at your option) any later version.
00011  * 
00012  * MASA-Core is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with MASA-Core.  If not, see <http://www.gnu.org/licenses/>.
00019  *
00020  ******************************************************************************/
00021 
00022 #include "SpecialRow.hpp"
00023 
00024 #include <stdlib.h>
00025 
00026 /*
00027  * @see description on header file
00028  */
00029 SpecialRow::SpecialRow() {
00030         this->offset = 0;
00031 }
00032 
00033 /*
00034  * @see description on header file
00035  */
00036 SpecialRow::~SpecialRow() {
00037 
00038 }
00039 
00040 /*
00041  * @see description on header file
00042  */
00043 void SpecialRow::open(bool readOnly, int length) {
00044         this->readOnly = readOnly;
00045         initialize(readOnly, length);
00046 }
00047 
00048 int SpecialRow::getType() {
00049         return INIT_WITH_CUSTOM_DATA;
00050 }
00051 
00052 /*
00053  * @see description on header file
00054  */
00055 void SpecialRow::setId(int id) {
00056         this->id = id;
00057 }
00058 
00059 /*
00060  * @see description on header file
00061  */
00062 int SpecialRow::getId() {
00063         return id;
00064 }
00065 
00066 /*
00067  * @see description on header file
00068  */
00069 int SpecialRow::sortById(SpecialRow* a, SpecialRow* b) {
00070         if (a == NULL) {
00071                 return true;
00072         } else if (b == NULL) {
00073                 return false;
00074         }
00075     return a->id < b->id;
00076 }
00077 
00078 /*
00079  * @see description on header file
00080  */
00081 void SpecialRow::seek(int offset) {
00082         if (!readOnly) {
00083         fprintf(stderr, "Cannot change offset of a read-only special row.\n");
00084         exit(1);
00085         }
00086         this->offset = offset;
00087 }
00088 
00089 /*
00090  * @see description on header file
00091  */
00092 int SpecialRow::getOffset() {
00093         return offset;
00094 }
00095 
00096 /*
00097  * @see description on header file
00098  */
00099 int SpecialRow::write(const cell_t* buf, int len) {
00100         int ret = write(buf, offset, len);
00101         if (ret != len) {
00102         fprintf(stderr, "Could not write bytes to special row: %d != %d\n", ret, len);
00103         perror("SpecialRow::write");
00104         exit(1);
00105         }
00106 
00107         offset += len;
00108         /*if (DEBUG) {
00109                 fprintf(stderr, "Debug[%s] %d. Tot: %d\n", filename.substr(filename.size()-8,8).c_str(), len, offset);
00110         }*/
00111 
00112 
00113         return len;
00114 }
00115 
00116 /*
00117  * @see description on header file
00118  */
00119 int SpecialRow::read(cell_t* buf, int len) {
00120         if (offset == 0) {
00121                 fprintf(stderr, "Error: Special Row overflow: %d (%08X).\n", len, this->id);
00122                 exit(1);
00123         }
00124         if (len > offset) {
00125                 len = offset;
00126         }
00127         offset -= len;
00128         if (buf != NULL) {
00129                 int ret = read(buf, offset, len);
00130                 if (ret != len) {
00131                         fprintf(stderr, "Error: End of special row (%d).\n", len-ret);
00132                         exit(1);
00133                 }
00134 
00135                 // Reverse buffer order
00136                 for (int i=0; i<len/2; i++) {
00137                         cell_t aux = buf[i];
00138                         buf[i] = buf[len-1-i];
00139                         buf[len-1-i] = aux;
00140                 }
00141         }
00142 
00143         return len;
00144 }
00145 
00146 
00147