Index: /trunk/FACT++/src/fitsdump.cc
===================================================================
--- /trunk/FACT++/src/fitsdump.cc	(revision 11910)
+++ /trunk/FACT++/src/fitsdump.cc	(revision 11911)
@@ -197,8 +197,12 @@
 {
     int cnt = 0;
-
     vector<pair<int, int> >::const_iterator jt = ranges.begin();
     for (vector<MyColumn*>::const_iterator it=list.begin(); it!=list.end(); it++, jt++)
     {
+        if (jt == ranges.end())
+        {
+            cout << "ERROR: END OF RANGE POINTER REACHED" << endl;
+            return false;
+        }
         const MyColumn *col = *it;
 
@@ -210,4 +214,22 @@
 
         // Loop over all array entries
+        int sizeToSkip = 0;
+        switch (col->type())
+        {
+        case CCfits::Tbyte:     sizeToSkip = sizeof(uint8_t); break;
+        case CCfits::Tushort:   sizeToSkip = sizeof(uint16_t); break;
+        case CCfits::Tuint:
+        case CCfits::Tulong:    sizeToSkip = sizeof(uint32_t); break;
+        case CCfits::Tshort:    sizeToSkip = sizeof(int16_t); break;
+        case CCfits::Tint:
+        case CCfits::Tlong:     sizeToSkip = sizeof(int32_t); break;
+        case CCfits::Tlonglong: sizeToSkip = sizeof(int64_t); break;
+        case CCfits::Tfloat:    sizeToSkip = sizeof(float); break;
+        case CCfits::Tdouble:   sizeToSkip = sizeof(double); break;
+        default:
+            cerr << "Data type not implemented yet." << endl;
+            return 0;
+        }
+        ptr += sizeToSkip*jt->first;
         for (int width=jt->first; width<jt->second; width++)
         {
@@ -467,11 +489,64 @@
 bool FitsDumper::Dump(const string &filename, const vector<string> &list, int precision)
 {
+
+    //first of all, let's separate the columns from their ranges and check that the requested columns are indeed part of the file
+    vector<pair<int, int> > ranges;
+    vector<string> listNamesOnly;
     for (vector<string>::const_iterator it=list.begin(); it!=list.end(); it++)
-        if (fColMap.find(*it) == fColMap.end())
-        {
-            cerr << "WARNING - Column '" << *it << "' not found in table." << endl;
+    {
+        string columnNameOnly = *it;
+        unsigned long bracketIndex0 = columnNameOnly.find_first_of('[');
+        unsigned long bracketIndex1 = columnNameOnly.find_first_of(']');
+        unsigned long colonIndex = columnNameOnly.find_first_of(':');
+        cout << bracketIndex0 << " " << bracketIndex1 << " " << colonIndex << endl;
+        int columnStart = -1;
+        int columnEnd = -1;
+        if (bracketIndex0 != string::npos)
+        {//there is a range given. Extract the range
+            if (colonIndex != string::npos)
+            {//we have a range here
+                columnStart = atoi(columnNameOnly.substr(bracketIndex0+1, colonIndex-(bracketIndex0+1)).c_str());
+                columnEnd = atoi(columnNameOnly.substr(colonIndex+1, bracketIndex1-(colonIndex+1)).c_str());
+                columnEnd++;
+            }
+            else
+            {//only a single index there
+                columnStart = atoi(columnNameOnly.substr(bracketIndex0+1, bracketIndex1 - (bracketIndex0+1)).c_str());
+                columnEnd = columnStart+1;
+//                cout << "Cstart " << columnStart  << " end: " << columnEnd << endl;
+            }
+            columnNameOnly = columnNameOnly.substr(0, bracketIndex0);
+        }
+
+        if (fColMap.find(columnNameOnly) == fColMap.end())
+        {
+            cerr << "ERROR - Column '" << columnNameOnly << "' not found in table." << endl;
             return false;
         }
-
+//        cout << "The column name is: " << columnNameOnly << endl;
+        MyColumn *cCol = static_cast<MyColumn*>(fColMap.find(columnNameOnly)->second);
+        if (bracketIndex0 == string::npos)
+        {//no range given: use the full range
+            ranges.push_back(make_pair(0, cCol->width()));
+            columnStart = 0;
+            columnEnd = 1;
+        }
+        else
+        {//use the range extracted earlier
+            if (columnStart < 0)
+            {
+                cerr << "ERROR - Start range for column " << columnNameOnly << " is less than zero (" << columnStart << "). Aborting" << endl;
+                return false;
+            }
+            if (columnEnd > cCol->width())
+            {
+                cerr << "ERROR - End range for column " << columnNameOnly << " is greater than the last element (" << cCol->width() << " vs " << columnEnd << "). Aborting" << endl;
+                return false;
+            }
+            ranges.push_back(make_pair(columnStart, columnEnd));
+        }
+//        cout << "Will be exporting from " << columnStart << " to " << columnEnd-1 << " for column " << columnNameOnly << endl;
+        listNamesOnly.push_back(columnNameOnly);
+    }
     // FIXME: Maybe do this when opening a table?
     const vector<int> offsets = CalculateOffsets();
@@ -501,5 +576,5 @@
     out << "## --------------------------------------------------------------------------\n";
     out << "#\n";
-    for (vector<string>::const_iterator it=list.begin(); it!=list.end(); it++)
+    for (vector<string>::const_iterator it=listNamesOnly.begin(); it!=listNamesOnly.end(); it++)
     {
         const MyColumn *col = static_cast<MyColumn*>(fTable->column()[*it]);
@@ -514,11 +589,10 @@
     // Loop over all columns in our list of requested columns
     vector<MyColumn*> columns;
-    vector<pair<int, int> > ranges;
-    for (vector<string>::const_iterator it=list.begin(); it!=list.end(); it++)
+    for (vector<string>::const_iterator it=listNamesOnly.begin(); it!=listNamesOnly.end(); it++)
     {
         MyColumn *cCol = static_cast<MyColumn*>(fColMap.find(*it)->second);
         //CCfits::Column* cCol = fColMap.find(*it)->second;
         columns.push_back(cCol);
-        int start, end;
+/*        int start, end;
         if (cCol->width() != 1)
         {//get the desired range of that column to output
@@ -544,7 +618,6 @@
         {
             ranges.push_back(make_pair(0, cCol->width()));
-        }
-    }
-
+        }*/
+    }
     const int size = offsets[offsets.size()-1];
     unsigned char* fitsBuffer = new unsigned char[size];
