function code_chart_source_file_rtw(fileNameInfo, chart, specsIdx)

%    Copyright 1995-2016 The MathWorks, Inc.

   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   %%%%%  GLOBAL VARIABLES
   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

   global gChartInfo %#ok<GVMIS>
   
   initialize_data_information(gChartInfo.chartData, gChartInfo.chartDataNumbers, chart);
   chartNumber = sf('get',chart,'chart.number');
   fileName = fullfile(fileNameInfo.targetDirName,fileNameInfo.chartSourceFiles{chartNumber+1}{specsIdx});
   sf_echo_generating('Coder',fileName);

   file = fopen(fileName,'Wt');
   if file<3
      construct_coder_error([],sprintf('Failed to create file: %s.',fileName),1);
      return;
   end

fprintf(file,'%%implements "chartSource" "C"\n');

fprintf(file,'%%function ChartConfig(block, system) void\n');
fprintf(file,'  %%createrecord chartConfiguration { ...\n');
fprintf(file,'          executeAtInitialization  %.17g ...\n',gChartInfo.executeAtInitialization);
fprintf(file,'  }\n');
fprintf(file,'  %%return chartConfiguration\n');
fprintf(file,'%%endfunction\n');

   % Only generate the ChartDataMap function,
   % which is used by globalmaplib.tlc.
   dump_chart_data_map_with_unified_dwork(file, chart);

   fclose(file);
   try_indenting_file(fileName);

function dump_chart_data_map_with_unified_dwork(file, chart)

    dworkInfo = sf('get', chart, 'chart.rtwInfo.dWorkVarInfo');
    numDWorks = length(dworkInfo);
    
fprintf(file,'%%function ChartDataMap(block, system) void\n');
fprintf(file,'  %%createrecord ChartDataElements {\\\n');
fprintf(file,'    NumChartData   %.17g \\\n',numDWorks);
fprintf(file,'    ChartDataDefaults {\\\n');
fprintf(file,'      RecordType   "ChartData"\\\n');
fprintf(file,'      Dimensions   []\\\n');
fprintf(file,'      IsTestPoint  0\\\n');
fprintf(file,'    }\\\n');

    for i = 1:numDWorks
        
fprintf(file,'    ChartData {\\\n');
fprintf(file,'      Name         "%s"\\\n',dworkInfo(i).varName);
fprintf(file,'      Description  "%s"\\\n',dworkInfo(i).description);
fprintf(file,'      SFName       "%s"\\\n',dworkInfo(i).objName);
fprintf(file,'      Path         "%s"\\\n',dworkInfo(i).path);
fprintf(file,'      SrcLocation  "%s"\\\n',dworkInfo(i).srcLocation);

      if ~isempty(dworkInfo(i).size)
          % Not a scalar, which is the default case
          dims = sprintf('%d,', dworkInfo(i).size);
fprintf(file,'      Dimensions   [%s]\\\n',dims(1:end-1));
      end
      
      if dworkInfo(i).isTestPoint
fprintf(file,'      IsTestPoint  1\\\n');
      end
        
fprintf(file,'    }\\\n');
    end
    
fprintf(file,'  }\n');
fprintf(file,'  %%return ChartDataElements\n');
fprintf(file,'%%endfunction\n');

idx = sf('get', chart, 'chart.rtwInfo.dataSetVecIdxInDWork');

if idx >= 0
fprintf(file,'    \n');
fprintf(file,'%%function USLRegCode(block, system) Output\n');

fprintf(file,'  %%assign simS            = RTMsGet(system, "MdlRefSfcnS")\n');
fprintf(file,'  %%assign blockPath       = LibGetBlockPath(block)\n');
fprintf(file,'  %%assign dWork           = LibBlockDWorkName(block)\n');
fprintf(file,'%%endfunction\n');
end
