sformatCtrl.cpp

00001 /*
00002   S-Format ファイル管理
00003   Satofumi KAMIMURA
00004   $Id$
00005 */
00006 
00007 #include "sformatCtrl.h"
00008 #include <stdio.h>
00009 #include <fstream>
00010 #include <iterator>
00011 
00012 
00013 SFormat_Ctrl::SFormat_Ctrl(void) : debug_level(0), error_message("no error") {
00014   char* level = getenv("DEBUG_VIEW");
00015   if (level) {
00016     debug_level = atoi(level);
00017   }
00018 }
00019 
00020 
00021 SFormat_Ctrl::~SFormat_Ctrl(void) {
00022 }
00023 
00024 
00025 const char* SFormat_Ctrl::what(void) {
00026   return error_message.c_str();
00027 }
00028 
00029 
00030 bool SFormat_Ctrl::load(const char* file) {
00031   text.clear();
00032 
00033   std::ifstream fin(file);
00034   text.assign(std::istream_iterator<char>(fin), std::istream_iterator<char>());
00035   int size = text.size();
00036 
00037   int index = 0;
00038   srec_t srec;
00039   while (index < size) {
00040     int n = parseSFormat(&srec, &text[index]);
00041     if (n <= 0) {
00042       error_message = "S-format syntax error.";
00043       return false;
00044     }
00045     if (debug_level > 0) {
00046       for (int i = 0; i < n; ++i) {
00047         fprintf(stderr, "%c", text[index + i]);
00048       }
00049       fprintf(stderr, "\n");
00050     }
00051     index += n;
00052   }
00053   return true;
00054 }
00055 
00056 
00057 int SFormat_Ctrl::hex2int(char ch) {
00058   return (ch >= '0' && ch <= '9') ? (ch - '0') : (ch - 'A' + 10);
00059 }
00060 
00061 
00062 int SFormat_Ctrl::parseSFormat(srec_t* srec, const char* line) {
00063   int index = 0;
00064   int addr_size = 2;
00065   int byte_size;
00066   int i;
00067   unsigned char check_sum;
00068   unsigned char data_sum;
00069 
00070   if (line[index++] != 'S') {   // 最初の文字が 'S' かどうか
00071     return -1;
00072   }
00073   srec->type = line[index++] - '0'; // type の格納
00074 
00075   // byte_size の格納
00076   byte_size = (hex2int(line[index]) << 4) + hex2int(line[index + 1]);
00077   index += 2;
00078 
00079   // address の格納
00080   if (srec->type >= 1 && srec->type <= 3) {
00081     addr_size = srec->type + 1;
00082   } else if (srec->type >= 7 && srec->type <= 9) {
00083     addr_size = 11 - srec->type;
00084   }
00085   srec->address = 0;
00086   for (i = 0; i < 2*addr_size; ++i) {
00087     srec->address = (srec->address << 4) + hex2int(line[index++]);
00088   }
00089 
00090   // byte_data の格納
00091   srec->data_size = byte_size - addr_size - 1;
00092   for (i = 0; i < srec->data_size; ++i) {
00093     srec->byte_data[i] =
00094       (hex2int(line[index]) << 4) + hex2int(line[index + 1]);
00095     index += 2;
00096   }
00097 
00098   // check sum の取得とチェック
00099   check_sum = (hex2int(line[index]) << 4) + hex2int(line[index + 1]);
00100   index += 2;
00101   data_sum = 0;
00102   for (i = 0; i <= (srec->data_size + addr_size); ++i) {
00103     data_sum += (hex2int(line[2*i+2]) << 4) + hex2int(line[2*i+3]);
00104   }
00105   if (check_sum != (~data_sum & 0xff)) {
00106     return -1;
00107   }
00108 
00109   return index;
00110 }
00111 

Generated on Mon Apr 13 22:52:05 2009 by  doxygen 1.5.7.1