?login_element?

Subversion Repositories NedoOS

Rev

Rev 2005 | Rev 2023 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #include <math.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <oscalls.h>
  6. #include <osfs.h>
  7. #include <intrz80.h>
  8. #include <ctype.h>
  9. #include <tcp.h>
  10. #include <graphic.h>
  11. #include <terminal.c>
  12. struct fileStruct
  13. {
  14.   long picId;
  15.   unsigned int picYear;
  16.   unsigned long totalAmount;
  17.   unsigned char picRating[8];
  18.   unsigned char picName[256];
  19.   unsigned char picType[64];
  20.   unsigned char authorIds[64];
  21.   unsigned char authorTitle[64];
  22.   unsigned char authorRealName[64];
  23.   unsigned char afn[128];
  24.   unsigned char pfn[128];
  25.   unsigned char fileName[128];
  26. } curFileStruct;
  27. unsigned char ver[] = "2.4";
  28. unsigned char netbuf[2048];
  29. unsigned char picture[16384];
  30. unsigned char crlf[2] = {13, 10};
  31. unsigned char status, keypress, verbose, randomPic, slideShow;
  32. struct sockaddr_in targetadr;
  33. struct readstructure readStruct;
  34. unsigned long contLen;
  35. unsigned long count;
  36. unsigned int headlng;
  37. unsigned int slideShowTime = 0;
  38.  
  39. struct packetStruct
  40. {
  41.   unsigned long contLen;
  42.   unsigned int headlng;
  43. } pack;
  44.  
  45. void emptyKeys(void)
  46. {
  47.   unsigned char loop, key = 1;
  48.   do
  49.   {
  50.     key = _low_level_get();
  51.     if (loop > 64)
  52.     {
  53.       break;
  54.     }
  55.     loop++;
  56.   } while (key != 0);
  57. }
  58.  
  59. void printHelp(void)
  60. {
  61.   ATRIB(95);
  62.   printf("   GETPIC [%s] zxart.ee picture viewer for nedoNET\n\r", ver);
  63.   ATRIB(33);
  64.   ATRIB(40);
  65.   printf("-------------------------------------------------------\n\r");
  66.   printf(" Управление:\n\r");
  67.   printf(" 'ESC' - выход из программы;\n\r");
  68.   printf(" '<-' или 'B' к последним картинкам;\n\r");
  69.   printf(" '->' или 'Пробел' к более старым картинкам\n\r");
  70.   printf(" 'J' Прыжок на  указанную по счету картинку\n\r");
  71.   printf(" 'I' Просмотр экрана информации о картинках\n\r");
  72.   printf(" 'S' Сохранить картинку на диск в текущую папку\n\r");
  73.   printf(" 'V' не выводить информацию об авторах\n\r");
  74.   printf(" 'R' переход в режим  случайная картинка с рейтингом 4+\n\r");
  75.   printf(" 'A' переход в режим  слайд-шоу\n\r");
  76.   printf(" 'H' Данная справочная информация\n\r");
  77.   printf("-----------------Нажмите любую кнопку------------------\n\r");
  78.   ATRIB(93);
  79.   keypress = getchar();
  80. }
  81. void delay(unsigned long counter)
  82. {
  83.   unsigned long start, finish;
  84.   counter = counter / 20;
  85.   if (counter < 1)
  86.   {
  87.     counter = 1;
  88.   }
  89.   start = time();
  90.   finish = start + counter;
  91.  
  92.   while (start < finish)
  93.   {
  94.     start = time();
  95.   }
  96. }
  97.  
  98. void errorPrint(unsigned int error)
  99. {
  100.   switch (error)
  101.   {
  102.   case 2:
  103.     printf("02 SHUT_RDWR");
  104.     break;
  105.   case 4:
  106.     printf("04 ERR_INTR");
  107.     break;
  108.   case 23:
  109.     printf("23 ERR_NFILE");
  110.     break;
  111.   case 35:
  112.     printf("35 ERR_EAGAIN");
  113.     break;
  114.   case 37:
  115.     printf("37 ERR_ALREADY");
  116.     break;
  117.   case 38:
  118.     printf("38 ERR_NOTSOCK");
  119.     break;
  120.   case 40:
  121.     printf("40 ERR_EMSGSIZE");
  122.     break;
  123.   case 41:
  124.     printf("41 ERR_PROTOTYPE");
  125.     break;
  126.   case 47:
  127.     printf("47 ERR_AFNOSUPPORT");
  128.     break;
  129.   case 53:
  130.     printf("53 ERR_ECONNABORTED");
  131.     break;
  132.   case 54:
  133.     printf("54 ERR_CONNRESET");
  134.     break;
  135.   case 57:
  136.     printf("57 ERR_NOTCONN");
  137.     break;
  138.   case 65:
  139.     printf("65 ERR_HOSTUNREACH");
  140.     break;
  141.   default:
  142.     printf("%u UNKNOWN ERROR", error);
  143.     break;
  144.   }
  145. }
  146.  
  147. signed char OpenSock(unsigned char family, unsigned char protocol)
  148. {
  149.   signed char socket;
  150.   unsigned int todo;
  151.   todo = OS_NETSOCKET((family << 8) + protocol);
  152.   if (todo > 32767)
  153.   {
  154.     printf("OS_NETSOCKET: [ERROR:");
  155.     errorPrint(todo & 255);
  156.     printf("] Press any key.");
  157.     getchar();
  158.     exit(0);
  159.   }
  160.   else
  161.   {
  162.     socket = ((todo & 65280) >> 8);
  163.     // printf("OS_NETSOCKET: Socket #%d created\n\r", socket);
  164.   }
  165.   return socket;
  166. }
  167.  
  168. signed char netShutDown(signed char socket, unsigned char type)
  169. {
  170.   unsigned int todo;
  171.   todo = OS_NETSHUTDOWN(socket, type);
  172.   if (todo > 32767)
  173.   {
  174.     printf("OS_NETSHUTDOWN: [ERROR:");
  175.     errorPrint(todo & 255);
  176.     printf("] Press any key.");
  177.     return -1;
  178.   }
  179.   else
  180.   {
  181.     // printf("Socket #%d closed.\n\r", socket);
  182.   }
  183.   return 1;
  184. }
  185.  
  186. unsigned char netConnect(signed char socket)
  187. {
  188.   unsigned int todo, retry = 10;
  189.  
  190.   targetadr.family = AF_INET;
  191.   targetadr.porth = 00;
  192.   targetadr.portl = 80;
  193.   targetadr.b1 = 217;
  194.   targetadr.b2 = 146;
  195.   targetadr.b3 = 69;
  196.   targetadr.b4 = 13;
  197.   while (retry != 0)
  198.   {
  199.     todo = OS_NETCONNECT(socket, &targetadr);
  200.  
  201.     if (todo > 32767)
  202.     {
  203.       retry--;
  204.       printf("OS_NETCONNECT [ERROR:");
  205.       errorPrint(todo & 255);
  206.       printf("] [Retry:%u] [Pic:%lu]\r\n", retry, count);
  207.       YIELD();
  208.       netShutDown(socket, 0);
  209.       socket = OpenSock(AF_INET, SOCK_STREAM);
  210.     }
  211.     else
  212.     {
  213.       // printf("OS_NETCONNECT: connection successful, %u\n\r", (todo & 255));
  214.       return 1;
  215.     }
  216.   }
  217.   getchar();
  218.   exit(0);
  219.   return 0;
  220. }
  221.  
  222. unsigned int tcpSend(signed char socket, unsigned int messageadr, unsigned int size)
  223. {
  224.   unsigned char retry = 10;
  225.   unsigned int todo;
  226.   readStruct.socket = socket;
  227.   readStruct.BufAdr = messageadr;
  228.   readStruct.bufsize = size;
  229.   readStruct.protocol = SOCK_STREAM;
  230.   while (retry > 0)
  231.   {
  232.     todo = OS_WIZNETWRITE(&readStruct);
  233.     if (todo > 32767)
  234.     {
  235.       printf("OS_WIZNETWRITE: [ERROR:");
  236.       errorPrint(todo & 255);
  237.       printf("] [Retry:%u] [Pic:%lu]\r\n", retry, count);
  238.       YIELD();
  239.       retry--;
  240.     }
  241.     else
  242.     {
  243.       // printf("OS_WIZNETWRITE: %u bytes written. \n\r", todo);
  244.       return todo;
  245.     }
  246.   }
  247.  
  248.   getchar();
  249.   exit(0);
  250.   return todo;
  251. }
  252.  
  253. unsigned int tcpRead(signed char socket)
  254. {
  255.   unsigned char retry = 10;
  256.   unsigned int todo;
  257.  
  258.   readStruct.socket = socket;
  259.   readStruct.BufAdr = (unsigned int)&netbuf;
  260.   readStruct.bufsize = sizeof(netbuf);
  261.   readStruct.protocol = SOCK_STREAM;
  262.  
  263.   while (retry > 0)
  264.   {
  265.     todo = OS_WIZNETREAD(&readStruct);
  266.  
  267.     if (todo > 32767)
  268.     {
  269.       if ((todo & 255) != ERR_EAGAIN)
  270.       {
  271.         printf("OS_WIZNETREAD: [ERROR:");
  272.         errorPrint(todo & 255);
  273.         printf("] [Retry:%u] [Pic:%lu]\r\n", retry, count);
  274.         YIELD();
  275.         retry--;
  276.       }
  277.     }
  278.     else
  279.     {
  280.       // printf("OS_WIZNETREAD: %u bytes read. \n\r", todo);
  281.       return todo;
  282.     }
  283.   }
  284.   getchar();
  285.   exit(0);
  286.   return todo;
  287. }
  288.  
  289. unsigned int cutHeader(unsigned int todo)
  290. {
  291.   unsigned int q;
  292.   unsigned char *count1;
  293.   count1 = strstr(netbuf, "Content-Length:");
  294.   if (count1 == NULL)
  295.   {
  296.     printf("contLen  not found \r\n");
  297.     contLen = 0;
  298.   }
  299.   else
  300.   {
  301.     contLen = atol(count1 + 15);
  302.     // printf ("Dlinna  soderzhimogo = %lu \n\r", contLen);
  303.   }
  304.  
  305.   count1 = strstr(netbuf, "\r\n\r\n");
  306.   if (count1 == NULL)
  307.   {
  308.     printf("header not found\r\n");
  309.   }
  310.   else
  311.   {
  312.     headlng = ((unsigned int)count1 - (unsigned int)netbuf + 4);
  313.     q = todo - headlng;
  314.     // printf("header removed. %u bytes\r\n", headlng);
  315.   }
  316.  
  317.   return q;
  318. }
  319.  
  320. char *str_replace(char *dst, int num, const char *str,
  321.                   const char *orig, const char *rep)
  322. {
  323.   const char *ptr;
  324.   size_t len1 = strlen(orig);
  325.   size_t len2 = strlen(rep);
  326.   char *tmp = dst;
  327.  
  328.   num -= 1;
  329.   while ((ptr = strstr(str, orig)) != NULL)
  330.   {
  331.     num -= (ptr - str) + len2;
  332.     if (num < 1)
  333.       break;
  334.  
  335.     strncpy(dst, str, (size_t)(ptr - str));
  336.     dst += ptr - str;
  337.     strncpy(dst, rep, len2);
  338.     dst += len2;
  339.     str = ptr + len1;
  340.   }
  341.  
  342.   for (; (*dst = *str) && (num > 0); --num)
  343.   {
  344.     ++dst;
  345.     ++str;
  346.   }
  347.   return tmp;
  348. }
  349.  
  350. void fillPicture(signed char socket)
  351. {
  352.   unsigned int todo, w, pPos, headskip;
  353.   headskip = 0;
  354.   pPos = 0;
  355.   while (1)
  356.   {
  357.     headlng = 0;
  358.     todo = tcpRead(socket);
  359.     if (todo == 0)
  360.     {
  361.       break;
  362.     }
  363.     if (headskip == 0)
  364.     {
  365.       headskip = 1;
  366.       todo = cutHeader(todo);
  367.     }
  368.  
  369.     if (pPos + todo > sizeof(picture))
  370.     {
  371.       printf("dataBuffer overrun... %u reached \n\r", pPos + todo);
  372.       break;
  373.     }
  374.     // memcpy(&picture + pPos, &netbuf + headlng, todo );
  375.  
  376.     for (w = 0; w < todo; w++)
  377.     {
  378.       picture[w + pPos] = netbuf[w + headlng];
  379.     }
  380.  
  381.     pPos = pPos + todo;
  382.     if (pPos == contLen)
  383.     {
  384.       break;
  385.     }
  386.   }
  387.   netShutDown(socket, 0);
  388. }
  389. void nameRepair(unsigned char *pfn, unsigned int tfnSize)
  390. {
  391.  
  392.   str_replace(pfn, tfnSize, pfn, "\\", "_");
  393.   str_replace(pfn, tfnSize, pfn, "/", "_");
  394.   str_replace(pfn, tfnSize, pfn, ":", "_");
  395.   str_replace(pfn, tfnSize, pfn, "*", "_");
  396.   str_replace(pfn, tfnSize, pfn, "?", "_");
  397.   str_replace(pfn, tfnSize, pfn, "<", "_");
  398.   str_replace(pfn, tfnSize, pfn, ">", "_");
  399.   str_replace(pfn, tfnSize, pfn, "|", "_");
  400.   str_replace(pfn, tfnSize, pfn, " ", "_");
  401.   str_replace(pfn, tfnSize, pfn, "&#039;", "'");
  402.   str_replace(pfn, tfnSize, pfn, "&amp;", "&");
  403.   str_replace(pfn, tfnSize, pfn, "&quot;", "'");
  404.   str_replace(pfn, tfnSize, pfn, "&gt;", ")");
  405.   str_replace(pfn, tfnSize, pfn, "&lt;", "(");
  406.   str_replace(pfn, tfnSize, pfn, "\"", "'");
  407. }
  408.  
  409. void stringRepair(unsigned char *pfn, unsigned int tSize)
  410. {
  411.   str_replace(pfn, tSize, pfn, "&#039;", "'");
  412.   str_replace(pfn, tSize, pfn, "&amp;", "&");
  413.   str_replace(pfn, tSize, pfn, "&gt;", ">");
  414.   str_replace(pfn, tSize, pfn, "&lt;", "<");
  415.   str_replace(pfn, tSize, pfn, "&quot;", "\"");
  416.   str_replace(pfn, tSize, pfn, "\\/", "/");
  417. }
  418.  
  419. unsigned char getPic(unsigned long fileId)
  420. {
  421.   unsigned int todo;
  422.   unsigned char buffer[] = "0000000000";
  423.   signed char socket;
  424.   socket = OpenSock(AF_INET, SOCK_STREAM);
  425.   todo = netConnect(socket);
  426.   sprintf(buffer, "%lu", fileId);
  427.   strcpy(netbuf, "GET /file/id:");
  428.   strcat(netbuf, buffer);
  429.   strcat(netbuf, " HTTP/1.1\r\nHost: zxart.ee\r\nUser-Agent: User-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS)\r\n\r\n\0");
  430.   todo = tcpSend(socket, (unsigned int)&netbuf, strlen(netbuf));
  431.   fillPicture(socket);
  432.   return 0;
  433. }
  434.  
  435. void ncReplace(void)
  436. {
  437.   unsigned char len;
  438.   for (len = 0; len < strlen(curFileStruct.afn); len++)
  439.   {
  440.     if (curFileStruct.afn[len] < ' ')
  441.     {
  442.       curFileStruct.afn[len] = '_';
  443.     }
  444.   }
  445.  
  446.   for (len = 0; len < strlen(curFileStruct.pfn); len++)
  447.   {
  448.     if (curFileStruct.pfn[len] < ' ')
  449.     {
  450.       curFileStruct.pfn[len] = '_';
  451.     }
  452.   }
  453. }
  454.  
  455. unsigned char savePic(unsigned long fileId)
  456. {
  457.   FILE *fp2;
  458.   unsigned char afnSize, tfnSize;
  459.   unsigned char fileIdChar[10];
  460.  
  461.   afnSize = sizeof(curFileStruct.afn) - 1;
  462.   tfnSize = sizeof(curFileStruct.pfn) - 1;
  463.  
  464.   strcpy(curFileStruct.afn, curFileStruct.authorTitle);
  465.   nameRepair(curFileStruct.afn, afnSize);
  466.  
  467.   strcpy(curFileStruct.pfn, curFileStruct.picName);
  468.   nameRepair(curFileStruct.pfn, tfnSize);
  469.   ncReplace();
  470.  
  471.   sprintf(curFileStruct.fileName, "%s-%s-%ld.scr", curFileStruct.afn, curFileStruct.pfn, fileId);
  472.   if (strlen(curFileStruct.fileName) > 62)
  473.   {
  474.     sprintf(fileIdChar, "-%ld", fileId);
  475.     str_replace(curFileStruct.fileName, sizeof(curFileStruct.fileName) - 1, curFileStruct.fileName, fileIdChar, "");
  476.     curFileStruct.fileName[50] = '\0';
  477.     strcat(curFileStruct.fileName, fileIdChar);
  478.     strcat(curFileStruct.fileName, ".scr");
  479.   }
  480.   OS_SETSYSDRV();
  481.   OS_MKDIR("../downloads");        // Create if not exist
  482.   OS_MKDIR("../downloads/getpic"); // Create if not exist
  483.   OS_CHDIR("../downloads/getpic");
  484.   fp2 = OS_CREATEHANDLE(curFileStruct.fileName, 0x80);
  485.   if (((int)fp2) & 0xff)
  486.   {
  487.     printf("%s creating error\r\n", curFileStruct.fileName);
  488.     getchar();
  489.     exit(0);
  490.   }
  491.   OS_WRITEHANDLE(picture, fp2, 6912);
  492.   OS_CLOSEHANDLE(fp2);
  493.   return 0;
  494. }
  495.  
  496. int pos(unsigned char *s, unsigned char *c, unsigned int n, unsigned int startPos)
  497. {
  498.   unsigned int i, j;
  499.   unsigned int lenC, lenS;
  500.  
  501.   for (lenC = 0; c[lenC]; lenC++)
  502.     ;
  503.   for (lenS = 0; s[lenS]; lenS++)
  504.     ;
  505.  
  506.   for (i = startPos; i <= lenS - lenC; i++)
  507.   {
  508.     for (j = 0; s[i + j] == c[j]; j++)
  509.       ;
  510.  
  511.     if (j - lenC == 1 && i == lenS - lenC && !(n - 1))
  512.       return i;
  513.     if (j == lenC)
  514.       if (n - 1)
  515.         n--;
  516.       else
  517.         return i;
  518.   }
  519.   return -1;
  520. }
  521.  
  522. const char *parseJson(unsigned char *property)
  523. {
  524.   unsigned int w, lng, lngp1, findEnd, listPos;
  525.   unsigned char terminator;
  526.   int n;
  527.   n = -1;
  528.   // netbuf[0] = '\0';
  529.   n = pos(picture, property, 1, 0);
  530.   if (n == -1)
  531.   {
  532.     strcpy(netbuf, "-");
  533.     // printf("Property %s not found", property);
  534.     return netbuf;
  535.   }
  536.   lng = n - 1 + strlen(property);
  537.   if (picture[lng] == ':')
  538.   {
  539.     terminator = '\0';
  540.   }
  541.   if (picture[lng] == '\"')
  542.   {
  543.     terminator = '\"';
  544.   }
  545.   if (picture[lng] == '[')
  546.   {
  547.     terminator = ']';
  548.   }
  549.  
  550.   findEnd = 1;
  551.   lngp1 = lng + 1;
  552.  
  553.   while (42)
  554.   {
  555.  
  556.     if ((picture[lngp1 + findEnd] == ','))
  557.     {
  558.       if (terminator == '\0')
  559.       {
  560.         break;
  561.       }
  562.       if ((picture[lng + findEnd] == terminator))
  563.       {
  564.         findEnd--;
  565.         break;
  566.       }
  567.     }
  568.     findEnd++;
  569.   }
  570.   listPos = 0;
  571.   for (w = lngp1; w < findEnd + lngp1; w++)
  572.   {
  573.     netbuf[listPos] = picture[w];
  574.     listPos++;
  575.   }
  576.   netbuf[listPos] = '\0';
  577.   return netbuf;
  578. }
  579. void convert866(void)
  580. {
  581.   unsigned int lng, targetPos, w, q = 0;
  582.   unsigned char buffer[8], one, two;
  583.   unsigned int decVal;
  584.   lng = strlen(netbuf);
  585.   targetPos = lng + 1;
  586.  
  587.   while (q < lng)
  588.   {
  589.     one = netbuf[q];
  590.     two = netbuf[q + 1];
  591.     if (one == 92 && two == 117)
  592.     {
  593.       q = q + 2;
  594.       for (w = 0; w < 4; w++)
  595.       {
  596.         buffer[w] = netbuf[q + w];
  597.       }
  598.       q = q + 4;
  599.       buffer[4] = '\0';
  600.       decVal = (unsigned int)strtol(buffer, NULL, 16);
  601.  
  602.       if (decVal < 1088)
  603.       {
  604.         decVal = decVal - 912;
  605.       }
  606.       if (decVal > 1087)
  607.       {
  608.         decVal = decVal - 864;
  609.       }
  610.       if (decVal == 1025)
  611.       {
  612.         decVal = 240;
  613.       }
  614.       if (decVal == 1105)
  615.       {
  616.         decVal = 241;
  617.       }
  618.  
  619.       netbuf[targetPos] = decVal;
  620.     }
  621.     else
  622.     {
  623.       netbuf[targetPos] = netbuf[q];
  624.       q++;
  625.     }
  626.     targetPos++;
  627.   }
  628.   netbuf[targetPos] = 0;
  629.  
  630.   for (w = lng + 1; w < targetPos + 1; w++)
  631.   {
  632.     netbuf[w - lng - 1] = netbuf[w];
  633.   }
  634. }
  635.  
  636. long processJson(unsigned long startPos, unsigned char limit, unsigned char queryNum)
  637. {
  638.   unsigned int retry, tSize;
  639.   unsigned int todo;
  640.   unsigned char buffer[] = "000000000";
  641.   unsigned char *count1, socket;
  642.   unsigned char userAgent[] = " HTTP/1.1\r\nHost: zxart.ee\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS; GetPic)\r\n\r\n\0";
  643.  
  644.   switch (queryNum)
  645.   {
  646.   case 0:
  647.     strcpy(netbuf, "GET /api/export:zxPicture/filter:zxPictureType=standard/limit:");
  648.     sprintf(buffer, "%u", limit);
  649.     strcat(netbuf, buffer);
  650.     strcat(netbuf, "/start:");
  651.     sprintf(buffer, "%lu", startPos);
  652.     strcat(netbuf, buffer);
  653.     strcat(netbuf, "/order:date,desc");
  654.     strcat(netbuf, userAgent);
  655.     break;
  656.  
  657.   case 1:
  658.     strcpy(netbuf, "GET /api/types:zxPicture/export:zxPicture/language:eng/start:0/limit:1/order:rand/filter:zxPictureMinRating=4;zxPictureType=standard");
  659.     strcat(netbuf, userAgent);
  660.     break;
  661.  
  662.   case 3: // /api/export:author/filter:authorId=2202
  663.     strcpy(netbuf, "GET /api/export:author/filter:authorId=");
  664.     sprintf(buffer, "%lu", startPos);
  665.     strcat(netbuf, buffer);
  666.     strcat(netbuf, userAgent);
  667.     break;
  668.  
  669.   case 99: // GET /jsonElementData/elementId:182798
  670.     strcpy(netbuf, "GET /jsonElementData/elementId:");
  671.     sprintf(buffer, "%lu", startPos);
  672.     strcat(netbuf, buffer);
  673.     strcat(netbuf, userAgent);
  674.     break;
  675.   }
  676.   retry = 10;
  677.   while (42)
  678.   {
  679.     socket = OpenSock(AF_INET, SOCK_STREAM);
  680.     netConnect(socket);
  681.     todo = tcpSend(socket, (unsigned int)&netbuf, strlen(netbuf));
  682.     fillPicture(socket);
  683.  
  684.     count1 = strstr(picture, "responseStatus\":\"success");
  685.     if (count1 == NULL)
  686.     {
  687.       retry--;
  688.       ATRIB(91);
  689.       printf("PROCESS JSON: [ERROR: Bad responseStatus.] [Query:%u][Retry:%u] [Pic:%lu]\r\n", queryNum, retry, startPos);
  690.       YIELD();
  691.       puts(netbuf);
  692.       getchar();
  693.       if (retry < 1)
  694.       {
  695.         return -1;
  696.       }
  697.     }
  698.     else
  699.     {
  700.       break;
  701.     }
  702.   }
  703.  
  704.   count1 = strstr(picture, "\"id\":");
  705.   if (count1 == NULL)
  706.   {
  707.     ATRIB(91);
  708.     printf("PROCESS JSON: [ERROR: ID not found.] [Query:%u][Pic:%lu]\r\n", queryNum, startPos);
  709.     YIELD();
  710.     return -2;
  711.   }
  712.  
  713.   netbuf[0] = '\0';
  714.   if (queryNum < 3)
  715.   {
  716.     parseJson("\"id\":");
  717.     curFileStruct.picId = atol(netbuf);
  718.     parseJson(",\"title\":\"");
  719.     convert866();
  720.     strcpy(curFileStruct.picName, netbuf);
  721.  
  722.     tSize = sizeof(curFileStruct.picName);
  723.     stringRepair(curFileStruct.picName, tSize);
  724.  
  725.     parseJson(",\"type\":\"");
  726.     strcpy(curFileStruct.picType, netbuf);
  727.     parseJson("\"rating\":\"");
  728.     strcpy(curFileStruct.picRating, netbuf);
  729.     parseJson("\"year\":\"");
  730.     curFileStruct.picYear = atoi(netbuf);
  731.     parseJson("\"totalAmount\":");
  732.     curFileStruct.totalAmount = atol(netbuf);
  733.     parseJson("\"authorIds\":[");
  734.     strcpy(curFileStruct.authorIds, netbuf);
  735.   }
  736.   if (queryNum == 99)
  737.   {
  738.     parseJson(",\"title\":\"");
  739.     convert866();
  740.     strcpy(curFileStruct.authorTitle, netbuf);
  741.     parseJson(",\"realName\":\"");
  742.     convert866();
  743.     strcpy(curFileStruct.authorRealName, netbuf);
  744.   }
  745.   return curFileStruct.picId;
  746. }
  747.  
  748. void printData(void)
  749. {
  750.   ATRIB(93);
  751.   printf(" #: ");
  752.   ATRIB(97);
  753.   printf("%lu", count);
  754.   ATRIB(93);
  755.   printf(" ID: ");
  756.   ATRIB(97);
  757.   printf("%lu ", curFileStruct.picId);
  758.   ATRIB(93);
  759.   printf(" Total Pics: ");
  760.   ATRIB(97);
  761.   printf("%lu\r\n", curFileStruct.totalAmount);
  762.   ATRIB(93);
  763.   printf(" Author: ");
  764.   ATRIB(96);
  765.   printf("%s\r\n", curFileStruct.authorTitle);
  766.   ATRIB(93);
  767.   printf(" TITLE: ");
  768.   ATRIB(95);
  769.   printf("%s\r\n", curFileStruct.picName);
  770.   ATRIB(93);
  771.   printf(" RATING: ");
  772.   ATRIB(97);
  773.   printf("%s", curFileStruct.picRating);
  774.   ATRIB(93);
  775.   printf(" YEAR: ");
  776.   ATRIB(97);
  777.   printf("%u\r\n", curFileStruct.picYear);
  778.   ATRIB(93);
  779.   printf(" AuthorsIDs ");
  780.   ATRIB(97);
  781.   printf("%s", curFileStruct.authorIds);
  782.   ATRIB(93);
  783.   printf(" Real name: ");
  784.   ATRIB(97);
  785.   printf("%s\r\n", curFileStruct.authorRealName);
  786.   ATRIB(96);
  787.   printf("\r\n");
  788.   YIELD();
  789. }
  790. void safeKeys(unsigned char keypress)
  791. {
  792.   if (keypress == 27)
  793.   {
  794.     printf("Good bye...\r\n");
  795.     ATRIB(37);
  796.     ATRIB(40);
  797.     exit(0);
  798.   }
  799.  
  800.   if (keypress == 'j' || keypress == 'J')
  801.   {
  802.     printf("Jump to picture:");
  803.     scanf("%lu", &count);
  804.     if (count > curFileStruct.totalAmount - 1)
  805.     {
  806.       count = curFileStruct.totalAmount - 1;
  807.     }
  808.   }
  809.  
  810.   if (keypress == 'v' || keypress == 'V')
  811.   {
  812.     verbose = !verbose;
  813.  
  814.     if (verbose == 0)
  815.     {
  816.       BOX(1, 1, 80, 25, 40);
  817.       AT(1, 1);
  818.     }
  819.   }
  820.  
  821.   if (keypress == 'h' || keypress == 'H')
  822.   {
  823.     printHelp();
  824.   }
  825.  
  826.   if (keypress == 'r' || keypress == 'R')
  827.   {
  828.     randomPic = !randomPic;
  829.  
  830.     if (verbose == 1)
  831.     {
  832.       if (randomPic == 1)
  833.       {
  834.         printf("    Random mode enabled...\r\n");
  835.       }
  836.       else
  837.       {
  838.         printf("    Sequental mode enabled...\r\n");
  839.       }
  840.     }
  841.   }
  842.   if (keypress == 'a' || keypress == 'A')
  843.   {
  844.     slideShow = !slideShow;
  845.     if (slideShow == 1)
  846.     {
  847.       if (verbose == 1)
  848.         printf("    SlideShow mode enabled...\r\n\r\n");
  849.       slideShowTime = 250;
  850.     }
  851.     else
  852.     {
  853.       if (verbose == 1)
  854.         printf("    Manual mode enabled...\r\n\r\n");
  855.       slideShowTime = 0;
  856.     }
  857.   }
  858. }
  859.  
  860. C_task main(void)
  861. {
  862.   unsigned char errno;
  863.   unsigned long ipadress;
  864.   long iddqd, idkfa;
  865.   os_initstdio();
  866.  
  867.   count = 0;
  868.   verbose = 1;
  869.   randomPic = 0;
  870.   slideShow = 0;
  871.  
  872.   BOX(1, 1, 80, 25, 40);
  873.   AT(1, 1);
  874.   printHelp();
  875.   safeKeys(keypress);
  876.  
  877. start:
  878.   keypress = 0;
  879.   switch (randomPic)
  880.   {
  881.   case 0:
  882.     iddqd = processJson(count, 1, 0);
  883.     break;
  884.   case 1:
  885.     iddqd = processJson(0, 1, 1);
  886.     break;
  887.   }
  888.  
  889.   if (iddqd < 0)
  890.   {
  891.     count++;
  892.     goto start;
  893.   }
  894.  
  895.   if (verbose == 1)
  896.   {
  897.     idkfa = processJson(atol(curFileStruct.authorIds), 0, 99);
  898.  
  899.     if (idkfa < 0)
  900.     {
  901.       printf(" Cant parse curFileStruct.authorIds = %s \r\n\r\n", curFileStruct.authorIds);
  902.       count++;
  903.       goto start;
  904.     }
  905.  
  906.     printData();
  907.   }
  908.   else
  909.   {
  910.     // ATRIB(97);
  911.     // printf(" Getting picture...\r\n");
  912.   }
  913.  
  914.   if (strcmp(curFileStruct.picType, "standard") != 0)
  915.   {
  916.     printf("  >>Format '%s' not supported, skipped \n\r", curFileStruct.picType);
  917.     count++;
  918.     goto start;
  919.   }
  920.  
  921.   errno = getPic(iddqd);
  922. review:
  923.   keypress = viewScreen6912((unsigned int)&picture, slideShowTime);
  924.   emptyKeys();
  925.  
  926.   ///// Keys only for pictures
  927.   if (keypress == 's' || keypress == 'S')
  928.   {
  929.     savePic(iddqd);
  930.     if (verbose == 1)
  931.       printf("        ID:%lu    TITLE:%s  SAVED\r\n\r\n", curFileStruct.picId, curFileStruct.picName);
  932.     count++;
  933.   }
  934.  
  935.   if (keypress == 248 || keypress == 'b' || keypress == 'B')
  936.   {
  937.     if (count > 0)
  938.     {
  939.       count--;
  940.     }
  941.   }
  942.   if (keypress == 251 || keypress == 32)
  943.   {
  944.     count++;
  945.     goto start;
  946.   }
  947.   if (keypress == 'i' || keypress == 'I')
  948.   {
  949.     delay(100);
  950.     getchar();
  951.     goto review;
  952.   }
  953.   safeKeys(keypress);
  954.   goto start;
  955. }
  956.